1 21 22 package org.continuent.sequoia.common.protocol; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.DataInputStream ; 26 import java.io.FileInputStream ; 27 import java.io.IOException ; 28 import java.io.Serializable ; 29 import java.sql.SQLException ; 30 31 import org.continuent.sequoia.common.exceptions.NotImplementedException; 32 import org.continuent.sequoia.common.exceptions.driver.DriverSQLException; 33 34 51 public class ByteArrayBlob implements java.sql.Blob , Serializable 52 { 53 private static final long serialVersionUID = -3473780865755702765L; 54 55 56 byte[] internalArray; 57 58 60 63 public long length() throws SQLException 64 { 65 checkInitialized(); 66 return internalArray.length; 67 } 68 69 72 public byte[] getBytes(long sqlPos, int length) throws SQLException 73 { 74 checkInitialized(); 75 checkSQLRangeIsSupported(sqlPos, length); 76 77 int arrayPos = (int) (sqlPos - 1); 78 return resizedByteArray(internalArray, arrayPos, Math.min(length, internalArray.length - arrayPos)); } 82 83 86 public java.io.InputStream getBinaryStream() throws SQLException 87 { 88 checkInitialized(); 89 return new ByteArrayInputStream (internalArray); 90 } 91 92 95 public long position(byte[] pattern, long sqlStart) throws SQLException 96 { 97 checkInitialized(); 98 checkSQLRangeIsSupported(sqlStart, 0); 99 100 throw new NotImplementedException("position not yet implemented"); 101 } 102 103 106 public long position(java.sql.Blob pattern, long sqlStart) 107 throws SQLException 108 { 109 checkInitialized(); 110 checkSQLRangeIsSupported(sqlStart, 0); 111 112 return position(pattern.getBytes(0, (int) pattern.length()), sqlStart); 114 } 115 116 118 121 public int setBytes(long sqlStartPos, byte[] srcArray) throws SQLException 122 { 123 return this.setBytes(sqlStartPos, srcArray, 0, srcArray.length); 124 } 125 126 129 public int setBytes(long sqlStartPos, byte[] srcArray, int srcArrayOffset, 130 int copiedLength) throws SQLException 131 { 132 checkInitialized(); 133 checkSQLRangeIsSupported(sqlStartPos, copiedLength); 134 135 int minimumLengthNeeded = (int) (sqlStartPos - 1) + copiedLength; 136 137 if (this.length() < minimumLengthNeeded) 140 internalArray = resizedByteArray(internalArray, 0, minimumLengthNeeded); 141 142 145 System.arraycopy(srcArray, srcArrayOffset, internalArray, 149 (int) (sqlStartPos - 1), copiedLength); 150 151 155 return copiedLength; 156 } 157 158 161 public java.io.OutputStream setBinaryStream(long sqlStart) 162 throws SQLException 163 { 164 checkInitialized(); 165 checkSQLRangeIsSupported(sqlStart, 0); 166 167 return new ByteArrayBlobOutputStream(this, (int) (sqlStart - 1)); 168 } 169 170 173 public void truncate(long newLen) throws SQLException 174 { 175 checkInitialized(); 176 177 if (newLen >= this.length()) 178 return; 179 180 internalArray = resizedByteArray(internalArray, 0, (int) newLen); 181 } 182 183 187 public void free() 188 { 189 internalArray = null; 190 } 191 192 194 200 public ByteArrayBlob(byte[] src) 201 { 202 this.internalArray = resizedByteArray(src, 0, src.length); 204 } 205 206 213 public ByteArrayBlob(FileInputStream file) throws IOException , 214 NotImplementedException 215 { 216 long len = file.available(); 217 if (len > Integer.MAX_VALUE) 218 throw new NotImplementedException("file too big"); 219 internalArray = new byte[(int) len]; 220 DataInputStream stream = new DataInputStream (file); 221 stream.readFully(internalArray); 222 } 223 224 226 229 byte[] getInternalByteArray() 230 { 231 return internalArray; 232 } 233 234 private void checkInitialized() throws DriverSQLException 235 { 236 if (null == internalArray) 237 throw new DriverSQLException("Blob has been freed"); 238 } 239 240 251 static void checkSQLRangeIsSupported(long sqlStart, int len) 252 throws SQLException 253 { 254 long arrayStart = sqlStart - 1; 255 if (arrayStart < 0) 256 { 257 throw new DriverSQLException("Illegal argument: start of Blob/Clob (" 258 + sqlStart + ") cannot be less than 1"); 259 } 260 if (arrayStart + len - 1 > Integer.MAX_VALUE) 261 { 262 throw new NotImplementedException("End of Blob/Clob (" 263 + (sqlStart + len - 1) + ") is too large. Blobs larger than " 264 + Integer.MAX_VALUE + " are not supported"); 265 } 266 } 267 268 276 private byte[] resizedByteArray(byte[] src, int srcStart, int newSize) 277 { 278 byte[] newArray = new byte[newSize]; 279 System.arraycopy(src, srcStart, newArray, 0, Math.min( 280 src.length - srcStart, newSize)); return newArray; 283 } 284 285 } | Popular Tags |