db-derby-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Dag H. Wanvik" <Dag.Wan...@Sun.COM>
Subject Re: [jira] Created: (DERBY-463) Successive writes to a java.sql.Blob.setBinaryStream(long) seem to reset the file pointer
Date Fri, 15 Jul 2005 16:03:37 GMT

I got the same result using the new client driver (not DB2Driver). 
Looks like a bug.. I tried the embedded driver, but found
setBinaryStream isnt implemented there..

Dag



>>>>> "LA(" == Laurenz Albe (JIRA) <derby-dev@db.apache.org> wrote:
LA(> 
LA(> Successive writes to a java.sql.Blob.setBinaryStream(long) seem to reset the file
pointer
LA(> -----------------------------------------------------------------------------------------
LA(> 
LA(>          Key: DERBY-463
LA(>          URL: http://issues.apache.org/jira/browse/DERBY-463
LA(>      Project: Derby
LA(>         Type: Bug
LA(>   Components: JDBC  
LA(>     Versions: 10.0.2.1    
LA(>  Environment: Sun java full version "1.4.2_05-b04"
LA(> Linux x86
LA(> Derby is run in network server mode
LA(>     Reporter: Laurenz Albe
LA(> 
LA(> 
LA(> I have a table
LA(> PEOPLE(SEQ_ID INT NOT NULL PRIMARY KEY, PICTURE BLOB).
LA(> 
LA(> A row is inserted; both values are not NULL.
LA(> 
LA(> >From inside a JDBC program, I select the Blob for update.
LA(> I then get the Blob output stream with a call to
LA(>   Blob.setBinaryStream(long)
LA(> To this stream I write several times with
LA(>   OutputStream.write(byte[], int, int)
LA(> I close the stream, update the selected row with the new Blob and commit.
LA(> 
LA(> The new value of the Blob now is exactly the value of the last content of the byte[],
LA(> and it is like the previous calls to write() have never taken place, or as if the
file pointer
LA(> of the output stream has been reset between the calls.
LA(> 
LA(> A sample program follows; the size of the input file "picture.jpg" is 23237, the length
LA(> of the Blob after the program has run is 23237 % 1024 = 709
LA(> 
LA(> ------------ sample program -------------
LA(> import java.sql.*;
LA(> 
LA(> class TestApp {
LA(>    private TestApp() {}
LA(> 
LA(>    public static void main(String[] args)
LA(>          throws ClassNotFoundException, SQLException, java.io.IOException {
LA(>       // try to load JDBC driver
LA(>       Class.forName("com.ibm.db2.jcc.DB2Driver");
LA(> 
LA(>       // open the input file
LA(>       java.io.InputStream instream = new java.io.FileInputStream("picture.jpg");
LA(> 
LA(>       // login to database
LA(>       Connection conn = DriverManager.getConnection(
LA(>             "jdbc:derby:net://dbtuxe/testdb", "laurenz", "apassword");
LA(>       conn.setAutoCommit(false);
LA(> 
LA(>       // select Blob for update
LA(>       PreparedStatement stmt = conn.prepareStatement(
LA(>             "SELECT PICTURE FROM PEOPLE WHERE SEQ_ID=? FOR UPDATE OF PICTURE");
LA(>       stmt.setInt(1, 1);
LA(>       ResultSet rs = stmt.executeQuery();
LA(> 
LA(>       // get Blob output stream
LA(>       rs.next();
LA(>       Blob blob = rs.getBlob(1);
LA(>       java.io.OutputStream outstream = blob.setBinaryStream(1l);
LA(> 
LA(>       // copy the input file to the Blob in chunks of 1K
LA(>       byte[] buf = new byte[1024];
LA(>       int count;
LA(>       while (-1 != (count = instream.read(buf))) {
LA(>          outstream.write(buf, 0, count);
LA(>          System.out.println("Written " + count + " bytes to Blob");
LA(>       }
LA(> 
LA(>       // close streams
LA(>       instream.close();
LA(>       outstream.close();
LA(> 
LA(>       // update Blob with new value
LA(>       String cursor = rs.getCursorName();
LA(>       PreparedStatement stmt2 = conn.prepareStatement(
LA(>             "UPDATE PEOPLE SET PICTURE=? WHERE CURRENT OF " + cursor);
LA(>       stmt2.setBlob(1, blob);
LA(>       stmt2.executeUpdate();
LA(> 
LA(>       // clean up
LA(>       stmt2.close();
LA(>       stmt.close();
LA(>       conn.commit();
LA(>       conn.close();
LA(>    }
LA(> }
LA(> 
LA(> -- 
LA(> This message is automatically generated by JIRA.
LA(> -
LA(> If you think it was sent incorrectly contact one of the administrators:
LA(>    http://issues.apache.org/jira/secure/Administrators.jspa
LA(> -
LA(> For more information on JIRA, see:
LA(>    http://www.atlassian.com/software/jira
LA(> 
-- 
Dag H. Wanvik
Sun Microsystems, Web Services, Database Technology Group
Haakon VII gt. 7b, N-7485 Trondheim, Norway
Tel: x43496/+47 73842196, Fax:  +47 73842101

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
NOTICE: This email message is for the sole use of the intended
recipient(s) and may contain confidential and privileged
information. Any unauthorized review, use, disclosure or distribution
is prohibited. If you are not the intended recipient, please contact
the sender by reply email and destroy all copies of the original
message.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

Mime
View raw message