What replaces StringBufferInputStream

Discussion in 'Java' started by Patricia Shanahan, Aug 29, 2006.

  1. I need to generate an InputStream from a String containing some test data.

    StringBufferInputStream is deprecated, and the documentation points to
    StringReader.

    However, after looking through java.io several times, I have not found
    how to construct an InputStream from a Reader.

    What is the proper, undeprecated, replacement code for:

    InputStream in = new StringBufferInputStream(someString);

    Thanks,

    Patricia
     
    Patricia Shanahan, Aug 29, 2006
    #1
    1. Advertising

  2. Patricia Shanahan wrote:
    > I need to generate an InputStream from a String containing some test data.
    >
    > StringBufferInputStream is deprecated, and the documentation points to
    > StringReader.
    >
    > However, after looking through java.io several times, I have not found
    > how to construct an InputStream from a Reader.
    >
    > What is the proper, undeprecated, replacement code for:
    >
    > InputStream in = new StringBufferInputStream(someString);


    InputStream in = new ByteArrayInputStream(someString.getBytes(encoding));

    must be a candidate.

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Aug 29, 2006
    #2
    1. Advertising

  3. Arne Vajhøj wrote:
    > Patricia Shanahan wrote:
    >> I need to generate an InputStream from a String containing some test
    >> data.
    >>
    >> StringBufferInputStream is deprecated, and the documentation points to
    >> StringReader.
    >>
    >> However, after looking through java.io several times, I have not found
    >> how to construct an InputStream from a Reader.
    >>
    >> What is the proper, undeprecated, replacement code for:
    >>
    > > InputStream in = new StringBufferInputStream(someString);

    >
    > InputStream in = new ByteArrayInputStream(someString.getBytes(encoding));
    >
    > must be a candidate.
    >
    > Arne
    >


    Thanks.

    That works, and gets rid of the warnings. But why does the
    StringBufferInputStream documentation say "As of JDK 1.1, the preferred
    way to create a stream from a string is via the StringReader class." if
    StringReader cannot do StringBufferInputReader's job?

    Patricia
     
    Patricia Shanahan, Aug 29, 2006
    #3
  4. Patricia Shanahan wrote:
    > That works, and gets rid of the warnings. But why does the
    > StringBufferInputStream documentation say "As of JDK 1.1, the preferred
    > way to create a stream from a string is via the StringReader class." if
    > StringReader cannot do StringBufferInputReader's job?


    I dont know.

    A guess could be that a typical usage of StringBufferInputReader
    was to wrap it in an InputStreamReader.

    But this is pure guessing.

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Aug 29, 2006
    #4
  5. Patricia Shanahan wrote:
    > I need to generate an InputStream from a String containing some test data.
    >
    > StringBufferInputStream is deprecated, and the documentation points to
    > StringReader.
    >
    > However, after looking through java.io several times, I have not found
    > how to construct an InputStream from a Reader.
    >
    > What is the proper, undeprecated, replacement code for:
    >
    > InputStream in = new StringBufferInputStream(someString);


    From Java's viewpoint, you're looking at it wrong. In the normal case,
    since 1.1, Java doesn't want you to use an InputStream for anything, but
    a Reader (unless you need a DataInputStream).

    Reader in = new StringReader(someString);

    --
    John W. Kennedy
    "The blind rulers of Logres
    Nourished the land on a fallacy of rational virtue."
    -- Charles Williams. "Taliessin through Logres: Prelude"
     
    John W. Kennedy, Aug 29, 2006
    #5
  6. John W. Kennedy wrote:
    > Patricia Shanahan wrote:
    >> I need to generate an InputStream from a String containing some test
    >> data.
    >>
    >> StringBufferInputStream is deprecated, and the documentation points to
    >> StringReader.
    >>
    >> However, after looking through java.io several times, I have not found
    >> how to construct an InputStream from a Reader.
    >>
    >> What is the proper, undeprecated, replacement code for:
    >>
    >> InputStream in = new StringBufferInputStream(someString);

    >
    > From Java's viewpoint, you're looking at it wrong. In the normal case,
    > since 1.1, Java doesn't want you to use an InputStream for anything, but
    > a Reader (unless you need a DataInputStream).
    >
    > Reader in = new StringReader(someString);
    >


    I'm writing a unit test for a class that reads from InputStream. The
    string contains the test data I want it to operate on.

    The class I'm testing is intended to deal with the stdout and stderr
    streams from a java.lang.Process job, and java.lang.Process does not
    seem to have heard of Reader.

    Patricia
     
    Patricia Shanahan, Aug 29, 2006
    #6
  7. Patricia Shanahan

    Chris Uppal Guest

    John W. Kennedy wrote:

    > From Java's viewpoint, you're looking at it wrong. In the normal case,
    > since 1.1, Java doesn't want you to use an InputStream for anything, but
    > a Reader (unless you need a DataInputStream).


    Maybe that came out wrong, but taken literally it is completely false.
    InputStreams are central to the IO architecture.

    -- chris
     
    Chris Uppal, Aug 29, 2006
    #7
  8. Patricia Shanahan

    Chris Uppal Guest

    Patricia Shanahan wrote:

    > But why does the
    > StringBufferInputStream documentation say "As of JDK 1.1, the preferred
    > way to create a stream from a string is via the StringReader class." if
    > StringReader cannot do StringBufferInputReader's job?


    My guess is that they intended to create in InputStream-flavoured wrapper for
    Readers, but never got around to it. Probably they argued for a few days about
    what to call it, but could neither stomach ReaderInputStream nor agree on
    anything else, and just quietly dropped it.

    "Nobody's going to need it anyway"

    ;-)

    -- chris
     
    Chris Uppal, Aug 29, 2006
    #8
  9. On 29.08.2006 03:32, Patricia Shanahan wrote:
    > Arne Vajhøj wrote:
    >> Patricia Shanahan wrote:
    >>> I need to generate an InputStream from a String containing some test
    >>> data.
    >>>
    >>> StringBufferInputStream is deprecated, and the documentation points to
    >>> StringReader.
    >>>
    >>> However, after looking through java.io several times, I have not found
    >>> how to construct an InputStream from a Reader.


    > That works, and gets rid of the warnings. But why does the
    > StringBufferInputStream documentation say "As of JDK 1.1, the preferred
    > way to create a stream from a string is via the StringReader class." if
    > StringReader cannot do StringBufferInputReader's job?


    Um, as far as I can see there is no StringBufferInputReader. The
    replacement for StringBufferInputStream is InputStreamReader - the major
    difference is that the latter properly deals with encodings. In your
    case the default constructor is probably most appropriate because the
    process will likely emit its output using the default encoding of the
    platform. If not, you need a way to make sure the process and your
    InputStreamReader use the same encoding.

    HTH

    Kind regards

    robert
     
    Robert Klemme, Aug 29, 2006
    #9
  10. Patricia Shanahan

    Chris Uppal Guest

    Patricia Shanahan wrote:

    > I'm writing a unit test for a class that reads from InputStream. The
    > string contains the test data I want it to operate on.


    Wouldn't it make more sense to hold the test data in a byte[] array ? That,
    after all, is what your real data will look like -- binary, coming from an
    external application using who-knows-what encoding.

    -- chris
     
    Chris Uppal, Aug 29, 2006
    #10
  11. Patricia Shanahan

    M.J. Dance Guest

    Patricia Shanahan wrote:

    > What is the proper, undeprecated, replacement code for:
    >
    > InputStream in = new StringBufferInputStream(someString);


    There is no proper replacement. The line of code above is mixing two
    superficially similar but inherently different things: bytes and chars. Of
    course the two are related but, in order to fully describe that relatinship, one
    needs additional information: character encoding. Having that, one can
    getBytes() from a String and, using those, create a ByteArrayInputStream.

    There are some inconsistencies in Java API as far as such situations are
    concerned. Sun decided to deprecate a whole StringBufferInputStream (instead of
    deprecating only a constructor and adding a new one specifying encoding). Yet
    they chose a different approach with String for example. One can construct a new
    String from an array of bytes despite the fact that by doing that, one relies on
    default/platform character encoding. Assuming that this encoding is the right
    one, one can get desired results. But we all know that assumption is the mother
    of all FUs, don't we?


    So, whatever you do, don't... ;-)
     
    M.J. Dance, Aug 29, 2006
    #11
  12. Chris Uppal wrote:
    > Patricia Shanahan wrote:
    >
    >> I'm writing a unit test for a class that reads from InputStream. The
    >> string contains the test data I want it to operate on.

    >
    > Wouldn't it make more sense to hold the test data in a byte[] array ? That,
    > after all, is what your real data will look like -- binary, coming from an
    > external application using who-knows-what encoding.


    My real data is text. I find it much faster to write text as Strings.

    The solution, as Arne pointed out, is to first get a byte array from the
    String. If internationalization were an issue, I would specify an
    encoding on that conversion.

    In the real application, the data will be coming from Matlab. The
    application only needs to work on a few hundred systems, three of which
    I control and the remaining systems are all part of a UCSD grid
    computer, so I do know what encoding.

    Patricia
     
    Patricia Shanahan, Aug 29, 2006
    #12
  13. Patricia Shanahan

    vahan Guest

    As we know java String is char array. When we look through code source
    for
    StringBufferInputStream and StringReader read method we can see
    difference
    between:

    StringBufferInputStream:
    public synchronized int read() {
    return (pos < count) ? (buffer.charAt(pos++) & 0xFF) : -1;

    StringReader:
    public int read() throws IOException {
    synchronized (lock) {
    ensureOpen();
    if (next >= length)
    return -1;
    return str.charAt(next++);
    }
    }

    As we see StringBufferInputStream's read method return only low byte
    from char as int . That is why it is deprecated.
    Best Vahan

    Patricia Shanahan wrote:
    > Arne Vajhøj wrote:
    > > Patricia Shanahan wrote:
    > >> I need to generate an InputStream from a String containing some test
    > >> data.
    > >>
    > >> StringBufferInputStream is deprecated, and the documentation points to
    > >> StringReader.
    > >>
    > >> However, after looking through java.io several times, I have not found
    > >> how to construct an InputStream from a Reader.
    > >>
    > >> What is the proper, undeprecated, replacement code for:
    > >>
    > > > InputStream in = new StringBufferInputStream(someString);

    > >
    > > InputStream in = new ByteArrayInputStream(someString.getBytes(encoding));
    > >
    > > must be a candidate.
    > >
    > > Arne
    > >

    >
    > Thanks.
    >
    > That works, and gets rid of the warnings. But why does the
    > StringBufferInputStream documentation say "As of JDK 1.1, the preferred
    > way to create a stream from a string is via the StringReader class." if
    > StringReader cannot do StringBufferInputReader's job?
    >
    > Patricia
     
    vahan, Aug 29, 2006
    #13
  14. vahan wrote:
    > As we know java String is char array. When we look through code source
    > for
    > StringBufferInputStream and StringReader read method we can see
    > difference
    > between:
    >
    > StringBufferInputStream:
    > public synchronized int read() {
    > return (pos < count) ? (buffer.charAt(pos++) & 0xFF) : -1;
    >
    > StringReader:
    > public int read() throws IOException {
    > synchronized (lock) {
    > ensureOpen();
    > if (next >= length)
    > return -1;
    > return str.charAt(next++);
    > }
    > }
    >
    > As we see StringBufferInputStream's read method return only low byte
    > from char as int . That is why it is deprecated.
    > Best Vahan


    I don't have a problem with StringBufferInputStream being deprecated,
    and understand perfectly well why.

    The question is what replaces it, when the recommendation in the
    documentation, StringReader, does not seem to do the whole job, because
    there does not seem to be any way to get from there to an InputStream.

    Patricia
     
    Patricia Shanahan, Aug 29, 2006
    #14
  15. Patricia Shanahan

    Chris Uppal Guest

    Patricia Shanahan wrote:

    > The solution, as Arne pointed out, is to first get a byte array from the
    > String. If internationalization were an issue, I would specify an
    > encoding on that conversion.


    I'd still specify the encoding explicitly -- you may as well be clear on what
    the test is actually testing.

    BTW, I don't believe that character encodings should be thought of as just an
    internationalisation issue. (And even they if were, you -- being a
    foreigner -- shouldn't ignore those issues ;-)

    Matlab's a powerful program; surely it can be told to produce its output in
    UTF-8 or UTF-16...

    -- chris
     
    Chris Uppal, Aug 29, 2006
    #15
  16. Chris Uppal wrote:
    > Patricia Shanahan wrote:
    >
    >> The solution, as Arne pointed out, is to first get a byte array from the
    >> String. If internationalization were an issue, I would specify an
    >> encoding on that conversion.

    >
    > I'd still specify the encoding explicitly -- you may as well be clear on what
    > the test is actually testing.
    >
    > BTW, I don't believe that character encodings should be thought of as just an
    > internationalisation issue. (And even they if were, you -- being a
    > foreigner -- shouldn't ignore those issues ;-)
    >
    > Matlab's a powerful program; surely it can be told to produce its output in
    > UTF-8 or UTF-16...


    Matlab is extremely powerful in specific directions. The documentation
    for the student version I have has no hits for "UTF", "international",
    and "unicode", but pages of hits for "eigen". The only hit for
    "encoding" is in the Signal Processing Toolbox and "quantizes the
    entries in a multidimensional array of floating-point numbers u and
    encodes them as integers". "ASCII" is used in the documentation as being
    the alternative to "binary".

    The downloaded code that I'm using is all matrix-to-matrix internal
    operations, not I/O.

    I suspect the Matlab stdout and stderr are going to be predominantly in
    the ASCII character set. Any non-ASCII text would have to be in the
    default encoding for the platform.

    Patricia
     
    Patricia Shanahan, Aug 29, 2006
    #16
  17. Patricia Shanahan <> writes:

    > The question is what replaces it, when the recommendation in the
    > documentation, StringReader, does not seem to do the whole job, because
    > there does not seem to be any way to get from there to an InputStream.


    My guess is that a String should not be convertible to an InputStream
    of bytes directly, because there is so many ways a String can be made
    into bytes with none of them being an obvious default. I.e., asking to
    go directly from a String to an InputStream was asking for something
    that was better done in two separable steps: String to bytes, bytes to
    stream.

    Strings contain characters, so the most fitting sequential input to
    convert it to would be a Reader.

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Aug 29, 2006
    #17
  18. "Patricia Shanahan" <> wrote in message
    news:pAXIg.11588$...
    > Chris Uppal wrote:
    >> Patricia Shanahan wrote:
    >>
    >>> The solution, as Arne pointed out, is to first get a byte array from the
    >>> String. If internationalization were an issue, I would specify an
    >>> encoding on that conversion.

    >>
    >> I'd still specify the encoding explicitly -- you may as well be clear on
    >> what
    >> the test is actually testing.
    >>
    >> BTW, I don't believe that character encodings should be thought of as
    >> just an
    >> internationalisation issue. (And even they if were, you -- being a
    >> foreigner -- shouldn't ignore those issues ;-)
    >>
    >> Matlab's a powerful program; surely it can be told to produce its output
    >> in
    >> UTF-8 or UTF-16...

    >
    > Matlab is extremely powerful in specific directions. The documentation
    > for the student version I have has no hits for "UTF", "international",
    > and "unicode", but pages of hits for "eigen". The only hit for
    > "encoding" is in the Signal Processing Toolbox and "quantizes the
    > entries in a multidimensional array of floating-point numbers u and
    > encodes them as integers". "ASCII" is used in the documentation as being
    > the alternative to "binary".
    >
    > The downloaded code that I'm using is all matrix-to-matrix internal
    > operations, not I/O.
    >
    > I suspect the Matlab stdout and stderr are going to be predominantly in
    > the ASCII character set. Any non-ASCII text would have to be in the
    > default encoding for the platform.


    Which may or may not be the default encoding for any particular Java
    installation.

    I'm with Chris on this. If nothing else, specifying "US-ASCII" or
    "ISO_8851_1" (or whatever) is awfully cheap documentation about what sort of
    input your code expects, and will make debugging any future miscues far
    eaiser for whoever's maintaining it then.
     
    Mike Schilling, Aug 29, 2006
    #18
  19. Patricia Shanahan

    Oliver Wong Guest

    "Mike Schilling" <> wrote in message
    news:xo%Ig.4293$...
    >
    > "Patricia Shanahan" <> wrote in message
    > news:pAXIg.11588$...
    >> Chris Uppal wrote:
    >>> Patricia Shanahan wrote:
    >>>
    >>>> The solution, as Arne pointed out, is to first get a byte array from
    >>>> the
    >>>> String. If internationalization were an issue, I would specify an
    >>>> encoding on that conversion.
    >>>
    >>> I'd still specify the encoding explicitly -- you may as well be clear on
    >>> what
    >>> the test is actually testing.
    >>>
    >>> BTW, I don't believe that character encodings should be thought of as
    >>> just an
    >>> internationalisation issue. (And even they if were, you -- being a
    >>> foreigner -- shouldn't ignore those issues ;-)
    >>>
    >>> Matlab's a powerful program; surely it can be told to produce its output
    >>> in
    >>> UTF-8 or UTF-16...

    >>
    >> Matlab is extremely powerful in specific directions. The documentation
    >> for the student version I have has no hits for "UTF", "international",
    >> and "unicode", but pages of hits for "eigen".

    [...]
    >> I suspect the Matlab stdout and stderr are going to be predominantly in
    >> the ASCII character set. Any non-ASCII text would have to be in the
    >> default encoding for the platform.

    [...]

    > If nothing else, specifying "US-ASCII" or "ISO_8851_1" (or whatever) is
    > awfully cheap documentation about what sort of input your code expects,
    > and will make debugging any future miscues far eaiser for whoever's
    > maintaining it then.


    Alternatively, it might be more accurate to put a TODO with something
    like:

    /*TODO: I don't know what encoding MatLab actually uses. If you find out,
    set it here.*/

    Setting an explicit encoding (to me) implies that that's the actual
    encoding you want to use, as opposed to you having just chosen an encoding
    randomly because you didn't know which one was appropriate.

    - Oliver
     
    Oliver Wong, Aug 29, 2006
    #19
  20. "Oliver Wong" <> wrote in message
    news:ic0Jg.22473$tP4.17596@clgrps12...
    >
    > "Mike Schilling" <> wrote in message
    > news:xo%Ig.4293$...
    >>
    >> "Patricia Shanahan" <> wrote in message
    >> news:pAXIg.11588$...
    >>> Chris Uppal wrote:
    >>>> Patricia Shanahan wrote:
    >>>>
    >>>>> The solution, as Arne pointed out, is to first get a byte array from
    >>>>> the
    >>>>> String. If internationalization were an issue, I would specify an
    >>>>> encoding on that conversion.
    >>>>
    >>>> I'd still specify the encoding explicitly -- you may as well be clear
    >>>> on what
    >>>> the test is actually testing.
    >>>>
    >>>> BTW, I don't believe that character encodings should be thought of as
    >>>> just an
    >>>> internationalisation issue. (And even they if were, you -- being a
    >>>> foreigner -- shouldn't ignore those issues ;-)
    >>>>
    >>>> Matlab's a powerful program; surely it can be told to produce its
    >>>> output in
    >>>> UTF-8 or UTF-16...
    >>>
    >>> Matlab is extremely powerful in specific directions. The documentation
    >>> for the student version I have has no hits for "UTF", "international",
    >>> and "unicode", but pages of hits for "eigen".

    > [...]
    >>> I suspect the Matlab stdout and stderr are going to be predominantly in
    >>> the ASCII character set. Any non-ASCII text would have to be in the
    >>> default encoding for the platform.

    > [...]
    >
    >> If nothing else, specifying "US-ASCII" or "ISO_8851_1" (or whatever) is
    >> awfully cheap documentation about what sort of input your code expects,
    >> and will make debugging any future miscues far eaiser for whoever's
    >> maintaining it then.

    >
    > Alternatively, it might be more accurate to put a TODO with something
    > like:
    >
    > /*TODO: I don't know what encoding MatLab actually uses. If you find out,
    > set it here.*/


    Yes, I did mean setting it *after* finding out the correct value :)
     
    Mike Schilling, Aug 29, 2006
    #20
    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. David

    what replaces #include in .Net?

    David, Jul 2, 2004, in forum: ASP .Net
    Replies:
    8
    Views:
    3,064
    John Saunders
    Jul 2, 2004
  2. Robert Mazur

    What replaces RMI?

    Robert Mazur, Feb 5, 2004, in forum: Java
    Replies:
    3
    Views:
    683
    Chris Smith
    Feb 6, 2004
  3. Silas Snider

    StringBufferInputStream deprecation

    Silas Snider, Aug 25, 2004, in forum: Java
    Replies:
    2
    Views:
    4,675
    Simon Fairfax
    Aug 25, 2004
  4. John English
    Replies:
    5
    Views:
    28,058
    John English
    Oct 1, 2004
  5. Dick Wall
    Replies:
    0
    Views:
    636
    Dick Wall
    Oct 15, 2005
Loading...

Share This Page