Convert String to InputStream

Posted on 2005-04-12
Last Modified: 2012-05-05
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??
Question by:nagulas
    LVL 2

    Expert Comment

    Give this a shot . . .

    If your String is named str, you can convert it to a (subclass of by executing the following: bais = new;

    I hope that helps.
    LVL 15

    Expert Comment

    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.

    Author Comment

    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??
    LVL 15

    Accepted Solution

    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 = > 0)
             out.write(bytes, 0, length);

    String str = out.toString();

    Author Comment

    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.

    Author Comment

    It worked fine for JasperPrint as well , thnks for the solution
    LVL 15

    Expert Comment


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
    This video teaches viewers about errors in exception handling.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now