db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kahat...@apache.org
Subject svn commit: r691253 - in /db/derby/code/trunk/java: engine/org/apache/derby/iapi/services/io/ engine/org/apache/derby/iapi/util/ engine/org/apache/derby/impl/store/raw/data/ testing/org/apache/derbyTesting/unitTests/junit/
Date Tue, 02 Sep 2008 14:17:33 GMT
Author: kahatlen
Date: Tue Sep  2 07:17:32 2008
New Revision: 691253

URL: http://svn.apache.org/viewvc?rev=691253&view=rev
Log:
DERBY-3770: Create a utility class for skipping data in an InputStream

Moved helper methods from iapi/util to iapi/services/io and reduced
the number of calls to skipPersistent() from skipUntilEOF().

Patch contributed by Junjie Peng <pjj.ccce@gmail.com>.

Added:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/InputStreamUtilTest.java
      - copied, changed from r690974, db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/StreamUtilTest.java
Removed:
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/util/StreamUtil.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/StreamUtilTest.java
Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/InputStreamUtil.java
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/util/UTF8Util.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/InputStreamContainer.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/_Suite.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/InputStreamUtil.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/InputStreamUtil.java?rev=691253&r1=691252&r2=691253&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/InputStreamUtil.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/io/InputStreamUtil.java
Tue Sep  2 07:17:32 2008
@@ -29,6 +29,7 @@
 	a DataInputStream just to get this functionality.
 */
 public final class InputStreamUtil {
+    private static final int SKIP_FRAGMENT_SIZE = Integer.MAX_VALUE;
 
 	/**
 		Read an unsigned byte from an InputStream, throwing an EOFException
@@ -97,27 +98,86 @@
         return offset - firstOffset;
 	}
 
-
-	/**
-		Skip a number of bytes in the stream. Note that this version takes and returns
-		a long instead of the int used by skipBytes.
-
-		@exception IOException if an I/O error occurs.
-		@exception EOFException if the end of the stream is reached
-
-		@see DataInput#skipBytes
-	*/
-	public static long skipBytes(InputStream in, long n) throws IOException {
-
-		while (n > 0) {
-			//System.out.println(" skip n = " + n);
-			long delta = in.skip(n);
-			//System.out.println(" skipped = " + delta);
-			if (delta < 0)
-				throw new EOFException();
-			n -= delta;
-		}
-
-		return n;
-	}
+    /**
+     * Skips until EOF, returns number of bytes skipped.
+     * @param is
+     *      InputStream to be skipped.
+     * @return
+     *      number of bytes skipped in fact.
+     * @throws IOException
+     *      if IOException occurs. It doesn't contain EOFException.
+     * @throws NullPointerException
+     *      if the param 'is' equals null.
+     */
+    public static long skipUntilEOF(InputStream is) throws IOException {
+        if(is == null)
+            throw new NullPointerException();
+
+        long bytes = 0;
+        while(true){
+            long r = skipPersistent(is, SKIP_FRAGMENT_SIZE);
+            bytes += r;
+            if(r < SKIP_FRAGMENT_SIZE)
+                return bytes;
+        }
+    }
+
+    /**
+     * Skips requested number of bytes,
+     * throws EOFException if there is too few bytes in the stream.
+     * @param is
+     *      InputStream to be skipped.
+     * @param skippedBytes
+     *      number of bytes to skip. if skippedBytes <= zero, do nothing.
+     * @throws EOFException
+     *      if EOF meets before requested number of bytes are skipped.
+     * @throws IOException
+     *      if IOException occurs. It doesn't contain EOFException.
+     * @throws NullPointerException
+     *      if the param 'is' equals null.
+     */
+    public static void skipFully(InputStream is, long skippedBytes)
+    throws IOException {
+        if(is == null)
+            throw new NullPointerException();
+
+        if(skippedBytes <= 0)
+            return;
+
+        long bytes = skipPersistent(is, skippedBytes);
+
+        if(bytes < skippedBytes)
+            throw new EOFException();
+    }
+
+    /**
+     * Tries harder to skip the requested number of bytes.
+     * <p>
+     * Note that even if the method fails to skip the requested number of bytes,
+     * it will not throw an exception. If this happens, the caller can be sure
+     * that end-of-stream has been reached.
+     *
+     * @param in byte stream
+     * @param bytesToSkip the number of bytes to skip
+     * @return The number of bytes skipped.
+     * @throws IOException if reading from the stream fails
+     */
+    public static final long skipPersistent(InputStream in, long bytesToSkip)
+    throws IOException {
+        long skipped = 0;
+        while (skipped < bytesToSkip) {
+            long skippedNow = in.skip(bytesToSkip - skipped);
+            if (skippedNow == 0) {
+                if (in.read() == -1) {
+                    // EOF, return what we have and leave it up to caller to
+                    // decide what to do about it.
+                    break;
+                } else {
+                    skippedNow = 1; // Added to count below.
+                }
+            }
+            skipped += skippedNow;
+        }
+        return skipped;
+    }
 }

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/util/UTF8Util.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/util/UTF8Util.java?rev=691253&r1=691252&r2=691253&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/util/UTF8Util.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/util/UTF8Util.java Tue Sep  2 07:17:32
2008
@@ -27,6 +27,8 @@
 import java.io.InputStream;
 import java.io.UTFDataFormatException;
 
