StringReader vs. StringBufferInputStream

Discussion in 'Java' started by John English, Sep 20, 2004.

  1. John English

    John English Guest

    I want to use a method that takes an InputStream parameter and supply
    it with data that's stored in a String. I can easily do this using a
    StringBufferInputStream, but the compiler moans at me that this class
    is deprecated and the API spec says I should use StringReader instead.

    However, I can't find any way of turning a StringReader into an
    InputStream so that I can use it as a parameter for the method I
    need to use. I'm still using the deprecated StringBufferInputStream
    since I can't find any other way to do it.

    Surely there must be a way to do this using a StringReader, or what's
    the point of deprecating StringBufferInputStream? Aaargh!

    Any help much appreciated,

    -----------------------------------------------------------------
    John English | mailto:
    Senior Lecturer | http://www.it.bton.ac.uk/staff/je
    School of Computing & MIS | ** NON-PROFIT CD FOR CS STUDENTS **
    University of Brighton | -- see http://burks.bton.ac.uk
    -----------------------------------------------------------------
     
    John English, Sep 20, 2004
    #1
    1. Advertising

  2. On Mon, 20 Sep 2004 12:18:58 +0100, John English wrote:
    > I want to use a method that takes an InputStream parameter and supply
    > it with data that's stored in a String. I can easily do this using a
    > StringBufferInputStream, but the compiler moans at me that this class
    > is deprecated and the API spec says I should use StringReader instead.
    >
    > However, I can't find any way of turning a StringReader into an
    > InputStream so that I can use it as a parameter for the method I
    > need to use. I'm still using the deprecated StringBufferInputStream
    > since I can't find any other way to do it.


    byte[] byteArray = myString.getBytes("ISO-8859-1"); // choose a charset
    ByteArrayInputStream baos = new ByteArrayInputStream(byteArray);

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Sep 20, 2004
    #2
    1. Advertising

  3. John English

    John English Guest

    Gordon Beaton wrote:
    > On Mon, 20 Sep 2004 12:18:58 +0100, John English wrote:
    >
    >>I want to use a method that takes an InputStream parameter and supply
    >>it with data that's stored in a String. I can easily do this using a
    >>StringBufferInputStream, but the compiler moans at me that this class
    >>is deprecated and the API spec says I should use StringReader instead.
    >>
    >>However, I can't find any way of turning a StringReader into an
    >>InputStream so that I can use it as a parameter for the method I
    >>need to use. I'm still using the deprecated StringBufferInputStream
    >>since I can't find any other way to do it.

    >
    >
    > byte[] byteArray = myString.getBytes("ISO-8859-1"); // choose a charset
    > ByteArrayInputStream baos = new ByteArrayInputStream(byteArray);


    Thanks. This will work for most cases. But, what do I do if I want
    to read Unicode characters rather than bytes?

    -----------------------------------------------------------------
    John English | mailto:
    Senior Lecturer | http://www.it.bton.ac.uk/staff/je
    School of Computing & MIS | ** NON-PROFIT CD FOR CS STUDENTS **
    University of Brighton | -- see http://burks.bton.ac.uk
    -----------------------------------------------------------------
     
    John English, Sep 30, 2004
    #3
  4. John English wrote:
    >>> However, I can't find any way of turning a StringReader into an
    >>> InputStream so that I can use it as a parameter for the method I
    >>> need to use. I'm still using the deprecated StringBufferInputStream
    >>> since I can't find any other way to do it.

    >>
    >>
    >>
    >> byte[] byteArray = myString.getBytes("ISO-8859-1"); // choose a charset
    >> ByteArrayInputStream baos = new ByteArrayInputStream(byteArray);

    >
    >
    > Thanks. This will work for most cases. But, what do I do if I want
    > to read Unicode characters rather than bytes?


    You said you wanted this for use with a method that requires an
    InputStream, NOT a StringReader as a parameter. So what is it?
    An InputStream (==bytes) or a StringReader (== unicode characters)?
     
    Michael Borgwardt, Sep 30, 2004
    #4
  5. On Thu, 30 Sep 2004 15:20:29 +0100, John English wrote:
    > Thanks. This will work for most cases. But, what do I do if I want
    > to read Unicode characters rather than bytes?


    To read characters, use a Reader. You can create a StringReader from
    your original String, or wrap an InputStreamReader around a
    ByteArrayInputStream if you've got an array of bytes. Just make sure
    you specify the right character encoding.

    An InputStream (which you previously said your method requires) is for
    reading raw bytes, not characters.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Sep 30, 2004
    #5
  6. John English

    John English Guest

    Michael Borgwardt wrote:

    > John English wrote:
    >
    >> Thanks. This will work for most cases. But, what do I do if I want
    >> to read Unicode characters rather than bytes?

    >
    > You said you wanted this for use with a method that requires an
    > InputStream, NOT a StringReader as a parameter. So what is it?
    > An InputStream (==bytes) or a StringReader (== unicode characters)?


    Ah, OK. Good point... :)

    -----------------------------------------------------------------
    John English | mailto:
    Senior Lecturer | http://www.it.bton.ac.uk/staff/je
    School of Computing & MIS | ** NON-PROFIT CD FOR CS STUDENTS **
    University of Brighton | -- see http://burks.bton.ac.uk
    -----------------------------------------------------------------
     
    John English, Oct 1, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Silas Snider

    StringBufferInputStream deprecation

    Silas Snider, Aug 25, 2004, in forum: Java
    Replies:
    2
    Views:
    4,637
    Simon Fairfax
    Aug 25, 2004
  2. Patricia Shanahan

    What replaces StringBufferInputStream

    Patricia Shanahan, Aug 29, 2006, in forum: Java
    Replies:
    45
    Views:
    19,951
    Dale King
    Sep 5, 2006
  3. MattB
    Replies:
    7
    Views:
    764
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    May 16, 2007
Loading...

Share This Page