aspose file tools*
The moose likes JDBC and the fly likes why won't clob open Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "why won Watch "why won New topic
Author

why won't clob open

Jan Piper
Greenhorn

Joined: Jan 10, 2008
Posts: 13
I am trying to open oracle blobs and clobs to the screen in either Notepad or Wordpad. This code always works on the blob, sometimes on the clob and I can't figure out why it fails on certain resumes but not others. The data looks similar, some of the clobs are bigger than others, but I can't see anything specific to make it fail. I get no errors in my log, they just open with the text all ascii looking instead of readable text. If anybody has any ideas or areas for me to research, please let me know. I just now added the substring bit because I thought maybe some of the clobs were too big, but even 300 characters fail, so that's not it. Could there be something in the clob data itself that would blow this up when it is being converted to Wordpad? This is java servlet to an oracle database.

Statement stmt = con.createStatement();
ResultSet rs;
InputStream is = null;
oracle.sql.BLOB blob = null;
oracle.sql.CLOB clob = null;
String name = "";
int pos =0;
int length=0;
OutputStream os = res.getOutputStream();

try{

query = " select attached_resume, "
+ " SUBSTR(candidate_resume, 1, 300) , "
+ " uploaded_resume_file_name "
+ " from candidate_resume "
+ " where candidate_id = '"+candidateId+"' ";

rs = stmt.executeQuery(query);

while(rs.next()){
blob = (BLOB) rs.getBlob(1);
clob = (CLOB) rs.getClob(2);
name = rs.getString(3);
}

if(Common.isNotEmpty(name)){ //resume is attached, open it

String ext = name.substring(name.indexOf(".")+1, name.length());
res.setContentType("application/"+ext+"");
res.setHeader("Content-Disposition","attachment;filename="+name);
is = blob.getBinaryStream();

byte []b = new byte[blob.getChunkSize()];

while ((length=is.read(b)) != -1){
pos += length;
os.write(b);
}
}else{ //no attached resume, try to open clob
res.setContentType("application/txt");
res.setHeader("Content-Disposition","attachment;filename="+candidateId);
is = clob.getStream();

byte []b = new byte[clob.getChunkSize()];

while((length=is.read(b)) != -1){
pos += length;
os.write(b);
}
}

}catch (SQLException se){
se.printStackTrace();
}finally{
is.close();
os.close();
}

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18168
    
    8

There's a problem in this code:



The method "is.read(b)" reads some bytes into the byte array, and returns a value indicating exactly how many bytes it read. However "os.write(b)" writes the entire byte array, regardless of how many bytes were put into it by "is.read(b)".
Jan Piper
Greenhorn

Joined: Jan 10, 2008
Posts: 13
I'm confused.. If it writes the entire thing, why does that matter? So it's trying to write more than is there or ? Is there a fairly easy solution?
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1106

So it isn't returning an error? have you debugged it and seen the contents of a clob that won't load?

will one clob load sometimes but not others? or will certain clobs never load?
Jan Piper
Greenhorn

Joined: Jan 10, 2008
Posts: 13
Nothing in the catalina log. Wordpad always opens, so it's not a runtime error. Sometimes Wordpad opens fine and the resume is there. Click on a different resume, Wordpad opens and there's ascii there and then click on a different one and it's completely blank. There's data in the database, though. I can open it in TOAD and see what's there, but they look like regular resumes to me. I don't see anything to make them not appear correctly.

I thought maybe some were too big, so I used the substring to only show the first 3000 characters on one that wouldn't open, didn't help. Then I made it 300 characters, still didn't help.
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1106

Wendy Gibbons wrote:So it isn't returning an error? have you debugged it and seen the contents of a clob that won't load?

will one clob load sometimes but not others? or will certain clobs never load?


so certain clobs will never open?

when they have loaded can you do a system out to see the contents? see if anything is in there?


Jan Piper
Greenhorn

Joined: Jan 10, 2008
Posts: 13
Sorry, yes, certain ones will never open while others will always open. I put some printlns to the log and both appear to be returning something. The first one opened nicely, the second printed junk.

inside else
is-java.io.ByteArrayInputStream@4b2dd538
b-[B@2e097617
b-[B@2e097617

inside else
is-java.io.ByteArrayInputStream@40bb2bc3
b-[B@40363068
b-[B@40363068
Jan Piper
Greenhorn

Joined: Jan 10, 2008
Posts: 13
However, it's choking in the while loop, apparently. I put a println after the pos += and that line didn't print.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18168
    
    8

Jan Piper wrote:I'm confused.. If it writes the entire thing, why does that matter? So it's trying to write more than is there or ? Is there a fairly easy solution?


You read 100 bytes, then you write that 100 bytes and another 156 bytes which you don't know what they are... and you don't think that would matter?

And yes, there's an easy (not fairly easy) solution. Check the API documentation for overloaded versions of the OutputStream.write method.
Jan Piper
Greenhorn

Joined: Jan 10, 2008
Posts: 13
Except it failed before the os.write line. I am still confused though. If the first line returns the size of the clob. The second prints the entire size of the clob where is the problem?
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1106

you have a buffer that is 200 bytes long
but only 50 of the bytes have data in them, the other 150 have non printable garbage in it, what happens when you try to print it?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18168
    
    8

Jan Piper wrote:Except it failed before the os.write line. I am still confused though. If the first line returns the size of the clob...


"If" is correct. I haven't looked at the API documentation but I wouldn't expect a method named "getChunkSize" to return the size of the CLOB at all. Is that what its documentation says?

And even if it is, the assumption that the "is.read(b)" code will fill that byte array is still a careless one. Since your problem appears to be that you're sometimes getting corrupted data, it's a good idea to clean up code which might lead to corrupted data. Like the code I pointed out. So instead of trying to find reasons why your code is actually correct, even though it looks like it might be wrong, you should be fixing it so that it can't possibly be wrong.
Jan Piper
Greenhorn

Joined: Jan 10, 2008
Posts: 13
I agree and will fix that which might prevent a future problem. Thank you for that.

Now any ideas how I can fix this problem which is occurring before I call the writer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why won't clob open
 
Similar Threads
downloading a file
File download with servlets
Insert/Select Image from Oracle DB.
How to insert a file in oracle blob object
Servlet + Oracle9Ii help needed