File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes Reading From a Binary Large Object(BLOB) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Reading From a Binary Large Object(BLOB)" Watch "Reading From a Binary Large Object(BLOB)" New topic

Reading From a Binary Large Object(BLOB)

Robb Glietz

Joined: Nov 08, 2001
Posts: 1
I am writting a text file into a BLOB, when I go to read it back out there are square boxes around my text.
Also I am only able to read it by using methods found in the class ByteArrayOutputStream. When I try to read it using the class ObjectInputStream all I get is the numbers which I guess are object refer numbers.
The code below will be the insert and the read along with the result which I am getting. I have left out the connection part simple because I know that is working since I do have information within the database.
Here is the code:
private void insertBLOB()
throws java.lang.Exception, java.sql.SQLException {
System.out.println("INSIDE insertBLOB");
BLOB blob = null;
File binFile = null;
BufferedInputStream bis = null;
FileInputStream fis = null;
OutputStream outstream = null;
String fileName = "forthrun";
String fileType = "byte";
String fileLocation ="C:\\test11.txt";
//Set AutoCommit to OFF - required by BLOB locking mechanism
//Create a statement
Statement stmt = connection.createStatement();
//Insert an empty BLOB locator
stmt.execute("INSERT INTO MEDIA_STORE VALUES('"+fileName+"','"+fileType+"',empty_blob())");
//excute the query and lock the BLOB row.
ResultSet rset = stmt.executeQuery("SELECT MDATA FROM MEDIA_STORE WHERE MNAME ='"+fileName+"' FOR UPDATE");;
//get the BLOB locator
blob = ((OracleResultSet) rset).getBLOB(1);
//Get the large binary media file
int size;
binFile = new File(fileLocation);
fis = new FileInputStream(binFile);
System.out.println("FILEINPUTSTREAM BYTES= " + size = fis.available());
bis = new BufferedInputStream(fis);
//insert to the BLOB from an output stream
outstream = blob.getBinaryOutputStream();
//Read the input stream and write the output stream by chucks
byte[] chunk = new byte[blob.getChunkSize()];
int i = -1;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);

while ((i = != -1)

byte [] bytes = baos.toByteArray();


//close the input and output stream

//close the statement
} catch (SQLException e)
} //end void
public void readBLOB()
throws, java.sql.SQLException, java.lang.ClassNotFoundException {
System.out.println("INSIDE readBLOB");

String strStatement = null;
Statement stmt = connection.createStatement();
BLOB blob = null;
ResultSet rs = null;
FileInputStream instream = null;
ObjectInputStream objectinputstream = null;
ByteArrayOutputStream byteOut = null;
BufferedInputStream bis = null;
//String inobject="";
String fileName ="forthrun";
try {
System.out.println("select the blob locator from the database");
rs = stmt.executeQuery("SELECT MDATA FROM MEDIA_STORE WHERE MNAME ='"+fileName+"'");;
System.out.println("get the BLOB locator");
blob = ((OracleResultSet) rs).getBLOB(1);
int length =(int) blob.length();
System.out.println("Length of blob is = " + length);
InputStream is = blob.getBinaryStream();
byteOut = new ByteArrayOutputStream();
int size = blob.getBufferSize();
System.out.println("size blob = "+ size);

byte[] buffer = new byte[size];
int read = 0;
while((read = !=-1){
System.out.println("inside while for read");
byteOut.writeTo(System.out);//this is what prints out content right now..
byte[] bytes = byteOut.toByteArray();
ByteArrayInputStream bytein = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bytein);

Object c = ois.readObject();

System.out.println("c= "+c);

} catch (Exception ex) {

}//end void
INSIDE openSession
select the blob locator from the database
get the BLOB locator
Length of blob is = 8159
size blob = 24396
inside while for read
T�This is a test to see what happens??T�
Thank you in advance for any help you can provide
Don't get me started about those stupid light bulbs.
subject: Reading From a Binary Large Object(BLOB)
It's not a secret anymore!