db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From be...@apache.org
Subject svn commit: r546838 - in /db/derby/code/trunk/java: engine/org/apache/derby/impl/jdbc/EmbedBlob.java engine/org/apache/derby/impl/jdbc/UpdateableBlobStream.java testing/org/apache/derbyTesting/functionTests/tests/jdbc4/BlobTest.java
Date Wed, 13 Jun 2007 12:25:14 GMT
Author: bernt
Date: Wed Jun 13 05:25:13 2007
New Revision: 546838

URL: http://svn.apache.org/viewvc?view=rev&rev=546838
Log:
DERBY-2730 Embedded method Blob.getBinaryStream. Submitted by V.Narayanan

Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedBlob.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/UpdateableBlobStream.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/EmbedBlob.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedBlob.java?view=diff&rev=546838&r1=546837&r2=546838
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedBlob.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedBlob.java Wed Jun 13 05:25:13
2007
@@ -938,7 +938,30 @@
      */
     public InputStream getBinaryStream(long pos, long length)
         throws SQLException {
-        throw Util.notImplemented();
+        //call checkValidity to exit by throwing a SQLException if
+        //the Blob object has been freed by calling free() on it
+        checkValidity();
+        
+        if (pos <= 0) {
+            throw Util.generateCsSQLException(
+                    SQLState.BLOB_BAD_POSITION,
+                    new Long(pos));
+        }
+        if (length < 0) {
+            throw Util.generateCsSQLException(
+                    SQLState.BLOB_NONPOSITIVE_LENGTH,
+                    new Long(length));
+        }
+        if (length > (this.length() - pos)) {
+            throw Util.generateCsSQLException(
+                    SQLState.POS_AND_LENGTH_GREATER_THAN_LOB,
+                    new Long(pos), new Long(length));
+        }
+        
+        return new UpdateableBlobStream(this,
+                                        getBinaryStream(),
+                                        pos-1,
+                                        length);
     }
     
     /*

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/UpdateableBlobStream.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/UpdateableBlobStream.java?view=diff&rev=546838&r1=546837&r2=546838
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/UpdateableBlobStream.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/UpdateableBlobStream.java Wed
Jun 13 05:25:13 2007
@@ -47,6 +47,12 @@
     /* Current position of this stream in number of bytes. */
     private long pos;
     private final EmbedBlob blob;
