You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2006/12/13 11:35:51 UTC

svn commit: r486587 - in /harmony/enhanced/classlib/trunk/modules/sql/src: main/java/javax/sql/rowset/serial/ main/java/org/apache/harmony/sql/internal/nls/ test/java/org/apache/harmony/sql/tests/javax/sql/rowset/serial/

Author: tellison
Date: Wed Dec 13 02:35:50 2006
New Revision: 486587

URL: http://svn.apache.org/viewvc?view=rev&rev=486587
Log:
Apply slightly modified form of HARMONY-2596 ([classlib][sql] First implementation for javax.sql.rowset.serial.SerialBlob)

Added:
    harmony/enhanced/classlib/trunk/modules/sql/src/main/java/javax/sql/rowset/serial/SerialBlob.java   (with props)
    harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/javax/sql/rowset/serial/SerialBlobTest.java   (with props)
Modified:
    harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/nls/messages.properties

Added: 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=auto&rev=486587
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/main/java/javax/sql/rowset/serial/SerialBlob.java (added)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/main/java/javax/sql/rowset/serial/SerialBlob.java Wed Dec 13 02:35:50 2006
@@ -0,0 +1,179 @@
+/*
+ *  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 javax.sql.rowset.serial;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.Serializable;
+import java.sql.Blob;
+import java.sql.SQLException;
+
+import org.apache.harmony.luni.util.NotImplementedException;
+import org.apache.harmony.sql.internal.nls.Messages;
+
+public class SerialBlob implements Blob, Serializable, Cloneable {
+
+    private static final long serialVersionUID = -8144641928112860441L;
+
+    // required by serialized form
+    private byte[] buf;
+
+    // required by serialized form
+    @SuppressWarnings("unused")
+    private Blob blob;
+
+    // required by serialized form
+    private long len;
+
+    // required by serialized form
+    @SuppressWarnings("unused")
+    private long origLen;
+
+    /**
+     * Constructs an instance by the given <code>blob</code>
+     * 
+     * @param blob
+     *            the given blob
+     * @throws SerialException
+     *             if an error is encountered during serialization
+     * @throws SQLException
+     *             if <code>blob</code> is null
+     */
+    public SerialBlob(Blob blob) throws SerialException, SQLException {
+        if (blob == null) {
+            throw new SQLException(Messages.getString("sql.13")); //$NON-NLS-1$
+        }
+        this.blob = blob;
+        buf = blob.getBytes(1, (int) blob.length());
+        len = buf.length;
+        origLen = len;
+    }
+
+    /**
+     * Constructs an instance by the given <code>buf</code>
+     * 
+     * @param buf
+     *            the given buffer
+     * @throws SerialException
+     *             if an error is encountered during serialization
+     * @throws SQLException
+     *             if a SQL error is encountered
+     */
+    public SerialBlob(byte[] buf) throws SerialException, SQLException {
+        this.buf = new byte[buf.length];
+        len = buf.length;
+        origLen = len;
+        System.arraycopy(buf, 0, this.buf, 0, (int) len);
+    }
+
+    /**
+     * Returns an input stream of this SerialObject.
+     * 
+     * @throws SerialException
+     *             if an error is encountered
+     */
+    public InputStream getBinaryStream() throws SerialException {
+        return new ByteArrayInputStream(buf);
+    }
+
+    /**
+     * Returns a copied array of this SerialObject, starting at the
+     * <code> pos </code> with the given <code> length</code> number. If
+     * <code> pos </code> + <code> length </code> - 1 is larger than the length
+     * of this SerialObject array, the <code> length </code> will be shortened
+     * to the length of array - <code>pos</code> + 1.
+     * 
+     * @param pos
+     *            the starting position of the array to be copied.
+     * @param length
+     *            the total length of bytes to be copied
+     * @throws SerialException
+     *             if an error is encountered
+     */
+    public byte[] getBytes(long pos, int length) throws SerialException {
+        if (pos < 1 || pos > len || length < 0) {
+            throw new SerialException(Messages.getString("sql.14")); //$NON-NLS-1$
+        }
+        if (length > len - pos + 1) {
+            length = (int) (len - pos + 1);
+        }
+        byte[] copiedArray = new byte[length];
+        System.arraycopy(buf, (int) pos - 1, copiedArray, 0, length);
+        return copiedArray;
+    }
+
+    /**
+     * Gets the number of bytes in this SerialBlob object.
+     * 
+     * @return an long value with the length of the SerialBlob in bytes
+     * @throws SerialException
+     *             if an error is encoutnered
+     */
+    public long length() throws SerialException {
+        return len;
+    }
+
+    /**
+     * Search for the position in this Blob at which a specified pattern begins,
+     * starting at a specified position within the Blob.
+     * 
+     * @param pattern
+     *            a Blob 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 SQLException
+     *             if an error occurs accessing the Blob
+     * @throws SerialException
+     *             if an error is encountered
+     */
+    public long position(Blob pattern, long start) throws SerialException,
+            SQLException {
+        byte[] patternBytes = pattern.getBytes(1, (int) pattern.length());
+        return position(patternBytes, start);
+    }
+
+    public long position(byte[] pattern, long start) throws SerialException,
+            SQLException, NotImplementedException {
+        throw new NotImplementedException();
+    }
+
+    public OutputStream setBinaryStream(long pos) throws SerialException,
+            SQLException, NotImplementedException {
+        throw new NotImplementedException();
+    }
+
+    public int setBytes(long pos, byte[] theBytes) throws SerialException,
+            SQLException, NotImplementedException {
+        throw new NotImplementedException();
+    }
+
+    public int setBytes(long pos, byte[] theBytes, int offset, int len)
+            throws SQLException, NotImplementedException {
+        throw new NotImplementedException();
+    }
+
+    public void truncate(long len) throws SerialException, NotImplementedException {
+        throw new NotImplementedException();
+    }
+
+}

