db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From krist...@apache.org
Subject svn commit: r547704 - in /db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc: AutoPositioningStream.java BinaryToRawStream.java EmbedBlob.java UpdateableBlobStream.java
Date Fri, 15 Jun 2007 15:05:10 GMT
Author: kristwaa
Date: Fri Jun 15 08:05:09 2007
New Revision: 547704

URL: http://svn.apache.org/viewvc?view=rev&rev=547704
Log:
DERBY-2812: Accessing blob (when it's linked to DVD stream, ie large blob) corrupts stream
fetched prior to this access.
Patch file: derby-2812v3.diff

Patch contributed by Anurag Shekhar.

Added:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/AutoPositioningStream.java
  (with props)
Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/BinaryToRawStream.java
    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

Added: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/AutoPositioningStream.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/AutoPositioningStream.java?view=auto&rev=547704
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/AutoPositioningStream.java
(added)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/AutoPositioningStream.java
Fri Jun 15 08:05:09 2007
@@ -0,0 +1,164 @@
+/*
+
+   Derby - Class org.apache.derby.impl.jdbc.AutoPositioningStream
+
+   Licensed to the Apache Software Foundation (ASF) under one
+   or more contributor license agreements.  See the NOTICE file
+   distributed with this work for additional information
+   regarding copyright ownership.  The ASF licenses this file
+   to you under the Apache License, Version 2.0 (the
+   "License"); you may not use this file except in compliance
+   with the License.  You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing,
+   software distributed under the License is distributed on an
+   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+   KIND, either express or implied.  See the License for the
+   specific language governing permissions and limitations
+   under the License.
+
+ */
+
+package org.apache.derby.impl.jdbc;
+
+import org.apache.derby.iapi.error.StandardException;
+
+import java.io.EOFException;
+import java.io.IOException;
+import java.io.InputStream;
+import org.apache.derby.iapi.services.sanity.SanityManager;
+
+/**
+ * This Stream is a wrapper for PositionedStoreStream to set the position
+ * correctly before performing any operation on it. 
+ * All the read and skip methods ensure that the PositionedStoreStream
+ * is set to right position before actually performing these operations.
+ * PositionedStoreStream is accessed within synchronized block to ensure
+ * exclusive access to it.
+ *
+ * This class must be constructed while synchronizing on 
+ * ConnectionChild.getConnectionSynchronization
+ */
+final class AutoPositioningStream extends BinaryToRawStream {
+
+    /** ConnectionChild to get synchronizion object */
+    private final ConnectionChild conChild;
+    private long pos;
+    private final PositionedStoreStream positionedStream;
+
+    /**
+     * Constructs AutoPositioningStream object. This constructor must
+     * be called from block synchronized on 
+     * conChild.getConnectionSynchronization.
+     * @param conChild  ConnectionChild to get synchronization object
+     *                  before accessing PositionedStoreStream
+     * @param in        InputStream
+     * @param parent    Parent of the stream to prevent it from being
+     *                  gc.
+     * @throws IOException if an I/O error occurs
+     */
+    AutoPositioningStream(ConnectionChild conChild, 
+                InputStream in, Object parent) throws IOException {
+        //set the stream to actual data 
+        //BinaryToRawStream will skip the initial length info
+        super (in, parent);
+        if (SanityManager.DEBUG) {
+            SanityManager.ASSERT (in instanceof PositionedStoreStream,
+                    "Unexpected stream");
+        }
+        positionedStream = (PositionedStoreStream) in;
+        pos = positionedStream.getPosition();
+        this.conChild = conChild;
+    }
+
+    /**
+     * Reads a single byte from the underlying stream.
+     *
+     * @return The next byte of data, or -1 if the end of the stream is reached.
+     * @throws IOException if an I/O error occurs
+     */
+    public int read() throws IOException {
+        synchronized (conChild.getConnectionSynchronization()) {
+            try {
+                setPosition ();
+            }
+            catch (EOFException e) {
+                return -1;
+            }
+            int ret = positionedStream.read();
+            if (ret >= 0)
+                pos++;
+            return ret;
+        }
+    }
+
+    /**
+     * Reads a number of bytes from the underlying stream and stores them in the
+     * specified byte array at the specified offset.
+     *
+     * @return The actual number of bytes read, or -1 if the end of the stream
+     *      is reached.
+     * @throws IOException if an I/O error occurs
+     */
+    public int read(byte[] b, int off, int len) throws IOException {
+        synchronized (conChild.getConnectionSynchronization()) {
+            try {
+                setPosition ();
+            }
+            catch (EOFException e) {
+                return -1;
+            }
+            int ret = positionedStream.read(b, off, len);
+            if (ret > 0)
+                pos +=ret;
+            return ret;
+        }
+    }
+
+    /**
+     * Skips up to the specified number of bytes from the underlying stream.
+     *
+     * @return The actual number of bytes skipped.
+     * @throws IOException if an I/O error occurs
+     */
+    public long skip(long n) throws IOException {
+        synchronized (conChild.getConnectionSynchronization()) {
+            setPosition ();
+            long ret = positionedStream.skip(n);
+            pos += ret;
+            return ret;
+        }
+    }
+
+    /**
+     * Reads a number of bytes from the underlying stream and stores them in the
+     * specified byte array.
+     *
+     * @return The actual number of bytes read, or -1 if the end of the stream
+     *      is reached.
+     * @throws IOException if an I/O error occurs
+     */
+    public int read(byte[] b) throws IOException {
+        return read (b, 0, b.length);
+    }
+    
+    /**
+     * Checks if positionedStream's position was changed since 
+     * last used, sets the position to right position if its 
+     * changed.
+     */
+    private void setPosition () throws IOException {
+        try {
+            if (pos != positionedStream.getPosition()) {
+                positionedStream.reposition (pos);
+            }
+        }
+        catch (StandardException se) {
+            IOException ioe = new IOException (se.getMessage());
+            ioe.initCause (se);
+            throw ioe;
+        }
+    }
+}

Propchange: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/AutoPositioningStream.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/BinaryToRawStream.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/BinaryToRawStream.java?view=diff&rev=547704&r1=547703&r2=547704
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/BinaryToRawStream.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/BinaryToRawStream.java Fri
Jun 15 08:05:09 2007
@@ -32,7 +32,7 @@
 	to one that just contains the application's data.
 	Simply read and save the length information.
 */
-final class BinaryToRawStream
+class BinaryToRawStream
 extends java.io.FilterInputStream
 {
     /**

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=547704&r1=547703&r2=547704
==============================================================================
--- 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 Fri Jun 15 08:05:09
2007
@@ -174,7 +174,8 @@
              */
             if (SanityManager.DEBUG)
                 SanityManager.ASSERT(myStream instanceof Resetable);
-
+            //make myStream a position aware stream
+            myStream = new PositionedStoreStream (myStream);
             try {
                 ((Resetable) myStream).initStream();
             } catch (StandardException se) {
@@ -456,8 +457,9 @@
                     if (pushStack)
                         setupContextStack();
 
-                    setPosition(0);
-                    return new UpdateableBlobStream (this, biStream);
+                    ((Resetable)myStream).resetStream();
+                    return new UpdateableBlobStream (this, 
+                            new AutoPositioningStream (this, myStream, this));
                 }
             }
         }

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=547704&r1=547703&r2=547704
==============================================================================
--- 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 Fri
Jun 15 08:05:09 2007
@@ -212,7 +212,7 @@
             //maxPos the length requested.
             actualLength = len;
         }
-        int retValue = super.read(b, off, actualLength);
+        int retValue = stream.read(b, off, actualLength);
         if (retValue > 0)
             pos += retValue;
         return retValue;



Mime
View raw message