Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

From StringWriter to InputStream

2,281 views
Skip to first unread message

angelo...@gmail.com

unread,
Mar 28, 2009, 7:42:16 PM3/28/09
to
Hi,

how to get an InputStream from a StringWriter, I know this works:

is = new StringInputStream(myStringWriter)

but this introduce an dependency to
org.apache.tools.ant.filters.StringInputStream;

Thanks,

Angelo

Peter Duniho

unread,
Mar 28, 2009, 8:02:37 PM3/28/09
to

Maybe you could be more specific about what you're trying to do. On the
face of it, your question doesn't make sense: an InputStream is for
reading, and a StringWriter is for writing. The fact that anyone has
published a class with "InputStream" in its name but which is compatible
with StringWriter seems odd to me. I don't think I'd expect the stock
Java API to include anything to support that.

Pete

Joshua Cranmer

unread,
Mar 28, 2009, 7:56:40 PM3/28/09
to
angelo...@gmail.com wrote:
> how to get an InputStream from a StringWriter, I know this works:

What exactly are you trying to do that involves turning a Writer into an
InputStream, specifically a StringWriter?

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth

Angelo Chen

unread,
Mar 28, 2009, 8:09:17 PM3/28/09
to
Hi Pete,

Thanks for the quick reply, here is what I'm doing:

in a servlet app, I use a StringWriter to capture some information,
then to stream it back to the browser, I need to have a
StreamResponse:

http://tapestry.apache.org/tapestry5/apidocs/org/apache/tapestry/StreamResponse.html

and this one requires a InputStream for the data source, in my case,
the data is in the StringWriter


On Mar 29, 8:02 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> On Sat, 28 Mar 2009 16:42:16 -0700, angelochen...@gmail.com  

Peter Duniho

unread,
Mar 28, 2009, 8:26:34 PM3/28/09
to
On Sat, 28 Mar 2009 17:09:17 -0700, Angelo Chen <angelo...@gmail.com>
wrote:

> Hi Pete,
>
> Thanks for the quick reply, here is what I'm doing:
>
> in a servlet app, I use a StringWriter to capture some information,
> then to stream it back to the browser, I need to have a
> StreamResponse:
>
> http://tapestry.apache.org/tapestry5/apidocs/org/apache/tapestry/StreamResponse.html
>
> and this one requires a InputStream for the data source, in my case,
> the data is in the StringWriter

I have the feeling I'm overlooking something, but I don't see any direct
way to get an InputStream from a String. The docs unhelpfully state, in
regards to the StringBufferInputStream (the only String-related
InputStream class I see), "This class does not properly convert characters
into bytes. As of JDK 1.1, the preferred way to create a stream from a
string is via the StringReader class". But StringReader only provides a
character stream, not a byte stream that would be necessary in order to
get an InputStream.

That said, you can get a byte[] from the String you can get from the
StringWriter, using the Charset class. Then you can wrap that byte[] in a
ByteArrayInputStream to get the InputStream you require. Don't forget to
make sure you use the encoding that your StreamResponse expects!

Pete

Arne Vajhøj

unread,
Mar 28, 2009, 9:40:22 PM3/28/09
to
Angelo Chen wrote:
> in a servlet app, I use a StringWriter to capture some information,
> then to stream it back to the browser, I need to have a
> StreamResponse:
>
> http://tapestry.apache.org/tapestry5/apidocs/org/apache/tapestry/StreamResponse.html
>
> and this one requires a InputStream for the data source, in my case,
> the data is in the StringWriter

What about:

StreamResponse sr = new TextStreamResponse("text/plain",
yourstringwriter.toString());

?

Arne

Mike Schilling

unread,
Mar 28, 2009, 9:43:44 PM3/28/09
to

You'd like to do something like

InputStream is = new ReaderInputStream(new
StringReader(myStringWriter.toString(), encoding)

Unfortunately, Java fails to provide the ReaderInputStream (which
would be the obverse of InputStreamReader), so the best you can do is

byte[] barray = myStringWriter.toString().getBytes(encoding);
InputStream is = new ByteArrayInputStream(barray);


Peter Duniho

unread,
Mar 28, 2009, 10:37:57 PM3/28/09
to
On Sat, 28 Mar 2009 18:43:44 -0700, Mike Schilling
<mscotts...@hotmail.com> wrote:

> [...]


> Unfortunately, Java fails to provide the ReaderInputStream (which
> would be the obverse of InputStreamReader), so the best you can do is
>
> byte[] barray = myStringWriter.toString().getBytes(encoding);
> InputStream is = new ByteArrayInputStream(barray);

That's basically what I suggested. But I think Arne's suggestion is
better (just goes to show how useful it can be to look at the docs related
to what you're actually doing, instead of just trying for a standard Java
library solution :) ).

Pete

Angelo Chen

unread,
Mar 29, 2009, 12:22:48 AM3/29/09
to
On Mar 29, 10:37 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:

> On Sat, 28 Mar 2009 18:43:44 -0700, Mike Schilling  
>

Hi all,

Thanks for answering my question, with your help, i found this works:

new ByteArrayInputStream(sw.toString().getBytes());

but Arne's is more 'context correct':

class StrStreamResponse extends TextStreamResponse{

Thanks,

Angelo

Tom Anderson

unread,
Mar 30, 2009, 1:59:17 PM3/30/09
to
On Sat, 28 Mar 2009, Mike Schilling wrote:

> angelo...@gmail.com wrote:
>
>> how to get an InputStream from a StringWriter, I know this works:
>>
>> is = new StringInputStream(myStringWriter)
>>
>> but this introduce an dependency to
>> org.apache.tools.ant.filters.StringInputStream;
>
> You'd like to do something like
>
> InputStream is = new ReaderInputStream(new
> StringReader(myStringWriter.toString(), encoding)
>
> Unfortunately, Java fails to provide the ReaderInputStream (which
> would be the obverse of InputStreamReader),

I'm not sure i'd call that an obverse, but never mind.

> so the best you can do is
>
> byte[] barray = myStringWriter.toString().getBytes(encoding);
> InputStream is = new ByteArrayInputStream(barray);

Or you could do it with an OutputStreamWriter and a coroutine. :)

tom

--
The future will accost us with boob-slapping ferocity. -- H. G. Wells

danielesp...@gmail.com

unread,
Aug 11, 2015, 8:07:12 PM8/11/15
to
Hola espero que les ayude


Charset charset = Charset.forName("UTF-8");
CharsetEncoder encoder = charset.newEncoder();
CharBuffer buffer = CharBuffer.wrap(stringWriter.getBuffer());
byte[] bytesStringWriter = encoder.encode(buffer).array();

InputStream inputStreamStringWriter = new ByteArrayInputStream(bytesStringWriter);

0 new messages