Changeset: c5309def7d4b for monetdb-java
URL: https://dev.monetdb.org/hg/monetdb-java?cmd=changeset;node=c5309def7d4b
Modified Files:
        src/main/java/nl/cwi/monetdb/jdbc/MonetBlob.java
Branch: default
Log Message:

Improve MonetBlob implementation.


diffs (123 lines):

diff --git a/src/main/java/nl/cwi/monetdb/jdbc/MonetBlob.java 
b/src/main/java/nl/cwi/monetdb/jdbc/MonetBlob.java
--- a/src/main/java/nl/cwi/monetdb/jdbc/MonetBlob.java
+++ b/src/main/java/nl/cwi/monetdb/jdbc/MonetBlob.java
@@ -14,7 +14,6 @@ import java.io.OutputStream;
 import java.sql.Blob;
 import java.sql.SQLException;
 import java.sql.SQLFeatureNotSupportedException;
-import java.util.Arrays;
 
 /**
  * The MonetBlob class implements the {@link java.sql.Blob} interface.  Because
@@ -34,13 +33,22 @@ public class MonetBlob implements Blob {
        }
        
        static MonetBlob create(String in) {
+               // unpack the HEX (BLOB) notation to real bytes
                int len = in.length() / 2;
                byte[] buf = new byte[len];
-               for (int i = 0; i < len; i++)
-                       buf[i] = (byte)Integer.parseInt(in.substring(2 * i, (2 
* i) + 2), 16);
+               int offset;
+               for (int i = 0; i < len; i++) {
+                       offset = 2 * i;
+                       buf[i] = (byte)Integer.parseInt(in.substring(offset, 
offset + 2), 16);
+               }
                return new MonetBlob(buf);
        }
-       
+
+       /* internal utility method */
+       private void checkBufIsNotNull() throws SQLException {
+               if (buf == null)
+                       throw new SQLException("This MonetBlob has been freed", 
"M1M20");
+       }
 
        //== begin interface Blob
        
@@ -74,8 +82,7 @@ public class MonetBlob implements Blob {
         */
        @Override
        public InputStream getBinaryStream() throws SQLException {
-               if (buf == null)
-                       throw new SQLException("This Blob object has been 
freed", "M1M20");
+               checkBufIsNotNull();
                return new ByteArrayInputStream(buf);
        }
 
@@ -100,8 +107,7 @@ public class MonetBlob implements Blob {
        public InputStream getBinaryStream(long pos, long length)
                throws SQLException
        {
-               if (buf == null)
-                       throw new SQLException("This Blob object has been 
freed", "M1M20");
+               checkBufIsNotNull();
                if (pos < 1)
                        throw new SQLException("pos is less than 1", "M1M05");
                if (pos - 1 > buf.length)
@@ -127,10 +133,9 @@ public class MonetBlob implements Blob {
         */
        @Override
        public byte[] getBytes(long pos, int length) throws SQLException {
-               if (buf == null)
-                       throw new SQLException("This Blob object has been 
freed", "M1M20");
+               checkBufIsNotNull();
                try {
-                       return Arrays.copyOfRange(buf, (int) pos - 1, (int) pos 
- 1 + length);
+                       return java.util.Arrays.copyOfRange(buf, (int) pos - 1, 
(int) pos - 1 + length);
                } catch (IndexOutOfBoundsException e) {
                        throw new SQLException(e.getMessage(), "M0M10");
                }
@@ -146,8 +151,7 @@ public class MonetBlob implements Blob {
         */
        @Override
        public long length() throws SQLException {
-               if (buf == null)
-                       throw new SQLException("This Blob object has been 
freed", "M1M20");
+               checkBufIsNotNull();
                return (long)buf.length;
        }
 
@@ -183,8 +187,7 @@ public class MonetBlob implements Blob {
         */
        @Override
        public long position(byte[] pattern, long start) throws SQLException {
-               if (buf == null)
-                       throw new SQLException("This Blob object has been 
freed", "M1M20");
+               checkBufIsNotNull();
                try {
                        for (int i = (int)(start - 1); i < buf.length - 
pattern.length; i++) {
                                int j;
@@ -221,11 +224,7 @@ public class MonetBlob implements Blob {
         */
        @Override
        public OutputStream setBinaryStream(long pos) throws SQLException {
-               if (buf == null)
-                       throw new SQLException("This Blob object has been 
freed", "M1M20");
-               if (pos < 1)
-                       throw new SQLException("pos is less than 1", "M1M05");
-               throw new SQLFeatureNotSupportedException("Operation 
setBinaryStream(long pos) currently not supported", "0A000");
+               throw new SQLFeatureNotSupportedException("Method 
setBinaryStream(long pos) not supported", "0A000");
        }
 
        /**
@@ -268,8 +267,7 @@ public class MonetBlob implements Blob {
        public int setBytes(long pos, byte[] bytes, int offset, int len)
                throws SQLException
        {
-               if (buf == null)
-                       throw new SQLException("This Blob object has been 
freed", "M1M20");
+               checkBufIsNotNull();
                try {
                        /* transactions? what are you talking about? */
                        for (int i = (int)pos; i < len; i++)
@@ -291,8 +289,7 @@ public class MonetBlob implements Blob {
         */
        @Override
        public void truncate(long len) throws SQLException {
-               if (buf == null)
-                       throw new SQLException("This Blob object has been 
freed", "M1M20");
+               checkBufIsNotNull();
                if (buf.length > len) {
                        byte[] newbuf = new byte[(int)len];
                        for (int i = 0; i < len; i++)
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to