1 7 8 package javax.sql.rowset.serial; 9 10 import java.sql.*; 11 import java.io.*; 12 13 30 public class SerialClob implements Clob, Serializable, Cloneable { 31 32 39 private char buf[]; 40 41 45 private Clob clob; 46 47 53 private long len; 54 55 61 private long origLen; 62 63 77 public SerialClob(char ch[]) throws SerialException , SQLException { 78 79 83 len = ch.length; 84 buf = new char[(int)len]; 85 for (int i = 0; i < len ; i++){ 86 buf[i] = ch[i]; 87 } 88 origLen = len; 89 } 90 91 117 public SerialClob(Clob clob) throws SerialException , SQLException { 118 119 if (clob == null) { 120 throw new SQLException("Cannot instantiate a SerialClob " + 121 "object with a null Clob object"); 122 } 123 len = clob.length(); 124 this.clob = clob; 125 buf = new char[(int)len]; 126 int read = 0; 127 int offset = 0; 128 129 BufferedReader reader; 130 131 if (clob.getCharacterStream() == null || clob.getAsciiStream() == null) { 132 throw new SQLException("Invalid Clob object. Calls to getCharacterStream " + 133 "or getAsciiStream return null which cannot be serialized."); 134 } 135 136 try { 137 reader = new BufferedReader(clob.getCharacterStream()); 138 139 do { 140 read = reader.read(buf, offset, (int)(len - offset)); 141 offset += read; 142 } while (read > 0); 143 144 } catch (java.io.IOException ex) { 145 throw new SerialException ("SerialClob: " + ex.getMessage()); 146 } 147 148 origLen = len; 149 } 150 151 159 public long length() throws SerialException { 160 return len; 161 } 162 163 173 public java.io.Reader getCharacterStream() throws SerialException { 174 return (java.io.Reader ) new CharArrayReader(buf); 175 } 176 177 193 public java.io.InputStream getAsciiStream() throws SerialException , SQLException { 194 if (this.clob != null) { 195 return this.clob.getAsciiStream(); 196 } else { 197 throw new SerialException ("Unsupported operation. SerialClob cannot " + 198 "return a the CLOB value as an ascii stream, unless instantiated " + 199 "with a fully implemented Clob object."); 200 } 201 } 202 203 227 public String getSubString(long pos, int length) throws SerialException { 228 229 if (pos < 1 || pos > this.length()) { 230 throw new SerialException ("Invalid position in BLOB object set"); 231 } 232 233 if ((pos-1) + length > this.length()) { 234 throw new SerialException ("Invalid position and substring length"); 235 } 236 237 try { 238 return new String (buf, (int)pos - 1, length); 239 240 } catch (StringIndexOutOfBoundsException e) { 241 throw new SerialException ("StringIndexOutOfBoundsException: " + 242 e.getMessage()); 243 } 244 245 } 246 247 268 public long position(String searchStr, long start) 269 throws SerialException , SQLException { 270 271 if (start < 1 || start > len) { 272 return -1; 273 } 274 275 char pattern[] = searchStr.toCharArray(); 276 277 int pos = (int)start-1; 278 int i = 0; 279 long patlen = pattern.length; 280 281 while (pos < len) { 282 if (pattern[i] == buf[pos]) { 283 if (i + 1 == patlen) { 284 return (pos + 1) - (patlen - 1); 285 } 286 i++; pos++; 288 } else if (pattern[i] != buf[pos]) { 289 pos++; } 291 } 292 return -1; } 294 295 313 public long position(Clob searchStr, long start) 314 throws SerialException , SQLException { 315 316 char cPattern[] = null; 317 try { 318 java.io.Reader r = searchStr.getCharacterStream(); 319 cPattern = new char[(int)searchStr.length()]; 320 r.read(cPattern); 321 } catch (IOException e) { 322 throw new SerialException ("Error streaming Clob search data"); 323 } 324 return position(new String (cPattern), start); 325 } 326 327 345 public int setString(long pos, String str) throws SerialException { 346 return (setString(pos, str, 0, str.length())); 347 } 348 349 370 public int setString(long pos, String str, int offset, int length) 371 throws SerialException { 372 String temp = str.substring(offset); 373 char cPattern[] = temp.toCharArray(); 374 375 if (offset < 0 || offset > str.length()) { 376 throw new SerialException ("Invalid offset in byte array set"); 377 } 378 379 if (pos < 1 || pos > this.length()) { 380 throw new SerialException ("Invalid position in BLOB object set"); 381 } 382 383 if ((long)(length) > origLen) { 384 throw new SerialException ("Buffer is not sufficient to hold the value"); 385 } 386 387 if ((length + offset) > str.length()) { 388 throw new SerialException ("Invalid OffSet. Cannot have combined offset " + 390 " and length that is greater that the Blob buffer"); 391 } 392 393 int i = 0; 394 pos--; while ( i < length || (offset + i +1) < (str.length() - offset ) ) { 396 this.buf[(int)pos + i ] = cPattern[offset + i ]; 397 i++; 398 } 399 return i; 400 } 401 402 420 public java.io.OutputStream setAsciiStream(long pos) 421 throws SerialException , SQLException { 422 if (this.clob.setAsciiStream(pos) != null) { 423 return this.clob.setAsciiStream(pos); 424 } else { 425 throw new SerialException ("Unsupported operation. SerialClob cannot " + 426 "return a writable ascii stream\n unless instantiated with a Clob object " + 427 "that has a setAsciiStream() implementation"); 428 } 429 } 430 431 450 public java.io.Writer setCharacterStream(long pos) 451 throws SerialException , SQLException { 452 if (this.clob.setCharacterStream(pos) != null) { 453 return this.clob.setCharacterStream(pos); 454 } else { 455 throw new SerialException ("Unsupported operation. SerialClob cannot " + 456 "return a writable character stream\n unless instantiated with a Clob object " + 457 "that has a setCharacterStream implementation"); 458 } 459 } 460 461 474 public void truncate(long length) throws SerialException { 475 if (length > len) { 476 throw new SerialException 477 ("Length more than what can be truncated"); 478 } else { 479 len = length; 480 482 if (len == 0) { 483 buf = new char[] {}; 484 } else { 485 buf = (this.getSubString(1, (int)len)).toCharArray(); 486 } 487 488 } 489 } 490 491 492 493 497 static final long serialVersionUID = -1662519690087375313L; 498 } 499 | Popular Tags |