+    
+    /**
+     * Position in Blob where to stop reading.
+     */
+    private long maxPos;
+    
 
     /**
      * Constructs UpdateableBlobStream using the the InputStream receives as the
@@ -58,6 +64,48 @@
         stream = is;
         this.pos = 0;
         this.blob = blob;
+        //The subset of the Blob
+        //has not been requested.
+        //Hence set maxPos to -1.
+        this.maxPos = -1;
+    }
+    
+    /**
+     * Construct an <code>UpdateableBlobStream<code> using the 
+     * <code>InputStream</code> received as parameter. The initial
+     * position in the stream is set to <code>pos</code> and the
+     * stream is restricted to a length of <code>len</code>.
+     *
+     * @param blob EmbedBlob this stream is associated with.
+     * @param is InputStream this class is going to use internally.
+     * @param pos initial position
+     * @param len The length to which the underlying <code>InputStream</code>
+     *            has to be restricted.
+     * @throws IOException
+     */
+    UpdateableBlobStream (EmbedBlob blob, InputStream is, long pos, long len) 
+    throws SQLException {
+        this(blob, is);
+        //The length requested cannot exceed the length
+        //of the underlying Blob object. Hence chose the
+        //minimum of the length of the underlying Blob
+        //object and requested length.
+        maxPos = Math.min(blob.length(), pos + len);
+        
+        try {
+            //Skip to the requested position
+            //inside the stream.
+            skip(pos);
+        }
+        catch(IOException ioe) {
+            //Skip throws an IOException. Wrap the
+            //exception inside a SQLException and 
+            //throw it to the caller.
+            
+            SQLException sqle = new SQLException();
+            sqle.initCause(ioe);
+            throw sqle;
+        }
     }
 
     /**
@@ -118,6 +166,14 @@
      */
     public int read() throws IOException {
         updateIfRequired();
+        
+        //If maxPos is not invalid and the current
+        //position inside the stream has exceeded
+        //maxPos the read sould return -1 signifying
+        //end of stream.
+        if (maxPos != -1 && pos >= maxPos) {
+            return -1;
+        }
         int ret = stream.read();
         if (ret >= 0)
             pos++;
@@ -150,8 +206,23 @@
      * @see java.io.InputStream#read(byte[],int,int)
      */
     public int read(byte[] b, int off, int len) throws IOException {
+        int actualLength = 0;
         updateIfRequired();
-        int retValue = super.read(b, off, len);
+        
+        //If maxPos is not invalid then
+        //ensure that the length(len) 
+        //that is requested falls within
+        //the restriction set by maxPos.
+        if(maxPos != -1) {
+            actualLength 
+                    = (int )Math.min(len, maxPos - pos);
+        }
+        else {
+            //maxPos has not been set. Make
+            //maxPos the length requested.
+            actualLength = len;
+        }
+        int retValue = super.read(b, off, actualLength);
         if (retValue > 0)
             pos += retValue;
         return retValue;
@@ -178,7 +249,22 @@
      */
     public int read(byte[] b) throws IOException {
         updateIfRequired();
-        int retValue = stream.read(b);
+        int actualLength = 0;
+        //If maxPos is not invalid
+        //then ensure that the length
+        //(len of the byte array b) 
+        //falls within the restriction 
+        //set by maxPos.
+        if(maxPos != -1) {
+            actualLength 
+                    = (int )Math.min(b.length, maxPos - pos);
+        }
+        else {
+            //maxPos has not been set. Make
+            //maxPos the length requested.
+            actualLength = b.length;
+        }
+        int retValue = stream.read(b, 0, actualLength);
         if (retValue > 0)
             pos += retValue;
         return retValue;

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?view=diff&rev=546838&r1=546837&r2=546838
==============================================================================
--- 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
Wed Jun 13 05:25:13 2007
@@ -388,12 +388,11 @@
 
     /**
      * Tests the implementation of the method
-     * getBinaryStream(long pos, long length)
-     * in the NetworkClient.
+     * getBinaryStream(long pos, long length).
      *
      * @throws Exception
      */
-    public void clientTestGetBinaryStreamLong()
+    public void testGetBinaryStreamLong()
     throws Exception {
         byte[] BYTES1 = {
             0x65, 0x66, 0x67, 0x68, 0x69,
@@ -436,7 +435,7 @@
      *
      * @throws SQLException.
      */
-    public void clientTestGetBinaryStreamLongExceptionConditions()
+    public void testGetBinaryStreamLongExceptionConditions()
     throws SQLException {
         byte[] BYTES1 = {
             0x65, 0x66, 0x67, 0x68, 0x69,
@@ -517,40 +516,13 @@
 
     
     /**
-     * Tests the getBinaryStream(long pos, long length) on the Embedded side.
-     * @throws SQLException
-     */
-    public void embeddedTestGetBinaryStringLongNotImplemented()
-    throws SQLException {
-        try {
-            blob.getBinaryStream(5l, 10l);
-            fail("Blob.getBinaryStream(long,long) should not be implemented");
-        } catch (SQLFeatureNotSupportedException sfnse) {
-            // Do nothing, we are fine
-        }
-    }
-
-    /**
      * Create test suite for this test.
      */
     public static Test suite() {
-        TestSuite btSuite = new TestSuite("BlobTest suite");
-
-        TestSuite embedded = new TestSuite("BlobTest:embedded");
-        embedded.addTestSuite(BlobTest.class);
-        embedded.addTest(new BlobTest(
-                    "embeddedTestGetBinaryStringLongNotImplemented"));
-        btSuite.addTest(new BlobClobTestSetup(embedded));
-
-        TestSuite client = new TestSuite("BlobTest:client");
-        client.addTestSuite(BlobTest.class);
-        client.addTest(new BlobTest("clientTestGetBinaryStreamLong"));
-        client.addTest(new BlobTest("clientTestGetBinaryStreamLong" +
-                "ExceptionConditions"));
-        btSuite.addTest(TestConfiguration.clientServerDecorator(
-            new BlobClobTestSetup(client)));
-
-        return btSuite;
+        return  new BlobClobTestSetup(
+                TestConfiguration.defaultSuite(
+                BlobTest.class,
+                false));
     }
 
 } // End class BlobTest



Mime
View raw message