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
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
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
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
> 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
What about:
StreamResponse sr = new TextStreamResponse("text/plain",
yourstringwriter.toString());
?
Arne
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);
> [...]
> 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
> 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