changeset 150:c5309def7d4b

Improve MonetBlob implementation.
author Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
date Thu, 17 Aug 2017 18:50:31 +0200 (2017-08-17)
parents 1e49fc74dba4
children 3d5ea891bb74
files src/main/java/nl/cwi/monetdb/jdbc/MonetBlob.java
diffstat 1 files changed, 21 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- 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++)