« CVS stuff | Main | Moving to a New Server »

Java string to inputstream

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)

chris markiewicz:

works like a charm - thanks!

Phil Pitha:

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

Scott Chun:

The problem with this is that it uses the default encoding of the system.

Mohnish:

Maven provides such a class. If interested you could probably look into the code.. :)

tuan tu:

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.

tomas:

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.

tomas r:

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

Post a comment