harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject svn commit: r487245 - in /harmony/enhanced/classlib/trunk/modules/sql/src: main/java/javax/sql/rowset/serial/SerialBlob.java test/java/org/apache/harmony/sql/tests/javax/sql/rowset/serial/SerialBlobTest.java
Date Thu, 14 Dec 2006 15:54:49 GMT
Author: ayza
Date: Thu Dec 14 07:54:49 2006
New Revision: 487245

URL: http://svn.apache.org/viewvc?view=rev&rev=487245
Log:
Applying patch from HARMONY-2708 ([classlib][sql] Implementation for javax.sql.rowset.serial.SerialBlob.position(byte[],
long))

Modified:
    harmony/enhanced/classlib/trunk/modules/sql/src/main/java/javax/sql/rowset/serial/SerialBlob.java
    harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/javax/sql/rowset/serial/SerialBlobTest.java

Modified: harmony/enhanced/classlib/trunk/modules/sql/src/main/java/javax/sql/rowset/serial/SerialBlob.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sql/src/main/java/javax/sql/rowset/serial/SerialBlob.java?view=diff&rev=487245&r1=487244&r2=487245
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/main/java/javax/sql/rowset/serial/SerialBlob.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/main/java/javax/sql/rowset/serial/SerialBlob.java
Thu Dec 14 07:54:49 2006
@@ -152,9 +152,49 @@
         return position(patternBytes, start);
     }
 
+    /**
+     * Search for the position in this Blob at which the specified pattern
+     * begins, starting at a specified position within the Blob.
+     * 
+     * @param pattern
+     *            a byte array containing the pattern of data to search for in
+     *            this Blob
+     * @param start
+     *            the position within this Blob to start the search, where the
+     *            first position in the Blob is 1
+     * @return a long value with the position at which the pattern begins. -1 if
+     *         the pattern is not found in this Blob.
+     * @throws SerialException
+     *             if an error is encountered
+     * @throws SQLException
+     *             if an error occurs accessing the Blob
+     */
     public long position(byte[] pattern, long start) throws SerialException,
-            SQLException, NotImplementedException {
-        throw new NotImplementedException();
+            SQLException {
+        if (start < 1 || len - (start - 1) < pattern.length) {
+            return -1;
+        }
+
+        for (int i = (int) (start - 1); i <= (len - pattern.length); ++i) {
+            if (match(buf, i, pattern)) {
+                return i + 1;
+            }
+        }
+        return -1;
+    }
+
+    /*
+     * Returns true if the bytes array contains exactly the same elements from
+     * start position to start + subBytes.length as subBytes. Otherwise returns
+     * false. 
+     */
+    private boolean match(byte[] bytes, int start, byte[] subBytes) {
+        for (int i = 0; i < subBytes.length;) {
+            if (bytes[start++] != subBytes[i++]) {
+                return false;
+            }
+        }
+        return true;
     }
 
     public OutputStream setBinaryStream(long pos) throws SerialException,
@@ -172,7 +212,8 @@
         throw new NotImplementedException();
     }
 
-    public void truncate(long len) throws SerialException, NotImplementedException {
+    public void truncate(long len) throws SerialException,
+            NotImplementedException {
         throw new NotImplementedException();
     }
 

Modified: harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/javax/sql/rowset/serial/SerialBlobTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/javax/sql/rowset/serial/SerialBlobTest.java?view=diff&rev=487245&r1=487244&r2=487245
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/javax/sql/rowset/serial/SerialBlobTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/javax/sql/rowset/serial/SerialBlobTest.java
Thu Dec 14 07:54:49 2006
@@ -136,6 +136,144 @@
         }
     }
 
+    public void testPosition$BJ() throws Exception {
+        byte[] buf = { 1, 2, 3, 4, 5, 6, 7, 8 };
+        SerialBlob serialBlob = new SerialBlob(buf);
+
+        assertBlobPosition_BytePattern(serialBlob);
+
+        MockSerialBlob mockBlob = new MockSerialBlob();
+        mockBlob.buf = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
+        serialBlob = new SerialBlob(mockBlob);
+        assertBlobPosition_BytePattern(serialBlob);
+    }
+
+    public void testPositionLBlobJ() throws Exception {
+        byte[] buf = { 1, 2, 3, 4, 5, 6, 7, 8 };
+        SerialBlob serialBlob = new SerialBlob(buf);
+        assertBlobPosition_BlobPattern(serialBlob);
+
+        MockSerialBlob mockBlob = new MockSerialBlob();
+        mockBlob.buf = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
+        serialBlob = new SerialBlob(mockBlob);
+        assertBlobPosition_BlobPattern(serialBlob);
+    }
+
+    private void assertBlobPosition_BytePattern(Blob blob)
+            throws SerialException, SQLException {
+        byte[] pattern;
+        long pos;
+
+        pattern = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
+        pos = blob.position(pattern, 1);
+        assertEquals(1, pos);
+        
+        pattern = new byte[] { 2, 3, 4 };
+        pos = blob.position(pattern, 1);
+        assertEquals(2, pos);
+        pos = blob.position(pattern, 2);
+        assertEquals(2, pos);
+        pos = blob.position(pattern, 3);
+        assertEquals(-1, pos);
+
+        pattern = new byte[] { 2, 4 };
+        pos = blob.position(pattern, 1);
+        // RI's bug: RI doesn't returns -1 here even if the pattern can not be
+        // found
+        assertEquals(-1, pos);
+        pos = blob.position(pattern, 3);
+        assertEquals(-1, pos);
+
+        pattern = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
+        pos = blob.position(pattern, 1);
+        assertEquals(-1, pos);
+        pos = blob.position(pattern, 3);
+        assertEquals(-1, pos);
+
+        pattern = new byte[] { 2, 3, 4 };
+        pos = blob.position(pattern, 0);
+        assertEquals(-1, pos);
+        pos = blob.position(pattern, -1);
+        assertEquals(-1, pos);
+
+        // exceptional case
+        pos = blob.position((byte[]) null, -1);
+        assertEquals(-1, pos);
+        try {
+            pos = blob.position((byte[]) null, 1);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+    }
+
+    private void assertBlobPosition_BlobPattern(Blob blob)
+            throws SerialException, SQLException {
+        MockSerialBlob pattern = new MockSerialBlob();
+        long pos;
+
+        pattern.buf = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
+        pos = blob.position(pattern, 1);
+        assertEquals(1, pos);
+        
+        pattern.buf = new byte[] { 2, 3, 4 };
+        pos = blob.position(pattern, 1);
+        assertEquals(2, pos);
+        pos = blob.position(pattern, 2);
+        assertEquals(2, pos);
+        pos = blob.position(pattern, 3);
+        assertEquals(-1, pos);
+
+        pattern.buf = new byte[] { 2, 4 };
+        pos = blob.position(pattern, 1);
+        // RI's bug: RI doesn't returns -1 here even if the pattern can not be
+        // found
+        assertEquals(-1, pos);
+        pos = blob.position(pattern, 3);
+        assertEquals(-1, pos);
+
+        pattern.buf = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
+        pos = blob.position(pattern, 1);
+        assertEquals(-1, pos);
+        pos = blob.position(pattern, 3);
+        assertEquals(-1, pos);
+
+        pattern.buf = new byte[] { 2, 3, 4 };
+        pos = blob.position(pattern, 0);
+        assertEquals(-1, pos);
+        pos = blob.position(pattern, -1);
+        assertEquals(-1, pos);
+
+        // exceptional case
+        try {
+            pos = blob.position((Blob) null, -1);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+
+        try {
+            pos = blob.position((Blob) null, 1);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+
+        MockSerialBlob abnormalBlob = new MockSerialBlob(true);
+        try {
+            blob.position(abnormalBlob, 1);
+            fail("should throw SQLException");
+        } catch (SQLException e) {
+            // expected
+        }
+        try {
+            blob.position(abnormalBlob, -1);
+            fail("should throw SQLException");
+        } catch (SQLException e) {
+            // expected
+        }
+    }
+
     static class MockSerialBlob implements Blob {
         public byte buf[] = new byte[1];
 



Mime
View raw message