+import org.apache.derby.iapi.services.io.InputStreamUtil;
+
 /**
  * Utility methods for handling UTF-8 encoded byte streams.
  * <p>
@@ -118,7 +120,7 @@
                 bytesSkipped++;
             } else if ((c & 0x60) == 0x40) { // 7th bit set, 6th bit unset
                 // Found char of two byte width.
-                if (StreamUtil.skipPersistent(in, 1L) != 1L) {
+                if (InputStreamUtil.skipPersistent(in, 1L) != 1L) {
                     // No second byte present.
                     throw new UTFDataFormatException(
                         "Second byte in two byte character missing; byte pos " +
@@ -144,7 +146,7 @@
                         skipped = 2;
                     }
                 } else {
-                    skipped = (int)StreamUtil.skipPersistent(in, 2L);
+                    skipped = (int)InputStreamUtil.skipPersistent(in, 2L);
                 }
                 if (skipped != 2) {
                     // No second or third byte present

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/InputStreamContainer.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/InputStreamContainer.java?rev=691253&r1=691252&r2=691253&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/InputStreamContainer.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/InputStreamContainer.java
Tue Sep  2 07:17:32 2008
@@ -202,7 +202,7 @@
 			// no need to synchronize as each caller gets a new stream
 			is = getInputStream();
 
-			InputStreamUtil.skipBytes(is, pageOffset);
+			InputStreamUtil.skipFully(is, pageOffset);
 
 			InputStreamUtil.readFully(is, pageData, 0, pageSize);
 

Copied: db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/InputStreamUtilTest.java
(from r690974, db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/StreamUtilTest.java)
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/InputStreamUtilTest.java?p2=db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/InputStreamUtilTest.java&p1=db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/StreamUtilTest.java&r1=690974&r2=691253&rev=691253&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/StreamUtilTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/InputStreamUtilTest.java
Tue Sep  2 07:17:32 2008
@@ -1,6 +1,6 @@
 /*
 
-   Derby - Class org.apache.derbyTesting.unitTests.junit.StreamUtilTest
+   Derby - Class org.apache.derbyTesting.unitTests.junit.InputStreamUtilTest
 
    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements.  See the NOTICE file distributed with
@@ -25,31 +25,31 @@
 import java.io.IOException;
 import java.io.InputStream;
 
-import org.apache.derby.iapi.util.StreamUtil;
+import org.apache.derby.iapi.services.io.InputStreamUtil;
 import org.apache.derbyTesting.junit.BaseTestCase;
 
 import junit.framework.Test;
 import junit.framework.TestSuite;
 
 /**
- * Test case for StreamUtil.
+ * Test case for InputStreamUtil.
  */
-public class StreamUtilTest extends BaseTestCase {
+public class InputStreamUtilTest extends BaseTestCase {
 
-    public StreamUtilTest(String name) {
+    public InputStreamUtilTest(String name) {
         super(name);
     }
 
     public void testNullStream() throws IOException{
         try{
-            StreamUtil.skipFully(null);
+            InputStreamUtil.skipUntilEOF(null);
             fail("Null InputStream is accepted!");
         }catch (NullPointerException e) {
             assertTrue(true);
         }
 
         try{
-            StreamUtil.skipFully(null, 0);
+            InputStreamUtil.skipFully(null, 0);
             fail("Null InputStream is accepted!");
         }catch (NullPointerException e) {
             assertTrue(true);
@@ -62,7 +62,7 @@
         for(int i = 0; i < lengths.length; i++){
             int length = lengths[i];
             InputStream is = new ByteArrayInputStream(new byte[length]);
-            assertEquals(length, StreamUtil.skipFully(is));
+            assertEquals(length, InputStreamUtil.skipUntilEOF(is));
         }
     }
 
@@ -72,7 +72,7 @@
         for(int i = 0; i < lengths.length; i++){
             int length = lengths[i];
             InputStream is = new ByteArrayInputStream(new byte[length]);
-            assertEquals(length, StreamUtil.skipFully(is));
+            assertEquals(length, InputStreamUtil.skipUntilEOF(is));
         }
     }
 
@@ -80,27 +80,27 @@
         int length = 1024;
 
         InputStream is = new ByteArrayInputStream(new byte[length]);
-        StreamUtil.skipFully(is, length);
-        assertEquals(0, StreamUtil.skipFully(is));
+        InputStreamUtil.skipFully(is, length);
+        assertEquals(0, InputStreamUtil.skipUntilEOF(is));
 
         is = new ByteArrayInputStream(new byte[length]);
-        StreamUtil.skipFully(is, length - 1);
-        assertEquals(1, StreamUtil.skipFully(is));
+        InputStreamUtil.skipFully(is, length - 1);
+        assertEquals(1, InputStreamUtil.skipUntilEOF(is));
 
         is = new ByteArrayInputStream(new byte[length]);
         try {
-            StreamUtil.skipFully(is, length + 1);
+            InputStreamUtil.skipFully(is, length + 1);
             fail("Should have Meet EOF!");
         } catch (EOFException e) {
             assertTrue(true);
         }
-        assertEquals(0, StreamUtil.skipFully(is));
+        assertEquals(0, InputStreamUtil.skipUntilEOF(is));
     }
 
     /**
      * Returns a suite of tests.
      */
     public static Test suite() {
-        return new TestSuite(StreamUtilTest.class, "StreamUtil tests");
+        return new TestSuite(InputStreamUtilTest.class, "InputStreamUtil tests");
     }
 }

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/_Suite.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/_Suite.java?rev=691253&r1=691252&r2=691253&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/_Suite.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/_Suite.java Tue
Sep  2 07:17:32 2008
@@ -52,7 +52,7 @@
         suite.addTest(UTF8UtilTest.suite());
         suite.addTestSuite(CompressedNumberTest.class);
         suite.addTest(AssertFailureTest.suite());
-        suite.addTest(StreamUtilTest.suite());
+        suite.addTest(InputStreamUtilTest.suite());
 
         return suite;
     }



Mime
View raw message