changeset 329:05c7989eae91

Removed duplicate code for converting a string of hex characters into a byte array, done both in MonetBlob.create() and in MonetResultSet.getBytes(). Extended MonetBlob with utitlity method: hexStrToByteArray(final String hexString) such that it can be called from both places. Also added an extra constructor MonetBlob(String hexString) which replaces the utility method MonetBlob.create(String hexString). This makes it more orthogonal with MonetClob.
author Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
date Thu, 12 Sep 2019 19:51:30 +0200 (2019-09-12)
parents 65bf0be846f3
children 98ae44c5fd56
files src/main/java/nl/cwi/monetdb/jdbc/MonetBlob.java src/main/java/nl/cwi/monetdb/jdbc/MonetResultSet.java
diffstat 2 files changed, 12 insertions(+), 13 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
@@ -30,11 +30,18 @@ import java.sql.SQLFeatureNotSupportedEx
 public final class MonetBlob implements Blob {
 	private byte[] buf;
 
+	/* constructors */
 	protected MonetBlob(final byte[] data) {
 		buf = data;
 	}
 
-	static final MonetBlob create(final String hexString) {
+	protected MonetBlob(final String hexString) {
+		buf = hexStrToByteArray(hexString);
+	}
+
+
+	/* class utility methods */
+	static final byte[] hexStrToByteArray(final String hexString) {
 		// unpack the HEX (BLOB) notation to real bytes
 		final int len = hexString.length() / 2;
 		final byte[] buf = new byte[len];
@@ -43,7 +50,7 @@ public final class MonetBlob implements 
 			buf[i] = (byte) ((Character.digit(hexString.charAt(2 * i), 16) << 4)
 					+ Character.digit(hexString.charAt((2 * i) +1), 16));
 		}
-		return new MonetBlob(buf);
+		return buf;
 	}
 
 	/* internal utility method */
--- a/src/main/java/nl/cwi/monetdb/jdbc/MonetResultSet.java
+++ b/src/main/java/nl/cwi/monetdb/jdbc/MonetResultSet.java
@@ -559,7 +559,7 @@ public class MonetResultSet
 				return null;
 			}
 			lastReadWasNull = false;
-			return MonetBlob.create(val);
+			return new MonetBlob(val);
 		} catch (IndexOutOfBoundsException e) {
 			throw newSQLInvalidColumnIndexException(columnIndex);
 		}
@@ -901,15 +901,7 @@ public class MonetResultSet
 				case Types.BINARY:
 				case Types.VARBINARY:
 				case Types.LONGVARBINARY:
-					// unpack the HEX (BLOB) notation to real bytes
-					final int len = val.length() / 2;
-					final byte[] buf = new byte[len];
-					int offset;
-					for (int j = 0; j < len; j++) {
-						offset = j * 2;
-						buf[j] = (byte)Integer.parseInt(val.substring(offset, offset + 2), 16);
-					}
-					return buf;
+					return MonetBlob.hexStrToByteArray(val);
 				default:
 					throw new SQLException("Cannot operate on " + types[columnIndex - 1] + " type", "M1M05");
 			}
@@ -1945,7 +1937,7 @@ public class MonetResultSet
 			case Types.CLOB:
 				return new MonetClob(val);
 			case Types.BLOB:
-				return MonetBlob.create(val);
+				return new MonetBlob(val);
 			case Types.DATE:
 				return getDate(columnIndex, null);
 			case Types.TIME: