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 rh...@apache.org on 2007/05/04 01:17:52 UTC

svn commit: r535027 - in /db/derby/code/trunk/java/client/org/apache/derby/client: am/Blob.java am/BlobLocatorInputStream.java am/BlobLocatorOutputStream.java am/Lob.java net/NetCursor.java

Author: rhillegas
Date: Thu May  3 16:17:51 2007
New Revision: 535027

URL: http://svn.apache.org/viewvc?view=rev&rev=535027
Log:
DERBY-2496: Commit Oystein's blob-followup_v2.diff patch.

Modified:
    db/derby/code/trunk/java/client/org/apache/derby/client/am/Blob.java
    db/derby/code/trunk/java/client/org/apache/derby/client/am/BlobLocatorInputStream.java
    db/derby/code/trunk/java/client/org/apache/derby/client/am/BlobLocatorOutputStream.java
    db/derby/code/trunk/java/client/org/apache/derby/client/am/Lob.java
    db/derby/code/trunk/java/client/org/apache/derby/client/net/NetCursor.java

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/Blob.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/Blob.java?view=diff&rev=535027&r1=535026&r2=535027
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/Blob.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/Blob.java Thu May  3 16:17:51 2007
@@ -644,19 +644,38 @@
         //call checkValidity to exit by throwing a SQLException if
         //the Blob object has been freed by calling free() on it
         checkValidity();
-        synchronized (agent_.connection_) {
-            if (agent_.loggingEnabled()) {
-                agent_.logWriter_.traceEntry(this, "getBinaryStream",
-                    (int) pos, length);
+        try {
+            synchronized (agent_.connection_) {
+                if (agent_.loggingEnabled()) {
+                    agent_.logWriter_.traceEntry(this, "getBinaryStream",
+                                                 (int) pos, length);
+                }
+                checkPosAndLength(pos, length);
+                
+                InputStream retVal;
+                if (isLocator()) {
+                    retVal = new BlobLocatorInputStream(agent_.connection_, 
+                                                        this, 
+                                                        pos, 
+                                                        length);
+                } else {  // binary string
+                    retVal = new java.io.ByteArrayInputStream
+                        (binaryString_, 
+                         (int)(dataOffset_ + pos - 1), 
+                         (int)length);
+                }
+                
+                if (agent_.loggingEnabled()) {
+                    agent_.logWriter_.traceExit(this, 
+                                                "getBinaryStream", 
+                                                retVal);
+                }
+                return retVal;
             }
-            checkPosAndLength(pos, length);
-            InputStream retVal = new java.io.ByteArrayInputStream
-                    (binaryString_, (int)(dataOffset_ + pos - 1), (int)length);
-            if (agent_.loggingEnabled()) {
-                agent_.logWriter_.traceExit(this, "getBinaryStream", retVal);
-            }
-            return retVal;
+        } catch ( SqlException se ) {
+            throw se.getSQLException();
         }
+
     }
 
     //------------------ Material layer event callback methods -------------------

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/BlobLocatorInputStream.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/BlobLocatorInputStream.java?view=diff&rev=535027&r1=535026&r2=535027
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/BlobLocatorInputStream.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/BlobLocatorInputStream.java Thu May  3 16:17:51 2007
@@ -48,96 +48,132 @@
 
     /**
      * Create an <code>InputStream</code> for reading the
-   * <code>Blob</code> value represented by the given locator based
+     * <code>Blob</code> value represented by the given locator based
      * <code>Blob</code> object.
      * @param connection connection to be used to read the
      *        <code>Blob</code> value from the server
      * @param blob <code>Blob</code> object that contains locator for
-   *        the <code>Blob</code> value on the server.
+     *        the <code>Blob</code> value on the server.
+     * @throws SqlException if an error occurs when obtaining the
+     *         length of the <code>Blob</code>.
      */
     public BlobLocatorInputStream(Connection connection, Blob blob) 
+        throws SqlException
     {        
         if (SanityManager.DEBUG) {
         	SanityManager.ASSERT(blob.isLocator());
         }
-         this.connection = connection;
+        this.connection = connection;
         this.blob = blob;
+        this.currentPos = 1;
+        this.maxPos = blob.sqlLength();
     }
     
     /**
-   * @see java.io.InputStream#read()
+     * Create an <code>InputStream</code> for reading the
+     * <code>Blob</code> value represented by the given locator based
+     * <code>Blob</code> object.
+     * @param connection connection to be used to read the
+     *        <code>Blob</code> value from the server
+     * @param blob <code>Blob</code> object that contains locator for
+     *        the <code>Blob</code> value on the server.
+     * @param offset the offset in the <code>Blob</code> of the first
+     *        byte to read.  
+     * @param length the maximum number of bytes to read from
+     *        the <code>Blob</code>.
+     * @throws SqlException if an error occurs when obtaining the
+     *         length of the <code>Blob</code>.
+     */
+    public BlobLocatorInputStream(Connection connection, Blob blob,
+                                  long position, long length) 
+        throws SqlException
+    {        
+        SanityManager.ASSERT(blob.isLocator());
+        this.connection = connection;
+        this.blob = blob;
+        this.currentPos = position;
+        this.maxPos = Math.min(blob.sqlLength(), position + length - 1);
+    }
+    
+    /**
+     * @see java.io.InputStream#read()
      *
      * This method fetches one byte at a time from the server. For more 
      * efficient retrieval, use #read(byte[]).
      */    
-  public int read() throws IOException
+    public int read() throws IOException
     {
         byte[] bytes = readBytes(1);
         if (bytes.length == 0) { // EOF
             return -1;
-      } else {
+        } else {
             return bytes[0];
         }
     }
     
-  /**
+    /**
      * @see java.io.InputStream#read(byte[], int, int)
      */
     public int read(byte[] b, int off, int len) throws IOException 
     {
-      if (len == 0) return 0;
+        if (len == 0) return 0;
         if ((off < 0) || (len < 0) || (off+len > b.length)) {
             throw new IndexOutOfBoundsException();
         }
         
-      byte[] bytes = readBytes(len);
+        byte[] bytes = readBytes(len);
         if (bytes.length == 0) { // EOF
             return -1;
         } else {
             System.arraycopy(bytes, 0, b, off, bytes.length);
-          return bytes.length;
+            return bytes.length;
         }
     }
 
     /**
-   * Read the next <code>len</code> bytes of the <code>Blob</code>
+     * Read the next <code>len</code> bytes of the <code>Blob</code>
      * value from the server.
      * 
      * @param len number of bytes to read
      * @throws java.io.IOException Wrapped SqlException if reading
-   *         from server fails.
+     *         from server fails.
      * @return <code>byte[]</code> containing the read bytes
      */
     private byte[] readBytes(int len) throws IOException
     {
-      try {
+        try {
             int actualLength 
-                = (int )Math.min(len, blob.sqlLength() - currentPos + 1);
+                = (int )Math.min(len, maxPos - currentPos + 1);
             byte[] result = connection.locatorProcedureCall()
                 .blobGetBytes(blob.getLocator(), currentPos, actualLength);
-          currentPos += result.length;
+            currentPos += result.length;
             return result;       
         } catch (SqlException ex) {
             IOException ioEx = new IOException();
             ioEx.initCause(ex);
-          throw ioEx;
+            throw ioEx;
         }
     }
     
     
-  /**
+    /**
      * Connection used to read Blob from server.
      */
     private Connection connection;
     
-  /**
+    /**
      * The Blob to be accessed.
      */
     private Blob blob;
 
-  /**
+    /**
      * Current position in the underlying Blob.
      * Blobs are indexed from 1
      */
-    private long currentPos = 1;
+    private long currentPos;
+
+    /**
+     * Position in Blob where to stop reading.
+     */
+    private long maxPos;
 }

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/BlobLocatorOutputStream.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/BlobLocatorOutputStream.java?view=diff&rev=535027&r1=535026&r2=535027
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/BlobLocatorOutputStream.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/BlobLocatorOutputStream.java Thu May  3 16:17:51 2007
@@ -26,9 +26,9 @@
  * An <code>OutputStream</code> that will use an locator to write
  * bytes to the Blob value on the server.
  * <p>
- * Closing a <code>ByteArrayInputStream</code> has no effect. The methods in
- * this class can be called after the stream has been closed without
- * generating an <code>IOException</code>.
+ * Closing a <code>BlobLocatorOutputStream</code> has no effect. The
+ * methods in this class can be called after the stream has been
+ * closed without generating an <code>IOException</code>.
  * <p>
  * This <code>OutputStream</code> implementation is pretty basic.  No
  * buffering of data is done.  Hence, for efficieny #write(byte[])
@@ -53,7 +53,7 @@
     {
         if (pos-1 > blob.sqlLength()) {
             throw new IndexOutOfBoundsException();
-       }
+        }
         
         this.connection = connection;
         this.blob = blob;
@@ -86,21 +86,21 @@
      * @see java.io.OutputStream#write(byte[], int, int)
      */
     public void write(byte[] b, int off, int len) throws IOException 
-   {
-         if (len == 0) return;
-         if ((off < 0) || (off > b.length) || (len < 0) || 
-                 (off+len > b.length) || (off+len < 0)) {
-             throw new IndexOutOfBoundsException();
-         } 
-         
-         byte[] ba = b;
-         if ((off > 0) || (len < b.length)) { // Copy the part we will use
-             ba = new byte[len];
-             System.arraycopy(b, off, ba, 0, len);
+    {
+        if (len == 0) return;
+        if ((off < 0) || (off > b.length) || (len < 0) || 
+            (off+len > b.length) || (off+len < 0)) {
+            throw new IndexOutOfBoundsException();
+        } 
+        
+        byte[] ba = b;
+        if ((off > 0) || (len < b.length)) { // Copy the part we will use
+            ba = new byte[len];
+            System.arraycopy(b, off, ba, 0, len);
         }
-         writeBytes(ba);
+        writeBytes(ba);
     }
- 
+
     /**
      * Write the <code>byte[]</code> to the <code>Blob</code> value on
      * the server; starting from the current position of this stream.
@@ -108,7 +108,7 @@
      * @param b The byte array containing the bytes to be written
      * @throws java.io.IOException Wrapped SqlException if writing
      *         to server fails.
-    */
+     */
     private void writeBytes(byte[] b) throws IOException
     {
         try {         
@@ -119,7 +119,7 @@
                 // Wrote past the old end of the Blob value, update length
                 blob.setSqlLength(currentPos - 1);
             }
-       } catch (SqlException ex) {
+        } catch (SqlException ex) {
             IOException ioEx= new IOException();
             ioEx.initCause(ex);
             throw ioEx;
@@ -130,7 +130,7 @@
      * Connection used to read Blob from server.
      */
     private Connection connection;
-   
+
     /**
      * The Blob to be accessed.
      */
@@ -141,5 +141,5 @@
      * Blobs are indexed from 1
      */
     private long currentPos;
- 
+
 }

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/Lob.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/Lob.java?view=diff&rev=535027&r1=535026&r2=535027
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/Lob.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/Lob.java Thu May  3 16:17:51 2007
@@ -42,12 +42,13 @@
     public static final int BINARY_STRING = 64;
     public static final int LOCATOR = 128;
 
+    public static final int INVALID_LOCATOR = -1;
     //---------------------navigational members-----------------------------------
     protected Agent agent_;
 
     //-----------------------------state------------------------------------------
     protected int dataType_ = 0;      // data type(s) the LOB instance currently contains
-    protected int locator_ = -1;   // locator id for this LOB, -1 if not locator
+    protected int locator_ = INVALID_LOCATOR; // locator id for this LOB
 
     private long sqlLength_;// length of the LOB value, as defined by the server
     private boolean lengthObtained_;
@@ -91,14 +92,13 @@
         if (isLocator()) {
             sqlLength_ = getLocatorLength();
             lengthObtained_ = true;
-        }
-        
-        if (willBeLayerBStreamed()) {
+        } else if (willBeLayerBStreamed()) {
             throw new SqlException(agent_.logWriter_,
                                    LOB_OBJECT_LENGTH_UNKNOWN_YET);
+        } else {
+            materializeStream();  // Will set sqlLength_
         }
 
-        materializeStream();  // Will set sqlLength_
         return sqlLength_;
     }
 
@@ -281,7 +281,8 @@
 
     /**
      * Get locator for this Lob
-     * @return locator for this Lob, -1 is Lob is not based on locator
+     * @return locator for this Lob, INVALID_LOCATOR if Lob is not
+     *         based on locator
      */
     public int getLocator() {
         return locator_;

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/net/NetCursor.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/net/NetCursor.java?view=diff&rev=535027&r1=535026&r2=535027
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/net/NetCursor.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/net/NetCursor.java Thu May  3 16:17:51 2007
@@ -25,6 +25,7 @@
 import org.apache.derby.client.am.Blob;
 import org.apache.derby.client.am.Clob;
 import org.apache.derby.client.am.DisconnectException;
+import org.apache.derby.client.am.Lob;
 import org.apache.derby.client.am.SignedBinary;
 import org.apache.derby.client.am.SqlException;
 import org.apache.derby.client.am.ClientMessageId;
@@ -641,7 +642,8 @@
         int currentPosition = 0;
 
         for (int i = 0; i < columns_; i++) {
-            if ((isNonTrivialDataLob(i)) && (locator(i + 1) == -1))
+            if ((isNonTrivialDataLob(i)) 
+                && (locator(i + 1) == Lob.INVALID_LOCATOR))
             // key = column position, data = index to corresponding data in extdtaData_
             // ASSERT: the server always returns the EXTDTA objects in ascending order
             {
@@ -1051,14 +1053,15 @@
     /**
      * Get locator for LOB of the designated column
      * @param column column number, starts at 1
-     * @return locator value, -1 if LOB value was sent instead of locator
+     * @return locator value, <code>Lob.INVALID_LOCATOR</code> if LOB
+     *         value was sent instead of locator
      */
     private int locator(int column)
     {
         int locator = get_INTEGER(column);
         // If Lob value was sent instead of locator, highest bit will be set
         if ((locator & 0x8000) == 0x8000) { 
-            return -1;
+            return Lob.INVALID_LOCATOR;
         } else {
             return locator;
         }



Re: svn commit: r535027 - in /db/derby/code/trunk/java/client/org/apache/derby/client: am/Blob.java am/BlobLocatorInputStream.java am/BlobLocatorOutputStream.java am/Lob.java net/NetCursor.java

Posted by Myrna van Lunteren <m....@gmail.com>.
On 5/3/07, rhillegas@apache.org <rh...@apache.org> wrote:
> Author: rhillegas
> Date: Thu May  3 16:17:51 2007
> New Revision: 535027
>
> URL: http://svn.apache.org/viewvc?view=rev&rev=535027
> Log:
> DERBY-2496: Commit Oystein's blob-followup_v2.diff patch.
>
> Modified:
...
db/derby/code/trunk/java/client/org/apache/derby/client/am/BlobLocatorInputStream.java
...
This change caused one new javadoc error and one sanity build error
*** Failure
      [javadoc]
trunk\java\client\org\apache\derby\client\am\BlobLocatorInputStream.java:87:
warning - @param argument "offset" is not a parameter name.
      [javadoc] 1 warning

*** Failure\buildZip.txt
         [java] SANITY >>>
/org/apache/derby/client/am/BlobLocatorInputStream.class

Re the second, a SanityManager.ASSERT(..) statement needs to have an
if (SanityManager.DEBUG) block, like Andrew put in with revision
533876, after the previous check in for bug DERBY-2496 with revision
533268.

see: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/BlobLocatorInputStream.java?view=log

Myrna

Re: svn commit: r535027 - in /db/derby/code/trunk/java/client/org/apache/derby/client: am/Blob.java am/BlobLocatorInputStream.java am/BlobLocatorOutputStream.java am/Lob.java net/NetCursor.java

Posted by Myrna van Lunteren <m....@gmail.com>.
On 5/3/07, rhillegas@apache.org <rh...@apache.org> wrote:
> Author: rhillegas
> Date: Thu May  3 16:17:51 2007
> New Revision: 535027
>
> URL: http://svn.apache.org/viewvc?view=rev&rev=535027
> Log:
> DERBY-2496: Commit Oystein's blob-followup_v2.diff patch.
>
> Modified:
...
db/derby/code/trunk/java/client/org/apache/derby/client/am/BlobLocatorInputStream.java
...
This change caused one new javadoc error and one sanity build error
*** Failure
      [javadoc]
trunk\java\client\org\apache\derby\client\am\BlobLocatorInputStream.java:87:
warning - @param argument "offset" is not a parameter name.
      [javadoc] 1 warning

*** Failure\buildZip.txt
         [java] SANITY >>>
/org/apache/derby/client/am/BlobLocatorInputStream.class

Re the second, a SanityManager.ASSERT(..) statement needs to have an
if (SanityManager.DEBUG) block, like Andrew put in with revision
533876, after the previous check in for bug DERBY-2496 with revision
533268.

see: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/BlobLocatorInputStream.java?view=log

Myrna