Return-Path: Delivered-To: apmail-harmony-commits-archive@www.apache.org Received: (qmail 8738 invoked from network); 14 Dec 2006 15:55:36 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 14 Dec 2006 15:55:36 -0000 Received: (qmail 34876 invoked by uid 500); 14 Dec 2006 15:55:44 -0000 Delivered-To: apmail-harmony-commits-archive@harmony.apache.org Received: (qmail 34856 invoked by uid 500); 14 Dec 2006 15:55:44 -0000 Mailing-List: contact commits-help@harmony.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@harmony.apache.org Delivered-To: mailing list commits@harmony.apache.org Received: (qmail 34844 invoked by uid 99); 14 Dec 2006 15:55:44 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 14 Dec 2006 07:55:44 -0800 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 14 Dec 2006 07:55:35 -0800 Received: by eris.apache.org (Postfix, from userid 65534) id 1D61D1A981A; Thu, 14 Dec 2006 07:54:50 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@harmony.apache.org From: ayza@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20061214155450.1D61D1A981A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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];