Propchange: harmony/enhanced/classlib/trunk/modules/sql/src/main/java/javax/sql/rowset/serial/SerialBlob.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/nls/messages.properties
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/nls/messages.properties?view=diff&rev=486587&r1=486586&r2=486587
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/nls/messages.properties (original)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/main/java/org/apache/harmony/sql/internal/nls/messages.properties Wed Dec 13 02:35:50 2006
@@ -27,3 +27,5 @@
 sql.10=Cannot instantiate a SerialRef object that returns a null base type name
 sql.11=SQLException: {0}
 sql.12=Cannot serialize empty URL instance
+sql.13=Cannot instantiate a SerialBlob object with a null Blob object
+sql.14=Invalid starting position or length

Added: 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=auto&rev=486587
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/javax/sql/rowset/serial/SerialBlobTest.java (added)
+++ harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/javax/sql/rowset/serial/SerialBlobTest.java Wed Dec 13 02:35:50 2006
@@ -0,0 +1,197 @@
+/*
+ *  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.harmony.sql.tests.javax.sql.rowset.serial;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.sql.Blob;
+import java.sql.SQLException;
+import java.util.Arrays;
+
+import javax.sql.rowset.serial.SerialBlob;
+import javax.sql.rowset.serial.SerialException;
+
+import junit.framework.TestCase;
+
+public class SerialBlobTest extends TestCase {
+
+    public void testConstructorLBlob() throws Exception {
+        boolean isAbnormal = false;
+        MockSerialBlob mockBlob = new MockSerialBlob(isAbnormal);
+        SerialBlob serialBlob = new SerialBlob(mockBlob);
+        // SerialBlob constructor initiliases with the data of given blob,
+        // therefore, blob.getBytes is invoked.
+        assertTrue(mockBlob.isGetBytesInvoked);
+        assertEquals(1, serialBlob.length());
+
+        isAbnormal = true;
+        mockBlob = new MockSerialBlob(isAbnormal);
+        try {
+            new SerialBlob(mockBlob);
+            fail("should throw SQLException");
+        } catch (SQLException e) {
+            // expected
+        }
+
+        try {
+            new SerialBlob((Blob) null);
+            fail("should throw SQLException");
+        } catch (SQLException e) {
+            // expected
+        }
+    }
+
+    public void testConstructor$B() throws Exception {
+        byte[] buf = new byte[8];
+        SerialBlob serialBlob = new SerialBlob(buf);
+        assertEquals(8, serialBlob.length());
+
+        try {
+            new SerialBlob((byte[]) null);
+            fail("should throw SQLException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+    }
+
+    public void testGetBinaryStream() throws Exception {
+        byte[] buf = { 1, 2, 3, 4, 5, 6, 7, 8 };
+        SerialBlob serialBlob = new SerialBlob(buf);
+        InputStream is = serialBlob.getBinaryStream();
+        int i = 0;
+        while (true) {
+            int b = is.read();
+            if (b == -1) {
+                if (i < buf.length) {
+                    fail("returned input stream contains too few data");
+                }
+                break;
+            }
+
+            if (i > buf.length) {
+                fail("returned input stream contains too much data");
+            }
+            assertEquals(buf[i++], b);
+        }
+    }
+
+    public void testGetBytesJI() throws Exception {
+        byte[] buf = { 1, 2, 3, 4, 5, 6, 7, 8 };
+        SerialBlob serialBlob = new SerialBlob(buf);
+        byte[] data = serialBlob.getBytes(1, 1);
+        assertEquals(1, data.length);
+        assertEquals(1, data[0]);
+
+        data = serialBlob.getBytes(2, 3);
+        assertEquals(3, data.length);
+        assertEquals(2, data[0]);
+        assertEquals(3, data[1]);
+        assertEquals(4, data[2]);
+
+        data = serialBlob.getBytes(1, 10);
+        assertEquals(8, data.length);
+        assertTrue(Arrays.equals(buf, data));
+
+        try {
+            data = serialBlob.getBytes(2, -1);
+            fail("should throw SerialException");
+        } catch (SerialException e) {
+            // expected
+        }
+
+        try {
+            data = serialBlob.getBytes(0, 2);
+            fail("should throw SerialException");
+        } catch (SerialException e) {
+            // expected
+        }
+
+        try {
+            data = serialBlob.getBytes(-1, 2);
+            fail("should throw SerialException");
+        } catch (SerialException e) {
+            // expected
+        }
+
+        try {
+            data = serialBlob.getBytes(10, 11);
+            fail("should throw SerialException");
+        } catch (SerialException e) {
+            // expected
+        }
+    }
+
+    static class MockSerialBlob implements Blob {
+        public byte buf[] = new byte[1];
+
+        public boolean isGetBytesInvoked;
+
+        public boolean isAbnormal;
+
+        public MockSerialBlob() {
+
+        }
+
+        public MockSerialBlob(boolean isAbnormal) {
+            this.isAbnormal = isAbnormal;
+        }
+
+        public InputStream getBinaryStream() throws SQLException {
+            return null;
+        }
+
+        public byte[] getBytes(long pos, int length) throws SQLException {
+            isGetBytesInvoked = true;
+            if (isAbnormal) {
+                throw new SQLException();
+            }
+            return buf;
+        }
+
+        public long length() throws SQLException {
+            return buf.length;
+        }
+
+        public long position(Blob pattern, long start) throws SQLException {
+            return 0;
+        }
+
+        public long position(byte[] pattern, long start) throws SQLException {
+            return 0;
+        }
+
+        public OutputStream setBinaryStream(long pos) throws SQLException {
+            return null;
+        }
+
+        public int setBytes(long pos, byte[] theBytes) throws SQLException {
+            return 0;
+        }
+
+        public int setBytes(long pos, byte[] theBytes, int offset, int len)
+                throws SQLException {
+            return 0;
+        }
+
+        public void truncate(long len) throws SQLException {
+
+        }
+
+    }
+
+}

Propchange: harmony/enhanced/classlib/trunk/modules/sql/src/test/java/org/apache/harmony/sql/tests/javax/sql/rowset/serial/SerialBlobTest.java
------------------------------------------------------------------------------
    svn:eol-style = native