This discussion is archived
3 Replies Latest reply: Jan 12, 2007 9:53 AM by 475314 RSS

reading blob and passing it as byte[]

438458 Newbie
Currently Being Moderated
Hi,

I apologize if this is not in the right forum. I just started using blob. I have gone through many tutorials and forums, but I am not able to make my application work.

I need to retrieve value from blob field in Oracle database. I need to convert that into a byte array. I need to pass that to another class. I need to use this byte array to print a part of document. (The document has many sections, and this blob text is one of the sections).

Also I have no idea of the value in the blob field, (though I think its a word document) and I do not know what encoding is done for it. BTW, is this necessary information, should I need to look at the encoding, if so what encoding is done for word document.


blob = ((OracleResultSet)rsRefStandardConditionData).getBLOB ("AAAA");
byte[] buffer = blob.getBytes(1, (int) blob.length());
object.setValue(buffer);



For this, the output I am getting is junk with some text. But the text is like A,s,d,f,g,h,j,k. I am not sure if this is right.

I also tried


byte[] returndata = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
BufferedInputStream bis = new BufferedInputStream( rsRefStandardConditionData.getBinaryStream("AAA") );
byte bindata[] = new byte[1024];
int bytesread = 0;
try{
//if ( (bytesread = bis.read(bindata,0,bindata.length)) != -1 ) {
     while ( (bytesread = bis.read(bindata)) != -1 ) {
baos.write(bindata,0,bytesread);

     
     }
returndata = baos.toByteArray();
object.setValue(returndata);
}catch(Exception e){
                    e.printStackTrace();
               }




Any help in this would be greatly appreciated.

Thanks
  • 1. Re: reading blob and passing it as byte[]
    475314 Newbie
    Currently Being Moderated
    First of all; Blob (Binary large object) is a collection of binary data stored as a single entity in the database. Basically Byte array in the database... So, I am not sure what do you mean with "encoding", if you mean "character encoding", you shouldn't care about it since it is binary data and you do not want to convert it to String.

    And here is the code snippet that retrieves BLOG field to Byte array in Java

    byte[] returnBytes;
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    ... PREPARE THE QUERY ...

    result = stmt.executeQuery();

    if (result.next())
    {
    Blob blob = result.getBlob("BLOB_FIELD_NAME");
    InputStream inputStream = blob.getBinaryStream();

    int inByte;

    while ((Size = inputStream.read()) != -1)
    {
    byteArrayOutputStream.write(inByte);
    }

    returnBytes = byteArrayOutputStream.toByteArray();
    }
  • 2. Re: reading blob and passing it as byte[]
    438458 Newbie
    Currently Being Moderated
    Thanks for your reply.

    Actually I want the byte array to be converted into a word document.

    i.e. A Microsoft Word Document was stored in Blob field. I need to get that and combine into a big master document with some other data. So I need the word document in the way as it was before storage.

    Now, I am able to retrieve the Blob data, but its binary format. I need the clear text to display. I tried converting a byte array to String using encodings, but its the same.

    Any help would be great.

    Thanks.
  • 3. Re: reading blob and passing it as byte[]
    475314 Newbie
    Currently Being Moderated
    You have to use MS Office API to access/modify word document.