You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by kr...@apache.org on 2010/06/15 10:21:08 UTC

svn commit: r954748 - /db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLBinary.java

Author: kristwaa
Date: Tue Jun 15 08:21:08 2010
New Revision: 954748

URL: http://svn.apache.org/viewvc?rev=954748&view=rev
Log:
DERBY-4686: SQLBinary.writeBlob is inefficient, reading one byte at a time from the source BLOB

Improves performance by reading and writing from/to a transfer buffer.

Patch contributed by Yun Lee (yun dot lee dot bj at gmail dot com).

Patch file: DERBY-4686-1.diff

Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLBinary.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLBinary.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLBinary.java?rev=954748&r1=954747&r2=954748&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLBinary.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLBinary.java Tue Jun 15 08:21:08 2010
@@ -31,6 +31,7 @@ import org.apache.derby.iapi.types.DataV
 import org.apache.derby.iapi.types.ConcatableDataValue;
 import org.apache.derby.iapi.error.StandardException;
 
+import org.apache.derby.iapi.services.io.DerbyIOException;
 import org.apache.derby.iapi.services.io.StoredFormatIds;
 import org.apache.derby.iapi.services.io.FormatIdInputStream;
 
@@ -101,6 +102,8 @@ abstract class SQLBinary
 
     private static final int BASE_MEMORY_USAGE = ClassSize.estimateBaseFromCatalog( SQLBinary.class);
 
+    private static final int LEN_OF_BUFFER_TO_WRITE_BLOB = 1024;
+
     public int estimateMemoryUsage()
     {
         if (dataValue == null) {
@@ -368,10 +371,23 @@ abstract class SQLBinary
             InputStream         is = _blobValue.getBinaryStream();
             
             writeLength( out, len );
-
-            for ( int i = 0; i < len; i++ )
-            {
-                out.write( is.read() );
+            
+            int bytesRead = 0;
+            int numOfBytes = 0;
+            byte[] buffer = new byte[LEN_OF_BUFFER_TO_WRITE_BLOB];
+            
+            while(bytesRead < len) {
+                numOfBytes = is.read(buffer);
+                
+                if (numOfBytes == -1) {
+                    throw new DerbyIOException(
+                        MessageService.getTextMessage(
+                                SQLState.SET_STREAM_INEXACT_LENGTH_DATA),
+                            SQLState.SET_STREAM_INEXACT_LENGTH_DATA);
+                }
+                
+                out.write(buffer, 0, numOfBytes);
+                bytesRead += numOfBytes; 
             }
         }
         catch (StandardException se) { throw new IOException( se.getMessage() ); }