473,324 Members | 2,581 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 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. ...
  2.  
  3. private static byte[] getBytesFromBlob(Blob blob) throws Exception {
  4.  
  5. InputStream is = blob.getBinaryStream();
  6. System.out.println("\nDEBUG: BlobInputStream is " );
  7.  
  8. // Get the size of the blob
  9. int length = -1;
  10.  
  11. int size = (int)blob.length();
  12.  
  13. System.out.println("DEBUG: Length of blob" + " is " + size + "\n");
  14.  
  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. }
  24.  
  25. // Create the byte array to hold the data
  26. byte[] bytes = new byte[(int)size];
  27.  
  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) ) {
  34.  
  35. offset += numRead;
  36.  
  37. }
  38.  
  39. // Ensure all the bytes have been read in
  40. if (offset < bytes.length) {
  41. throw new Exception("Could not completely read file " );
  42. }
  43.  
  44. is.close();
  45. return bytes;
  46.  
  47. }
  48.  
  49. ...
  50.  
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
  3.  
  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. ...
  9.  
  10. fileArray[66] = 72 : HEX=(0x48) : charValue=(H)
  11. fileArray[67] = 42 : HEX=(0x2a) : charValue=(*) 
  12.  
---------
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.

rgds,
sumak
Nov 3 '08 #1
4 4195
Please can anyone give me some hints???? please....
Nov 5 '08 #2
chaarmann
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
JosAH
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,

Jos
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.

Rgds,
Suma.
Nov 12 '08 #5

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

Similar topics

1
by: Ike | last post by:
What object do I use for a blob that must be >64 k ?-Ike
4
by: Mark J. McGinty | last post by:
Greets, Part of the content of one of our web pages uses wingdings and Chr(239) through Chr(242) (which are little arrow outlines, though that's not really important.) It worked just fine in...
4
by: Peter Scott | last post by:
I created a table that has a column in that needs to contain a full Unix file path. Since 2048 was too long for a VARCHAR, I made it TEXT. I since populated the table. Now I want to make the...
18
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
4
by: Lingyun Yang | last post by:
*** post for FREE via your newsreader at post.newsfeed.com *** Dear all, I have a file it's binary data viewed in UltraEdit is EF BB BF 0D 0A 3C ....... I want to read them into a int or long...
4
by: Jonathan | last post by:
I have a client solution that requires data and associated files to be stored with data in a database. As such, I have a situation where JPEG thumbnails/images that are stored as BLOBs (image...
6
by: Selen | last post by:
I would like to be able to extract a BLOB from the database (SqlServer) and pass it to a browser without writing it to a file. (The BLOB's are word doc's, MS project doc's, and Excel spreadsheets....
5
by: snicks | last post by:
I'm trying to exec a program external to my ASP.NET app using the following code. The external app is a VB.NET application. Dim sPPTOut As String sPPTOut = MDEPDirStr + sID + ".ppt" Dim p As...
5
by: Martin | last post by:
I get below error when trying to write unicode xml to a zipfile. zip.writestr('content.xml', content.toxml()) File "/usr/lib/python2.4/zipfile.py", line 460, in writestr zinfo.CRC =...
1
by: jamal8t2 | last post by:
i have inserted blob data in database , but i am not able to access back that data to the browser. i have php script but it does not work proprly. our script is below if ($_REQUEST == 1) { ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.