1 7 8 package javax.sql.rowset.serial; 9 10 import java.sql.*; 11 import java.io.*; 12 import java.lang.reflect.*; 13 14 15 37 public class SerialBlob implements Blob, Serializable, Cloneable { 38 39 44 private byte buf[]; 45 46 50 private Blob blob; 51 52 57 private long len; 58 59 64 private long origLen; 65 66 80 public SerialBlob(byte[] b) throws SerialException , SQLException { 81 82 len = b.length; 83 buf = new byte[(int)len]; 84 for(int i = 0; i < len; i++) { 85 buf[i] = b[i]; 86 } 87 origLen = len; 88 } 89 90 91 110 public SerialBlob (Blob blob) throws SerialException , SQLException { 111 112 if (blob == null) { 113 throw new SQLException("Cannot instantiate a SerialBlob " + 114 "object with a null Blob object"); 115 } 116 117 len = blob.length(); 118 buf = blob.getBytes(1, (int)len ); 119 this.blob = blob; 120 121 origLen = len; 124 } 125 126 146 public byte[] getBytes(long pos, int length) throws SerialException { 147 if (length > len) { 148 length = (int)len; 149 } 150 151 if (pos < 1 || length - pos < 0 ) { 152 throw new SerialException ("Invalid arguments: position cannot be less that 1"); 153 } 154 155 pos--; 157 byte[] b = new byte[length]; 158 159 for (int i = 0; i < length; i++) { 160 b[i] = this.buf[(int)pos]; 161 pos++; 162 } 163 return b; 164 } 165 166 174 public long length() throws SerialException { 175 return len; 176 } 177 178 189 public java.io.InputStream getBinaryStream() throws SerialException { 190 InputStream stream = new ByteArrayInputStream(buf); 191 return (java.io.InputStream )stream; 192 } 193 194 214 public long position(byte[] pattern, long start) 215 throws SerialException , SQLException { 216 if (start < 1 || start > len) { 217 return -1; 218 } 219 220 int pos = (int)start-1; int i = 0; 222 long patlen = pattern.length; 223 224 while (pos < len) { 225 if (pattern[i] == buf[pos]) { 226 if (i + 1 == patlen) { 227 return (pos + 1) - (patlen - 1); 228 } 229 i++; pos++; } else if (pattern[i] != buf[pos]) { 231 pos++; } 233 } 234 return -1; } 236 237 257 public long position(Blob pattern, long start) 258 throws SerialException , SQLException { 259 return position(pattern.getBytes(1, (int)(pattern.length())), start); 260 } 261 262 281 public int setBytes(long pos, byte[] bytes) 282 throws SerialException , SQLException { 283 return (setBytes(pos, bytes, 0, bytes.length)); 284 } 285 286 316 public int setBytes(long pos, byte[] bytes, int offset, int length) 317 throws SerialException , SQLException { 318 319 if (offset < 0 || offset > bytes.length) { 320 throw new SerialException ("Invalid offset in byte array set"); 321 } 322 323 if (pos < 1 || pos > this.length()) { 324 throw new SerialException ("Invalid position in BLOB object set"); 325 } 326 327 if ((long)(length) > origLen) { 328 throw new SerialException ("Buffer is not sufficient to hold the value"); 329 } 330 331 if ((length + offset) > bytes.length) { 332 throw new SerialException ("Invalid OffSet. Cannot have combined offset " + 333 "and length that is greater that the Blob buffer"); 334 } 335 336 int i = 0; 337 pos--; while ( i < length || (offset + i +1) < (bytes.length-offset) ) { 339 this.buf[(int)pos + i] = bytes[offset + i ]; 340 i++; 341 } 342 return i; 343 } 344 345 364 public java.io.OutputStream setBinaryStream(long pos) 365 throws SerialException , SQLException { 366 if (this.blob.setBinaryStream(pos) != null) { 367 return this.blob.setBinaryStream(pos); 368 } else { 369 throw new SerialException ("Unsupported operation. SerialBlob cannot " + 370 "return a writable binary stream, unless instantiated with a Blob object " + 371 "that provides a setBinaryStream() implementation"); 372 } 373 } 374 375 385 public void truncate(long length) throws SerialException { 386 387 if (length > len) { 388 throw new SerialException 389 ("Length more than what can be truncated"); 390 } else if((int)length == 0) { 391 buf = new byte[0]; 392 len = length; 393 } else { 394 len = length; 395 buf = this.getBytes(1, (int)len); 396 } 397 } 398 399 400 404 405 static final long serialVersionUID = -8144641928112860441L; 406 } 407 | Popular Tags |