db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kahat...@apache.org
Subject svn commit: r688049 - in /db/derby/code/trunk/java: engine/org/apache/derby/iapi/util/ testing/org/apache/derbyTesting/unitTests/junit/
Date Fri, 22 Aug 2008 10:53:41 GMT
Author: kahatlen
Date: Fri Aug 22 03:53:40 2008
New Revision: 688049

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

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

Added:
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/util/StreamUtil.java   (with props)
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/StreamUtilTest.java
  (with props)
Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/util/UTF8Util.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/_Suite.java

Added: db/derby/code/trunk/java/engine/org/apache/derby/iapi/util/StreamUtil.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/util/StreamUtil.java?rev=688049&view=auto
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/util/StreamUtil.java (added)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/util/StreamUtil.java Fri Aug 22
03:53:40 2008
@@ -0,0 +1,118 @@
+/*
+
+   Derby - Class org.apache.derby.iapi.util.StreamUtil
+
+   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.util;
+
+import java.io.EOFException;
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * Utility methods for handling streams.
+ * It clarifies the using of skipping a stream. See DERBY-3770.
+ */
+public class StreamUtil {
+    private static final int SKIP_BUFFER_SIZE = 1024 * 1024;
+
+    /**
+     * 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 skipFully(InputStream is) throws IOException {
+        if(is == null)
+            throw new NullPointerException();
+
+        long bytes = 0;
+        long r = 0;
+        while((r = skipPersistent(is, SKIP_BUFFER_SIZE)) > 0){
+            bytes += r;
+        }
+
+        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
+     */
+    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;
+    }
+}

Propchange: db/derby/code/trunk/java/engine/org/apache/derby/iapi/util/StreamUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

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=688049&r1=688048&r2=688049&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 Fri Aug 22 03:53:40
2008
@@ -118,7 +118,7 @@
                 bytesSkipped++;
             } else if ((c & 0x60) == 0x40) { // 7th bit set, 6th bit unset
                 // Found char of two byte width.
-                if (skipPersistent(in, 1L) != 1L) {
+                if (StreamUtil.skipPersistent(in, 1L) != 1L) {
                     // No second byte present.
                     throw new UTFDataFormatException(
                         "Second byte in two byte character missing; byte pos " +
@@ -144,7 +144,7 @@
                         skipped = 2;
                     }
                 } else {
-                    skipped = (int)skipPersistent(in, 2L);
+                    skipped = (int)StreamUtil.skipPersistent(in, 2L);
                 }
                 if (skipped != 2) {
                     // No second or third byte present
@@ -165,37 +165,6 @@
     }
 
     /**
-     * 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
-     */
-    private 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;
-    }
-
-    /**
      * Helper class to hold skip counts; one for chars and one for bytes.
      */
     // @Immutable

Added: 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/StreamUtilTest.java?rev=688049&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/StreamUtilTest.java
(added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/StreamUtilTest.java
Fri Aug 22 03:53:40 2008
@@ -0,0 +1,106 @@
+/*
+
+   Derby - Class org.apache.derbyTesting.unitTests.junit.StreamUtilTest
+
+   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.derbyTesting.unitTests.junit;
+
+import java.io.ByteArrayInputStream;
+import java.io.EOFException;
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.derby.iapi.util.StreamUtil;
+import org.apache.derbyTesting.junit.BaseTestCase;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+/**
+ * Test case for StreamUtil.
+ */
+public class StreamUtilTest extends BaseTestCase {
+
+    public StreamUtilTest(String name) {
+        super(name);
+    }
+
+    public void testNullStream() throws IOException{
+        try{
+            StreamUtil.skipFully(null);
+            fail("Null InputStream is accepted!");
+        }catch (NullPointerException e) {
+            assertTrue(true);
+        }
+
+        try{
+            StreamUtil.skipFully(null, 0);
+            fail("Null InputStream is accepted!");
+        }catch (NullPointerException e) {
+            assertTrue(true);
+        }
+    }
+
+    public void testSkipUtilEOFWithOddLength() throws IOException{
+        int[] lengths = {0, 1};
+
+        for(int i = 0; i < lengths.length; i++){
+            int length = lengths[i];
+            InputStream is = new ByteArrayInputStream(new byte[length]);
+            assertEquals(length, StreamUtil.skipFully(is));
+        }
+    }
+
+    public void testSkipUtilEOF() throws IOException{
+        int[] lengths = {1024, 1024 * 1024};
+
+        for(int i = 0; i < lengths.length; i++){
+            int length = lengths[i];
+            InputStream is = new ByteArrayInputStream(new byte[length]);
+            assertEquals(length, StreamUtil.skipFully(is));
+        }
+    }
+
+    public void testSkipFully() throws IOException{
+        int length = 1024;
+
+        InputStream is = new ByteArrayInputStream(new byte[length]);
+        StreamUtil.skipFully(is, length);
+        assertEquals(0, StreamUtil.skipFully(is));
+
+        is = new ByteArrayInputStream(new byte[length]);
+        StreamUtil.skipFully(is, length - 1);
+        assertEquals(1, StreamUtil.skipFully(is));
+
+        is = new ByteArrayInputStream(new byte[length]);
+        try {
+            StreamUtil.skipFully(is, length + 1);
+            fail("Should have Meet EOF!");
+        } catch (EOFException e) {
+            assertTrue(true);
+        }
+        assertEquals(0, StreamUtil.skipFully(is));
+    }
+
+    /**
+     * Returns a suite of tests.
+     */
+    public static Test suite() {
+        return new TestSuite(StreamUtilTest.class, "StreamUtil tests");
+    }
+}

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/unitTests/junit/StreamUtilTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

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=688049&r1=688048&r2=688049&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 Fri
Aug 22 03:53:40 2008
@@ -24,7 +24,6 @@
 import java.sql.SQLException;
 
 import org.apache.derbyTesting.junit.BaseTestCase;
-import org.apache.derbyTesting.junit.SecurityManagerSetup;
 
 import junit.framework.Test;
 import junit.framework.TestSuite;
@@ -53,6 +52,7 @@
         suite.addTest(UTF8UtilTest.suite());
         suite.addTestSuite(CompressedNumberTest.class);
         suite.addTest(AssertFailureTest.suite());
+        suite.addTest(StreamUtilTest.suite());
 
         return suite;
     }



Mime
View raw message