• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

why won't clob open

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 L Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
However, it's choking in the while loop, apparently. I put a println after the pos += and that line didn't print.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 L Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic