If you need to make a string into an input stream it is really easy. Many people say you shouldnt convert a string into an input stream and that it is bad code design though. Since I was using someone elses library that only took input streams and all of my input was created as strings i really had no choice but to do the conversion (which is wastefull of memory cause you essentially have exact copies of the same data). Making a string into an inputstream can be done like this:
ByteArrayInputStream bs = new ByteArrayInputStream(site.getBytes());
If you have any problems leave a comment.

Comments (7)
works like a charm - thanks!
Posted by chris markiewicz | April 27, 2005 6:58 PM
Posted on April 27, 2005 18:58
Thanks, it worked for me too. I thought I was going to have to use a deprecated class:
StringBufferInputStream sbisContents = new StringBufferInputStream (contents);
when I came across this.
Phil
Posted by Phil Pitha | November 18, 2005 6:42 PM
Posted on November 18, 2005 18:42
The problem with this is that it uses the default encoding of the system.
Posted by Scott Chun | December 22, 2005 6:42 PM
Posted on December 22, 2005 18:42
Maven provides such a class. If interested you could probably look into the code.. :)
Posted by Mohnish | February 23, 2006 9:39 AM
Posted on February 23, 2006 09:39
here's my problem:
I want to implement dynamic report with jasper
report. I implement a custom tag, get it's body
content and pass to jasper report . The code
like this :
............
byte[] bytes = bodyContent.getString().getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
try{
JasperReport jasperReport = JasperCompileManager.compileReport(bais);
ServletOutputStream outputStream = pageContext.getResponse().getOutputStream();
//**********************Export to PDF***************
byte[] bytes = JasperRunManager.runReportToPdf(jasperReport,new HashMap(), new JREmptyDataSource());
pageContext.getResponse().setContentType("application/pdf");
pageContext.getResponse().setContentLength(bytes.length);
outputStream.write(bytes, 0, bytes.length);
outputStream.flush();
outputStream.close();
}
catch (JRException e){
e.printStackTrace();
}
............
it doesn't work. Can anybody help me ?
It's OK if i write body content on hard disk (.jrxml file) and pass it to compilerReport() method.
Thanks.
Posted by tuan tu | April 22, 2006 6:14 AM
Posted on April 22, 2006 06:14
I had the same issue, so I googled and get in this page. Your conversion suggestion made me sad, cause this kind of approach is neither complex or reusable.
The problem is that u only get java's byte representation of a String (@see java.lang.Character etc.) not the text-containing byte stream for the target platform.
The key is to use a conversion to a target java.nio.charset.Charset and use it's CharsetEncoder (Charset#newEncoder()). That's pitty I searched the whole day for this microIssue, but neither here / on java tutorials / somewhere else this key task isn't mentioned. I hope this helps u.
Posted by tomas | February 24, 2007 3:15 PM
Posted on February 24, 2007 15:15
using self made and configured java.nio.ByteBuffer for storing the CharsetDecoder#decode(buff ...) output you can achieve a conversion to other systems with different(java default is big endian) byteOrder (little/big endian).
finally create ByteArrayInputStream over ByteBuffer#array().
the simplier way to achieve String -> InputStream conversion is really the String#getBytes() method to achieve conversion by the local default charset. Or use String#getBytes(String charsetName) method to convert to other encoding (like US-ASCII wchich actually I do need while sending generated text content to an FTP server)
so long......
and thanks for all the fish
tomas r
Posted by tomas r | February 24, 2007 8:07 PM
Posted on February 24, 2007 20:07