Plesae help- needing to read a blob from db into bytes[]
843785Sep 25 2008 — edited Sep 26 2008Hi all,
I am having a requirement to read a blob stored in the oracle table and convert it into bytes. I am loading this table (wwv_flow_files) with APEX.
The code under page 1 is as follows:
DECLARE
z number;
y varchar2(4000);
x varchar2(400);
b blob;
BEGIN
select filename,blob_content into x ,b from APEX_APPLICATION_files where name =:P1_FILE_NAME;
select length(convertBlobToBytes(b)) into z from dual;
:P1_RESULT := z;
end;
Java code is as follows:
import java.io.*;
import java.sql.Blob;
public class convertBlob {
/**
* @param blob
* @return
*/
public static byte[] convertBlobToBytes(Blob blob) {
if (blob==null) return null;
try {
InputStream in = blob.getBinaryStream();
int len = (int) blob.length(); //read as long
long pos = 1; //indexing starts from 1
byte[] bytes = blob.getBytes(pos, len);
in.close();
return bytes;
}
catch (Exception e) {
System.out.println(e.getMessage());
}
return null;
}
}
PL/SQL wrapper is as follows:
CREATE OR REPLACE FUNCTION convertBlobToBytes(p1 IN BLOB) RETURN LONG RAW AUTHID CURRENT_USER AS LANGUAGE JAVA NAME 'convertBlob.convertBlobToBytes(java.sql.Blob) return byte[]';
I loaded this java class and pl/sql wrapper into the database using JDEVELOPER.
But I am getting the length of the file, as twice the size.
For example, When I run the program which reads the file returns the length of the file as a byte array, the length is 819.
When I pass the same file as a blob from apex, to the java program that converts blob to bytes, the length of the file is 1638.
And hence I am getting wrong results, further in the process.
Can you please help me? Any help is appreciated.
rgds,
Suma.