From StringWriter to InputStream

A

angelochen960

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
 
J

Joshua Cranmer

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?
 
A

Arne Vajhøj

M

Mike Schilling

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;

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);
 
A

Angelo Chen

[...]
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

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
 
T

Tom Anderson

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
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top