StringReader vs. StringBufferInputStream


J

John English

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:[email protected]
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
-----------------------------------------------------------------
 
Ad

Advertisements

G

Gordon Beaton

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
 
J

John English

Gordon said:
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:[email protected]
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
-----------------------------------------------------------------
 
M

Michael Borgwardt

John said:
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)?
 
G

Gordon Beaton

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
 
Ad

Advertisements

J

John English

Michael said:
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:[email protected]
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
-----------------------------------------------------------------
 
Ad

Advertisements


Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top