Trying to read blob and output the content in bytes-please help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • skunapareddy
    New Member
    • Oct 2008
    • 3

    Trying to read blob and output the content in bytes-please help

    I am needing to read a blob from database and pass it to another java program. I researched internet and found a program that reads a file on the client pc and gives bytes, but when I modified the code to read a blob from DB I am not having any luck, I am running this java programs using JDEVELOPER. Can anyone please give me some help?
    The code is as follows:
    Code:
    ...
    
    private static byte[] getBytesFromBlob(Blob blob) throws Exception {
    
    InputStream is = blob.getBinaryStream();
    System.out.println("\nDEBUG: BlobInputStream is " );
    
    // Get the size of the blob
    int length = -1;
    
    int size = (int)blob.length();
    
    System.out.println("DEBUG: Length of blob" + " is " + size + "\n");
    
    /*
    * You cannot create an array using a long type. It needs to be an int
    * type. Before converting to an int type, check to ensure that file is
    * not loarger than Integer.MAX_VALUE;
    */
    if (length > Integer.MAX_VALUE) {
    System.out.println("File is too large to process");
    return null;
    }
    
    // Create the byte array to hold the data
    byte[] bytes = new byte[(int)size];
    
    // Read in the bytes
    int offset = 0;
    int numRead = 0;
    while ( (offset < bytes.length)
    &&
    ( (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) ) {
    
    offset += numRead;
    
    }
    
    // Ensure all the bytes have been read in
    if (offset < bytes.length) {
    throw new Exception("Could not completely read file " );
    }
    
    is.close();
    return bytes;
    
    }
    
    ...
    The output from ReadFileIntoByt eArray is as follows:
    Code:
    DEBUG: FileInputStream is c:\eicar.com
    DEBUG: Length of c:\eicar.com is 68
    
    fileArray[0] = 88 : HEX=(0x58) : charValue=(X)
    fileArray[1] = 53 : HEX=(0x35) : charValue=(5)
    fileArray[2] = 79 : HEX=(0x4f) : charValue=(O)
    fileArray[3] = 33 : HEX=(0x21) : charValue=(!)
    ...
    
    fileArray[66] = 72 : HEX=(0x48) : charValue=(H)
    fileArray[67] = 42 : HEX=(0x2a) : charValue=(*)
    ---------
    The result of ReadBlobIntoByt eArray is as follows:
    58354F215025404 1505B345C505A58 353428505E29374 34329377D244549 4341522D5354414 E444152442D414E 544956495255532 D544553542D4649 4C452124482B482 A

    The result I am getting is in hex, instead of in bytes. Please any advice is appreciated.

    rgds,
    sumak
    Last edited by Nepomuk; Nov 10 '08, 04:49 PM. Reason: The CODE tags are in the format [CODE], not <CODE>
  • skunapareddy
    New Member
    • Oct 2008
    • 3

    #2
    Please can anyone give me some hints???? please....

    Comment

    • chaarmann
      Recognized Expert Contributor
      • Nov 2007
      • 785

      #3
      You have all your data aready stored as bytes (not as hex-code as what you have written), in variable "byte[] bytes"

      But I can't see here any code that passes it to another java program. You only have code to print it to screen.

      The code that read the blob from database is also missing. But you must have it somewhere, as what I can see from the program output.

      When you put-out the bytes to the screen, you should know that not all characters are printable, so you would not see them correctly (imagine putting out the bell-character which gives only a sound, or putting out the backspace character, which overwrites the character previously written).
      So it is already very wise that you print them all as decimal value, hex-value and character.
      So if you don't want the hex or decimal values being displayed, then simply remove the statement that prints them. But strange: you have done a big effort to develop all the code for hexadecimal output and then that would be for nothing.
      A final warning: save all your stuff well before running what you want: If you only want to print out the bytes as they are and the Blob is very long, your computer will not stop beeping for a very, very long time! You got what you wanted!

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        @OP: you wrote that you found this program on the internet. Are you sure that
        you can freely use and/or copy/modify it? Did you contact the author?

        btw, just dumping your code here without the proper code tags ruins the entire
        source code; e.g. the [ i ] index operators are gone completely. I wonder whether
        you understand what the code actually does.

        kind regards,

        Jos

        Comment

        • skunapareddy
          New Member
          • Oct 2008
          • 3

          #5
          Hi JosAH,
          First of all I apologise for posting my whole code! It was in desparation to find an answer to my problem. And yes, I do understand what the code does and how it works. But, I am not a java programmer. I did write to the author of this piece of code! I ended up using pl/sql procedure, so I have my problem solved. You can delete this thread if you like.

          Rgds,
          Suma.
          Last edited by skunapareddy; Nov 12 '08, 01:53 PM. Reason: a typo in his name

          Comment

          Working...