db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From krist...@apache.org
Subject svn commit: r746236 - in /db/derby/code/trunk/java: engine/org/apache/derby/impl/jdbc/UpdatableBlobStream.java testing/org/apache/derbyTesting/functionTests/tests/jdbc4/BlobTest.java
Date Fri, 20 Feb 2009 13:48:42 GMT
Author: kristwaa
Date: Fri Feb 20 13:48:42 2009
New Revision: 746236

URL: http://svn.apache.org/viewvc?rev=746236&view=rev
Log:
DERBY-4061:InputStream returned from Blob.getBinaryStream(long, long) terminates the stream
by returning 0, should return -1.
Fixed bug where calling code would enter infinite loop because 0 was returned instead of -1
(EOF). The problem occurred because the actual number of bytes to read was calculated and
the case where zero bytes remained wasn't handled properly.
Patch file: derby-4061-1b.diff


Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/UpdatableBlobStream.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/BlobTest.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/UpdatableBlobStream.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/UpdatableBlobStream.java?rev=746236&r1=746235&r2=746236&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/UpdatableBlobStream.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/UpdatableBlobStream.java Fri
Feb 20 13:48:42 2009
@@ -174,7 +174,13 @@
      */
     public int read(byte[] b, int off, int len) throws IOException {
         updateIfRequired();
-        int actualLength = (int) Math.min(len, maxPos - pos);
+        long remaining = maxPos - pos;
+        // Return EOF if the maximum allowed position has been reached,
+        // and we're trying to read at least one byte.
+        if (remaining == 0 && len > 0) {
+            return -1;
+        }
+        int actualLength = (int) Math.min(len, remaining);
         int retValue = stream.read(b, off, actualLength);
         if (retValue > 0)
             pos += retValue;
@@ -201,12 +207,7 @@
      * @see java.io.InputStream#read(byte[])
      */
     public int read(byte[] b) throws IOException {
-        updateIfRequired();
-        int actualLength = (int) Math.min(b.length, maxPos - pos);
-        int retValue = stream.read(b, 0, actualLength);
-        if (retValue > 0)
-            pos += retValue;
-        return retValue;
+        return read(b, 0, b.length);
     }
 
     /**

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/BlobTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/BlobTest.java?rev=746236&r1=746235&r2=746236&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/BlobTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/BlobTest.java
Fri Feb 20 13:48:42 2009
@@ -519,6 +519,22 @@
         }
     }
 
+    /**
+     * Tests that draining a "sub-stream" from the Blob works.
+     * This is a repro for DERBY-4061, where we ended up with an infinite loop.
+     */
+    public void testGetBinaryStreamLongDrain()
+            throws IOException, SQLException {
+        initializeLongBlob(); // Ignoring id for now, use instance variable.
+        InputStream in = blob.getBinaryStream(2000, 5000);
+        byte[] buf = new byte[256];
+        while (in.read(buf, 0, buf.length) != -1) {
+            // This should end when we have read all the bytes in the stream.
+            // If the code hangs here, see DERBY-4061.
+        }
+        in.close();
+        blob.free();
+    }
     
     /**
      * Tests that the InputStream got from



Mime
View raw message