Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > StringReader vs. StringBufferInputStream

Reply
Thread Tools

StringReader vs. StringBufferInputStream

 
 
John English
Guest
Posts: n/a
 
      09-20-2004
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 | private.php?do=newpm&u=
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
-----------------------------------------------------------------
 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      09-20-2004
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
 
Reply With Quote
 
John English
Guest
Posts: n/a
 
      09-30-2004
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 | private.php?do=newpm&u=
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
-----------------------------------------------------------------
 
Reply With Quote
 
Michael Borgwardt
Guest
Posts: n/a
 
      09-30-2004
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)?
 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      09-30-2004
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
 
Reply With Quote
 
John English
Guest
Posts: n/a
 
      10-01-2004
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 | private.php?do=newpm&u=
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
-----------------------------------------------------------------
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
1.1 vs. 2.0 and DataSet.ReadXML(StringReader) MattB ASP .Net 7 05-16-2007 09:17 PM
What replaces StringBufferInputStream Patricia Shanahan Java 45 09-05-2006 03:46 PM
StringBufferInputStream deprecation Silas Snider Java 2 08-25-2004 09:38 PM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57