471,595 Members | 856 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,595 software developers and data experts.

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:
Expand|Select|Wrap|Line Numbers
  1. ...
  3. private static byte[] getBytesFromBlob(Blob blob) throws Exception {
  5. InputStream is = blob.getBinaryStream();
  6. System.out.println("\nDEBUG: BlobInputStream is " );
  8. // Get the size of the blob
  9. int length = -1;
  11. int size = (int)blob.length();
  13. System.out.println("DEBUG: Length of blob" + " is " + size + "\n");
  15. /*
  16. * You cannot create an array using a long type. It needs to be an int
  17. * type. Before converting to an int type, check to ensure that file is
  18. * not loarger than Integer.MAX_VALUE;
  19. */
  20. if (length > Integer.MAX_VALUE) {
  21. System.out.println("File is too large to process");
  22. return null;
  23. }
  25. // Create the byte array to hold the data
  26. byte[] bytes = new byte[(int)size];
  28. // Read in the bytes
  29. int offset = 0;
  30. int numRead = 0;
  31. while ( (offset < bytes.length)
  32. &&
  33. ( (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) ) {
  35. offset += numRead;
  37. }
  39. // Ensure all the bytes have been read in
  40. if (offset < bytes.length) {
  41. throw new Exception("Could not completely read file " );
  42. }
  44. is.close();
  45. return bytes;
  47. }
  49. ...
The output from ReadFileIntoByteArray is as follows:
Expand|Select|Wrap|Line Numbers
  1. DEBUG: FileInputStream is c:\eicar.com
  2. DEBUG: Length of c:\eicar.com is 68
  4. fileArray[0] = 88 : HEX=(0x58) : charValue=(X)
  5. fileArray[1] = 53 : HEX=(0x35) : charValue=(5)
  6. fileArray[2] = 79 : HEX=(0x4f) : charValue=(O)
  7. fileArray[3] = 33 : HEX=(0x21) : charValue=(!)
  8. ...
  10. fileArray[66] = 72 : HEX=(0x48) : charValue=(H)
  11. fileArray[67] = 42 : HEX=(0x2a) : charValue=(*) 
The result of ReadBlobIntoByteArray is as follows:
58354F2150254041505B345C505A58353428505E2937434329 377D2445494341522D5354414E444152442D414E5449564952 55532D544553542D46494C452124482B482A

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

Nov 3 '08 #1
4 4013
Please can anyone give me some hints???? please....
Nov 5 '08 #2
785 Expert 512MB
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!
Nov 5 '08 #3
11,448 Expert 8TB
@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,

Nov 5 '08 #4
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.

Nov 12 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Ike | last post: by
4 posts views Thread by Mark J. McGinty | last post: by
4 posts views Thread by Peter Scott | last post: by
18 posts views Thread by jas | last post: by
4 posts views Thread by Lingyun Yang | last post: by
4 posts views Thread by Jonathan | last post: by
6 posts views Thread by Selen | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.