473,395 Members | 1,919 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,395 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 4203
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.