PLEASE can anyone help me to read bytes from a blob?
522653Oct 30 2008 — edited Nov 12 2008Hi all,
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>
import java.io.InputStream;
import java.sql.Blob;
public class ReadBlobIntoByteArray {
/**
* method to convert a byte to a hex string.
*
* @param data the byte to convert
* @return String the converted byte
*/
public static String byteToHex(byte data) {
StringBuffer buf = new StringBuffer();
buf.append(toHexChar((data >>> 4) & 0x0F));
buf.append(toHexChar(data & 0x0F));
return buf.toString();
}
/**
* Convenience method to convert an int to a hex char.
*
* @param i the int to convert
* @return char the converted char
*/
public static char toHexChar(int i) {
if ((0 <= i) && (i <= 9)) {
return (char) ('0' + i);
} else {
return (char) ('a' + (i - 10));
}
}
/**
* Returns the contents of the file in a byte array
* @param file File this method should read
* @return byte[] Returns a byte[] array of the contents of the file
*/
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;
}
/**
* @param filename
*/
public static byte[] chk_blob(Blob b) {
byte[] fileArray = null;
try {
fileArray = getBytesFromBlob(b);
} catch (Exception e) {
e.printStackTrace();
}
if (fileArray != null) {
for (int i=0; i<fileArray.length; i++) {
System.out.println(
"fileArray[" + i + "] = " +
((int)fileArray[i] < 9 ? " " : "") +
( ((int)fileArray[i] > 9 && (int)fileArray[i] <= 99) ? " " : "") +
fileArray[i] + " : " +
" HEX=(0x" + byteToHex(fileArray) + ") : " +
" charValue=(" + (char)fileArray[i] + ")");
}
}
return fileArray;
}
}
</code>
The result of ReadBlobIntoByteArray is as follows:
58354F2150254041505B345C505A58353428505E2937434329377D2445494341522D5354414E444152442D414E544956495255532D544553542D46494C452124482B482A
Edited by: sumak on Nov 5, 2008 5:41 AM