• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1854
  • 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??
0
nagulas
Asked:
nagulas
  • 3
  • 3
1 Solution
 
bcsonkaCommented:
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.
0
 
aozarovCommented:
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);
out.writeObject(your_serialized_object);
out.close();
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.
0
 
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??
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
aozarovCommented:
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();
0
 
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.
0
 
nagulasAuthor Commented:
It worked fine for JasperPrint as well , thnks for the solution
0
 
aozarovCommented:
:-)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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