PDA

View Full Version : char[] to InputStream


_underdog
01-03-2003, 03:47 PM
Say I have a method that takes a File or a InputStream as an argument, but I have the data in a String or char[]. It would be stupid to write it out to a file and then read it back in. Is there some easy way I can put this data into an InputStream that can be passed to the method.

inkedmn
01-03-2003, 04:36 PM
what exactly are you trying to do?

_underdog
01-03-2003, 06:05 PM
I am trying to do XML transformations of an XML file that is stored in a zip archive. When I read the zip entry from the zip file it is in a byte[]. I wanted to use that as a source for my transform.. I found that I could use a ByteArrayInputStream.

//xmlData is a byte[]
Source xmlSource = new StreamSource(new ByteArrayInputStream(xmlData));

phubuh
01-15-2003, 07:01 PM
Characters aren't bytes. Characters are characters.


Source xmlSource = new StreamSource(new StringBufferInputStream(new StringBuffer(xmlData)));

_underdog
01-15-2003, 07:55 PM
JavaTM 2 Platform
Std. Ed. v1.4.0

java.io
Class StringBufferInputStream

Deprecated. 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 that was not the point of what I was trying to do... I had read data in from a file into a buffer and I wanted to use this data in a method (in this case it was the costructor for a StreamSource) that took as arguments a File or an InputStream. The ByteArrayInputStream worked great.

Is there any way something like this could have been achieved if the only argument allowed had been a File?