db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From krist...@apache.org
Subject svn commit: r720517 - in /db/derby/code/trunk/java/engine/org/apache/derby: iapi/types/PositionedStream.java impl/jdbc/LOBInputStream.java impl/jdbc/PositionedStoreStream.java
Date Tue, 25 Nov 2008 15:57:30 GMT
Author: kristwaa
Date: Tue Nov 25 07:57:29 2008
New Revision: 720517

URL: http://svn.apache.org/viewvc?rev=720517&view=rev
Log:
DERBY-3935: Introduce interface for a position aware stream.
Added an interface for position aware streams, which can also reposition themselves on request.
The intended use of the interface is to allow the upper layer of the Clob code stack to treat
the different internal Clob representations in a consistent way.
It should be noted that the cost of repositioning may vary greatly for different internal
representations.
Patch file: derby-3935-1b-PositionedStream.diff (1 modification)

Added:
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/PositionedStream.java
Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/LOBInputStream.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/PositionedStoreStream.java

Added: db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/PositionedStream.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/PositionedStream.java?rev=720517&view=auto
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/PositionedStream.java (added)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/PositionedStream.java Tue
Nov 25 07:57:29 2008
@@ -0,0 +1,67 @@
+/*
+
+   Derby - Class org.apache.derby.iapi.types.PositionedStream
+
+   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.iapi.types;
+
+import java.io.IOException;
+import java.io.InputStream;
+import org.apache.derby.iapi.error.StandardException;
+
+/**
+ * This interface describes a stream that is aware of its own position and can
+ * reposition itself on request.
+ * <p>
+ * This interface doesn't convey any information about how expensive it is for
+ * the stream to reposition itself.
+ */
+public interface PositionedStream {
+
+    /**
+     * Returns a reference to self as an {@code InputStream}.
+     * <p>
+     * This method is not allowed to return {@code null}.
+     *
+     * @return An {@code InputStream} reference to self.
+     */
+    InputStream asInputStream();
+
+    /**
+     * Returns the current byte position of the stream.
+     *
+     * @return Current byte position of the stream.
+     */
+    long getPosition();
+
+    /**
+     * Repositions the stream to the requested byte position.
+     * <p>
+     * If the repositioning fails because the stream is exhausted, most likely
+     * because of an invalid position specified by the user, the stream is
+     * reset to position zero and an {@code EOFException} is thrown.
+     *
+     * @param requestedPos requested byte position, first position is {@code 0}
+     * @throws IOException if accessing the stream fails
+     * @throws EOFException if the requested position is equal to or larger
+     *      than the length of the stream
+     * @throws StandardException if an error occurs in store
+     */
+    void reposition(long requestedPos)
+            throws IOException, StandardException;
+}

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/LOBInputStream.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/LOBInputStream.java?rev=720517&r1=720516&r2=720517&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/LOBInputStream.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/LOBInputStream.java Tue Nov
25 07:57:29 2008
@@ -22,11 +22,14 @@
  */
 package org.apache.derby.impl.jdbc;
 
+import java.io.EOFException;
 import java.io.IOException;
 import java.io.InputStream;
 import org.apache.derby.iapi.error.StandardException;
 import org.apache.derby.iapi.reference.SQLState;
 import org.apache.derby.iapi.services.i18n.MessageService;
+import org.apache.derby.iapi.services.sanity.SanityManager;
+import org.apache.derby.iapi.types.PositionedStream;
 import org.apache.derby.shared.common.error.ExceptionUtil;
 
 /**
@@ -35,7 +38,9 @@
  * All the read methods are routed to {@link LOBStreamControl}.
  */
 
-public class LOBInputStream extends InputStream {
+public class LOBInputStream
+    extends InputStream
+    implements PositionedStream {
 
     private boolean closed;
     private final LOBStreamControl control;
@@ -200,4 +205,43 @@
     long length () throws IOException {
         return control.getLength();
     }
+
+    // Implementation of the PositionedStream interface:
+    //   - asInputStream
+    //   - getPosition
+    //   - reposition
+
+    public InputStream asInputStream() {
+        return this;
+    }
+
+    /**
+     * Returns the current byte position.
+     *
+     * @return The current byte position.
+     */
+    public long getPosition() {
+        return pos;
+    }
+
+    /**
+     * Repositions the stream to the requested byte position.
+     *
+     * @param requestedPos the requested position, starting at {@code 0}
+     * @throws EOFException if the requested position is larger than the length
+     * @throws IOException if obtaining the stream length fails
+     */
+    public void reposition(long requestedPos)
+            throws IOException{
+        if (SanityManager.DEBUG) {
+            if (requestedPos < 0) {
+                SanityManager.THROWASSERT("Negative position: " + requestedPos);
+            }
+        }
+        if (requestedPos > length()) {
+            pos = 0;
+            throw new EOFException();
+        }
+        pos = requestedPos;
+    }
 }

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/PositionedStoreStream.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/PositionedStoreStream.java?rev=720517&r1=720516&r2=720517&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/PositionedStoreStream.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/PositionedStoreStream.java
Tue Nov 25 07:57:29 2008
@@ -28,6 +28,8 @@
 
 import org.apache.derby.iapi.error.StandardException;
 import org.apache.derby.iapi.services.io.InputStreamUtil;
+import org.apache.derby.iapi.services.sanity.SanityManager;
+import org.apache.derby.iapi.types.PositionedStream;
 import org.apache.derby.iapi.types.Resetable;
 
 /**
@@ -56,7 +58,7 @@
 //@NotThreadSafe
 public class PositionedStoreStream
     extends InputStream
-    implements Resetable {
+    implements PositionedStream, Resetable {
 
     /** Underlying store stream serving bytes. */
     //@GuardedBy("EmbedConnection.getConnectionSynchronization()")
@@ -194,6 +196,11 @@
      */
     public void reposition(final long requestedPos)
             throws IOException, StandardException {
+        if (SanityManager.DEBUG) {
+            if (requestedPos < 0) {
+                SanityManager.THROWASSERT("Negative position: " + requestedPos);
+            }
+        }
         if (this.pos > requestedPos) {
             // Reset stream to reposition from start.
             resetStream();
@@ -221,4 +228,8 @@
         return this.pos;
     }
 
+    public InputStream asInputStream() {
+        return this;
+    }
+
 } // End class PositionedStoreStream



Mime
View raw message