• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1886
  • Last Modified:

Convert String to InputStream

I need to insert a string into SQl Server database which has the datatype of image.  The same column is supposed to store two different types of data. One is  normal string format and another is of type object (Jasperprint.. if you know what I mean). I have converted the latter into a binary stream and I am able to store and retrieve it using the setbinarystream and getbinarystream methods. This is working fine. I am now stuck with the problem of inserting the normal string datatype into the same column. I need to convert this into InputStream as well. Anybody any ideas on how to do so??
  • 3
  • 3
1 Solution
Give this a shot . . .

If your String is named str, you can convert it to a java.io.ByteArrayInputStream (subclass of java.io.InputStream) by executing the following:

java.io.ByteArrayInputStream bais = new java.io.ByteArrayInputStream(str.getBytes());

I hope that helps.
You can create a serializable object that will hold both the byte array (from JasperPrint) and the string.
Then you can convert the object into a byte array by doing:
ByteArrayOutputStream output = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(output);
byte bytes[] = output.toByteArray();

then you can use the inputstream wrapper mentioned above to convert the byte array into an inputstream and save it to DB.

when loading from DB you need to apply the reverse opeartion.
ObjectInputStream input = new ObjectInputStream(your_db_result_set_inputstream);
input.readObject(); // this will return you the serialzed object which you can get the string and the byte array to fill your JasperPrint.
nagulasAuthor Commented:
I think there is some confusion here . At one point of time , the column will have either the JasperPrint object or the String. This is determined based on the value of another column in the database. So I either insert the string or the Jasperprint object. I understand that I might be able to insert the string as a ByteArrayInputStream . Then How do I convert  it back to a String when I retrieve it from the database in the form of a InputStream now??
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Using something like this:
// asuming your input stream from the DB is stored in variable in
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int length = 0;

while ((length = in.read(bytes)) > 0)
         out.write(bytes, 0, length);

String str = out.toString();
nagulasAuthor Commented:
Gee Thanks , that worked wunderfull!! I can now insert and retrieve it using the ByteArrayInput/Output Streams. I am going to try the same for the JasperPrint as well. I was using a FileInput/Output Stream for it earlier , but that makes files which is something I dont want. I want it to be read directly , so I gonna try this one out there. Thanks again.
nagulasAuthor Commented:
It worked fine for JasperPrint as well , thnks for the solution
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now