1 21 22 package org.apache.derby.client.am; 23 24 import java.io.InputStream ; 25 import java.io.IOException ; 26 import java.io.Reader ; 27 import java.sql.SQLException ; 28 import org.apache.derby.shared.common.reference.SQLState; 29 import org.apache.derby.client.net.EncodedInputStream; 30 31 public class Clob extends Lob implements java.sql.Clob { 32 34 protected String string_ = null; 36 37 protected java.io.InputStream asciiStream_ = null; 40 protected java.io.InputStream unicodeStream_ = null; 41 protected java.io.Reader characterStream_ = null; 42 43 protected byte[] utf8String_; 46 47 protected long lengthInBytes_ = 0; 49 50 private PreparedStatement internalLengthStmt_ = null; 51 52 protected String encoding_ = "UNICODE"; 53 54 private boolean isValid = true; 57 58 public Clob(Agent agent, String string) { 60 this(agent); 61 string_ = string; 62 sqlLength_ = string_.length(); 63 lengthObtained_ = true; 64 dataType_ |= STRING; 65 } 66 67 public Clob(Agent agent, 69 byte[] unconvertedBytes, 70 String charsetName, 71 int dataOffset) throws SqlException { 72 this(agent); 73 try { 74 if (charsetName == null) { 80 throw new SqlException(agent.logWriter_, 81 new ClientMessageId(SQLState.CHARACTER_CONVERTER_NOT_AVAILABLE)); 82 } 83 84 string_ = new String (unconvertedBytes, 85 dataOffset, 86 unconvertedBytes.length - dataOffset, 87 charsetName); 88 sqlLength_ = string_.length(); 89 lengthObtained_ = true; 90 dataType_ |= STRING; 91 } catch (java.io.UnsupportedEncodingException e) { 92 throw new SqlException(agent_.logWriter_, 93 new ClientMessageId(SQLState.UNSUPPORTED_ENCODING), 94 "byte[]", charsetName + " String", e); 95 96 } 97 } 98 99 public Clob(Agent agent, 102 java.io.InputStream inputStream, 103 String encoding, 104 int length) throws SqlException { 105 this(agent); 106 107 sqlLength_ = length; 108 lengthObtained_ = true; 109 110 if (encoding.equals("US-ASCII")) { 111 asciiStream_ = inputStream; 112 dataType_ |= ASCII_STREAM; 113 } else if (encoding.equals("UTF-8")) { unicodeStream_ = inputStream; 115 dataType_ |= UNICODE_STREAM; 116 } else if (encoding.equals("UnicodeBigUnmarked")) { try { 118 characterStream_ = 119 new java.io.InputStreamReader (inputStream, "UnicodeBigUnmarked"); 120 } catch (java.io.UnsupportedEncodingException e) { 121 throw new SqlException(agent_.logWriter_, 122 new ClientMessageId(SQLState.UNSUPPORTED_ENCODING), 123 "UnicodeBigUnmarked", "InputStreamReader", e); 124 } 125 dataType_ |= CHARACTER_STREAM; 126 sqlLength_ = length / 2; 127 } 128 } 129 130 143 public Clob(Agent agent, java.io.InputStream inputStream, String encoding) 144 throws SqlException { 145 this(agent); 146 147 lengthObtained_ = false; 148 149 if (encoding.equals("US-ASCII")) { 150 asciiStream_ = inputStream; 151 dataType_ |= ASCII_STREAM; 152 } else { 153 throw new SqlException(agent_.logWriter_, 154 new ClientMessageId(SQLState.UNSUPPORTED_ENCODING), 155 encoding + " InputStream", "String/Clob"); 156 } 157 } 158 159 public Clob(Agent agent, java.io.Reader reader, int length) { 162 this(agent); 163 sqlLength_ = length; 164 lengthObtained_ = true; 165 characterStream_ = reader; 166 dataType_ |= CHARACTER_STREAM; 167 } 168 169 179 public Clob(Agent agent, Reader reader) { 180 this(agent); 181 lengthObtained_ = false; 182 unicodeStream_ = EncodedInputStream.createUTF8Stream(reader); 184 dataType_ |= UNICODE_STREAM; 186 } 187 188 private Clob(Agent agent) { 189 super(agent); 190 } 191 192 protected void finalize() throws java.lang.Throwable { 193 super.finalize(); 194 if (internalLengthStmt_ != null) { 195 internalLengthStmt_.closeX(); 196 } 197 } 198 199 public long length() throws SQLException { 202 203 checkValidity(); 206 207 try 208 { 209 synchronized (agent_.connection_) { 210 if (agent_.loggingEnabled()) { 211 agent_.logWriter_.traceEntry(this, "length"); 212 } 213 214 if (lengthObtained_) { 215 return sqlLength_; 216 } 217 materializeStream(); 218 lengthInBytes_ = super.sqlLength(); 219 220 if (agent_.loggingEnabled()) { 221 agent_.logWriter_.traceExit(this, "length", sqlLength_); 222 } 223 return sqlLength_; 224 } 225 } 226 catch ( SqlException se ) 227 { 228 throw se.getSQLException(); 229 } 230 } 231 232 254 public String getSubString(long pos, int length) throws SQLException { 255 256 checkValidity(); 259 260 try 261 { 262 synchronized (agent_.connection_) { 263 String retVal = null; 264 265 if (agent_.loggingEnabled()) { 266 agent_.logWriter_.traceEntry(this, "getSubString", (int) pos, length); 267 } 268 269 if ( pos <= 0 ) { 270 throw new SqlException(agent_.logWriter_, 271 new ClientMessageId(SQLState.BLOB_BAD_POSITION), 272 new Long (pos)); 273 } 274 275 if ( length < 0 ) { 276 throw new SqlException(agent_.logWriter_, 277 new ClientMessageId(SQLState.BLOB_NONPOSITIVE_LENGTH), 278 new Integer (length)); 279 } 280 281 if (pos > this.length() + 1) { 282 throw new SqlException(agent_.logWriter_, 283 new ClientMessageId(SQLState.BLOB_POSITION_TOO_LARGE), 284 new Long (pos)); 285 } 286 retVal = getSubStringX(pos, length); 287 288 if (agent_.loggingEnabled()) { 289 agent_.logWriter_.traceExit(this, "getSubString", retVal); 290 } 291 return retVal; 292 } 293 } 294 catch ( SqlException se ) 295 { 296 throw se.getSQLException(); 297 } 298 } 299 300 private String getSubStringX(long pos, int length) throws SqlException { 301 try 302 { 303 checkForClosedConnection(); 304 long actualLength = Math.min(this.length() - pos + 1, (long) length); 307 return string_.substring((int) pos - 1, (int) (pos - 1 + actualLength)); 308 } 309 catch ( SQLException se ) 310 { 311 throw new SqlException(se); 312 } 313 } 314 315 public java.io.Reader getCharacterStream() throws SQLException { 316 317 checkValidity(); 320 321 try 322 { 323 synchronized (agent_.connection_) { 324 if (agent_.loggingEnabled()) { 325 agent_.logWriter_.traceEntry(this, "getCharacterStream"); 326 } 327 328 java.io.Reader retVal = getCharacterStreamX(); 329 if (agent_.loggingEnabled()) { 330 agent_.logWriter_.traceExit(this, "getCharacterStream", retVal); 331 } 332 return retVal; 333 } 334 } 335 catch ( SqlException se ) 336 { 337 throw se.getSQLException(); 338 } 339 } 340 341 private java.io.Reader getCharacterStreamX() throws SqlException { 342 checkForClosedConnection(); 343 344 if (isCharacterStream()) { 346 return characterStream_; 347 } 348 349 return new java.io.StringReader (string_); 350 } 351 352 public java.io.InputStream getAsciiStream() throws SQLException { 353 354 checkValidity(); 357 358 try 359 { 360 synchronized (agent_.connection_) { 361 if (agent_.loggingEnabled()) { 362 agent_.logWriter_.traceEntry(this, "getAsciiStream"); 363 } 364 365 java.io.InputStream retVal = getAsciiStreamX(); 366 if (agent_.loggingEnabled()) { 367 agent_.logWriter_.traceExit(this, "getAsciiStream", retVal); 368 } 369 return retVal; 370 } 371 } 372 catch ( SqlException se ) 373 { 374 throw se.getSQLException(); 375 } 376 } 377 378 private java.io.InputStream getAsciiStreamX() throws SqlException { 379 checkForClosedConnection(); 380 381 if (isAsciiStream()) { 383 return asciiStream_; 384 } 385 386 return new AsciiStream(string_, new java.io.StringReader (string_)); 387 } 388 389 public long position(String searchstr, long start) throws SQLException { 390 391 checkValidity(); 394 395 try 396 { 397 synchronized (agent_.connection_) { 398 if (agent_.loggingEnabled()) { 399 agent_.logWriter_.traceEntry(this, 400 "position(String, long)", 401 searchstr, 402 start); 403 } 404 if (searchstr == null) { 405 throw new SqlException(agent_.logWriter_, 406 new ClientMessageId(SQLState.BLOB_NULL_PATTERN_OR_SEARCH_STR)); 407 } 408 if (start < 1) { 409 throw new SqlException(agent_.logWriter_, 410 new ClientMessageId(SQLState.BLOB_BAD_POSITION), 411 new Long (start)); 412 } 413 414 long pos = positionX(searchstr, start); 415 if (agent_.loggingEnabled()) { 416 agent_.logWriter_.traceExit(this, "position(String, long)", pos); 417 } 418 return pos; 419 } 420 } 421 catch ( SqlException se ) 422 { 423 throw se.getSQLException(); 424 } 425 } 426 427 private long positionX(String searchstr, long start) throws SqlException { 428 checkForClosedConnection(); 429 430 431 if (start <= 0) { 432 throw new SqlException(agent_.logWriter_, 433 new ClientMessageId(SQLState.INVALID_API_PARAMETER), 434 new Long (start), "start", "Clob.position()"); 435 } 436 437 int index = string_.indexOf(searchstr, (int) start - 1); 438 if (index != -1) { 439 index++; } 441 return (long) index; 442 } 443 444 public long position(java.sql.Clob searchstr, long start) throws SQLException { 445 446 checkValidity(); 449 450 try 451 { 452 synchronized (agent_.connection_) { 453 if (agent_.loggingEnabled()) { 454 agent_.logWriter_.traceEntry(this, 455 "position(Clob, long)", 456 searchstr, 457 start); 458 } 459 if (start < 1) { 460 throw new SqlException(agent_.logWriter_, 461 new ClientMessageId(SQLState.BLOB_BAD_POSITION), 462 new Long (start)); 463 } 464 465 if (searchstr == null) { 466 throw new SqlException(agent_.logWriter_, 467 new ClientMessageId(SQLState.BLOB_NULL_PATTERN_OR_SEARCH_STR)); 468 } 469 long pos = positionX(searchstr, start); 470 if (agent_.loggingEnabled()) { 471 agent_.logWriter_.traceExit(this, "position(Clob, long)", pos); 472 } 473 return pos; 474 } 475 } 476 catch ( SqlException se ) 477 { 478 throw se.getSQLException(); 479 } 480 } 481 482 private long positionX(java.sql.Clob searchstr, long start) throws SqlException { 483 checkForClosedConnection(); 484 485 if (start <= 0) { 486 throw new SqlException(agent_.logWriter_, 487 new ClientMessageId(SQLState.INVALID_API_PARAMETER), 488 new Long (start), "start", "Clob.position()"); 489 } 490 491 int index; 493 try { 494 if (searchstr.length() > length()) { 495 return -1; 496 } 497 498 index = string_.indexOf(searchstr.getSubString(1L, (int) searchstr.length()), (int) start - 1); 499 } catch (java.sql.SQLException e) { 500 throw new SqlException(e); 501 } 502 if (index != -1) { 503 index++; } 505 return (long) index; 506 } 507 508 510 public int setString(long pos, String str) throws SQLException { 511 512 checkValidity(); 515 516 try 517 { 518 synchronized (agent_.connection_) { 519 if (agent_.loggingEnabled()) { 520 agent_.logWriter_.traceEntry(this, "setString", (int) pos, str); 521 } 522 int length = setStringX(pos, str, 0, str.length()); 523 if (agent_.loggingEnabled()) { 524 agent_.logWriter_.traceExit(this, "setString", length); 525 } 526 return length; 527 } 528 } 529 catch ( SqlException se ) 530 { 531 throw se.getSQLException(); 532 } 533 } 534 535 public int setString(long pos, String str, int offset, int len) throws SQLException { 536 537 checkValidity(); 540 541 try 542 { 543 synchronized (agent_.connection_) { 544 if (agent_.loggingEnabled()) { 545 agent_.logWriter_.traceEntry(this, "setString", (int) pos, str, offset, len); 546 } 547 int length = setStringX(pos, str, offset, len); 548 if (agent_.loggingEnabled()) { 549 agent_.logWriter_.traceExit(this, "setString", length); 550 } 551 return length; 552 } 553 } 554 catch ( SqlException se ) 555 { 556 throw se.getSQLException(); 557 } 558 } 559 560 public int setStringX(long pos, String str, int offset, int len) throws SqlException { 561 if ((int) pos <= 0 ) { 562 throw new SqlException(agent_.logWriter_, 563 new ClientMessageId(SQLState.BLOB_BAD_POSITION), 564 new Long (pos)); 565 } 566 if ( pos - 1 > sqlLength_) { 567 throw new SqlException(agent_.logWriter_, 568 new ClientMessageId(SQLState.BLOB_POSITION_TOO_LARGE), 569 new Long (pos)); 570 } 571 if ((offset < 0) || offset > str.length() ) { 572 throw new SqlException(agent_.logWriter_, 573 new ClientMessageId(SQLState.BLOB_INVALID_OFFSET), 574 new Integer (offset)); 575 } 576 577 if ( len < 0 ) { 578 throw new SqlException(agent_.logWriter_, 579 new ClientMessageId(SQLState.BLOB_NONPOSITIVE_LENGTH), 580 new Integer (len)); 581 } 582 583 if (len == 0) { 584 return 0; 585 } 586 587 int length = 0; 588 length = Math.min((str.length() - offset), len); 589 String newString = string_.substring(0, (int) pos - 1); 590 string_ = newString.concat(str.substring(offset, offset + length)); 591 asciiStream_ = new java.io.StringBufferInputStream (string_); 592 unicodeStream_ = new java.io.StringBufferInputStream (string_); 593 characterStream_ = new java.io.StringReader (string_); 594 sqlLength_ = string_.length(); 595 return length; 596 } 597 598 public java.io.OutputStream setAsciiStream(long pos) throws SQLException { 599 600 checkValidity(); 603 604 try 605 { 606 synchronized (agent_.connection_) { 607 if (agent_.loggingEnabled()) { 608 agent_.logWriter_.traceEntry(this, "setAsciiStream", (int) pos); 609 } 610 ClobOutputStream outStream = new ClobOutputStream(this, pos); 611 612 if (agent_.loggingEnabled()) { 613 agent_.logWriter_.traceExit(this, "setAsciiStream", outStream); 614 } 615 return outStream; 616 } 617 } 618 catch ( SqlException se ) 619 { 620 throw se.getSQLException(); 621 } 622 } 623 624 public java.io.Writer setCharacterStream(long pos) throws SQLException { 625 626 checkValidity(); 629 630 try 631 { 632 synchronized (agent_.connection_) { 633 if (agent_.loggingEnabled()) { 634 agent_.logWriter_.traceEntry(this, "setCharacterStream", (int) pos); 635 } 636 ClobWriter writer = new ClobWriter(this, pos); 637 638 if (agent_.loggingEnabled()) { 639 agent_.logWriter_.traceExit(this, "setCharacterStream", writer); 640 } 641 return writer; 642 } 643 } 644 catch ( SqlException se ) 645 { 646 throw se.getSQLException(); 647 } 648 } 649 650 public void truncate(long len) throws SQLException { 651 652 checkValidity(); 655 656 try 657 { 658 synchronized (agent_.connection_) { 659 if (agent_.loggingEnabled()) { 660 agent_.logWriter_.traceEntry(this, " truncate", (int) len); 661 } 662 if (len < 0 ) { 663 throw new SqlException(agent_.logWriter_, 664 new ClientMessageId(SQLState.BLOB_NONPOSITIVE_LENGTH), 665 new Long (len)); 666 } 667 668 if ( len > this.length()) { 669 throw new SqlException(agent_.logWriter_, 670 new ClientMessageId(SQLState.BLOB_LENGTH_TOO_LONG), 671 new Long (len)); 672 } 673 674 if (len == this.length()) { 675 return; 676 } 677 String newstr = string_.substring(0, (int) len); 678 string_ = newstr; 679 asciiStream_ = new java.io.StringBufferInputStream (string_); 680 unicodeStream_ = new java.io.StringBufferInputStream (string_); 681 characterStream_ = new java.io.StringReader (string_); 682 sqlLength_ = string_.length(); 683 } 684 } 685 catch ( SqlException se ) 686 { 687 throw se.getSQLException(); 688 } 689 } 690 691 701 public void free() 702 throws SQLException { 703 704 if (!isValid) return; 706 707 isValid = false; 710 711 if(isString()) { 712 string_ = null; 713 utf8String_ = null; 714 } 715 if(isAsciiStream()) { 716 try { 717 asciiStream_.close(); 718 } 719 catch(IOException ioe) { 720 throw new SqlException(null, new ClientMessageId(SQLState.IO_ERROR_UPON_LOB_FREE)).getSQLException(); 721 } 722 } 723 if(isUnicodeStream()) { 724 try { 725 unicodeStream_.close(); 726 } 727 catch(IOException ioe) { 728 throw new SqlException(null, new ClientMessageId(SQLState.IO_ERROR_UPON_LOB_FREE)).getSQLException(); 729 } 730 } 731 if(isCharacterStream()) { 732 try { 733 characterStream_.close(); 734 } 735 catch(IOException ioe) { 736 throw new SqlException(null, new ClientMessageId(SQLState.IO_ERROR_UPON_LOB_FREE)).getSQLException(); 737 } 738 } 739 740 lengthInBytes_ = 0; 741 742 if (internalLengthStmt_ != null) { 743 try { 744 internalLengthStmt_.closeX(); 745 } 746 catch(SqlException sqle) { 747 throw sqle.getSQLException(); 748 } 749 } 750 } 751 752 public Reader getCharacterStream(long pos, long length) 753 throws SQLException { 754 throw SQLExceptionFactory.notImplemented( 755 "getCharacterStream(long,long"); 756 } 757 758 760 public boolean isString() { 761 return ((dataType_ & STRING) == STRING); 762 } 763 764 public boolean isAsciiStream() { 765 return ((dataType_ & ASCII_STREAM) == ASCII_STREAM); 766 } 767 768 public boolean isCharacterStream() { 769 return ((dataType_ & CHARACTER_STREAM) == CHARACTER_STREAM); 770 } 771 772 public boolean isUnicodeStream() { 773 return ((dataType_ & UNICODE_STREAM) == UNICODE_STREAM); 774 } 775 776 public java.io.InputStream getUnicodeStream() { 777 return unicodeStream_; 778 } 779 780 public String getString() { 781 return string_; 782 } 783 784 public byte[] getUtf8String() { 785 return utf8String_; 786 } 787 788 public int getUTF8Length() throws SqlException { 791 if (utf8String_ != null) { 792 return utf8String_.length; 793 } 794 795 try { 796 utf8String_ = string_.getBytes("UTF-8"); 797 return utf8String_.length; 798 } catch (java.io.UnsupportedEncodingException e) { 799 throw new SqlException(agent_.logWriter_, 800 new ClientMessageId(SQLState.UNSUPPORTED_ENCODING), 801 "String", "UTF8 byte[]", e); 802 } 803 } 804 805 protected Clob createClobWrapper(java.sql.Clob clob) throws SqlException { 807 long length; 808 java.io.Reader rdr; 809 810 try { 811 length = clob.length(); 812 } catch (java.sql.SQLException e) { 813 throw new SqlException(e); 814 } 815 816 if (length > Integer.MAX_VALUE) { 817 throw new SqlException(agent_.logWriter_, 818 new ClientMessageId(SQLState.BLOB_TOO_LARGE_FOR_CLIENT), 819 new Long (length), new Integer (Integer.MAX_VALUE)); 820 } 821 822 try { 823 rdr = clob.getCharacterStream(); 824 } catch (java.sql.SQLException e) { 825 throw SqlException.javaException(agent_.logWriter_, e); 826 } 827 828 return new Clob(this.agent_, rdr, (int) length); 829 } 830 831 public void convertFromAsciiToCharacterStream() throws SqlException { 832 try { 833 characterStream_ = 834 new java.io.InputStreamReader (asciiStream_, "US-ASCII"); 835 dataType_ = CHARACTER_STREAM; 836 } catch (java.io.UnsupportedEncodingException e) { 837 throw new SqlException(agent_.logWriter_, 838 new ClientMessageId(SQLState.UNSUPPORTED_ENCODING), 839 "US-ASCII", "CharacterStream", e); 840 } 841 } 842 843 public long getByteLength() throws SQLException { 846 if (lengthObtained_ == true) { 847 return lengthInBytes_; 848 } 849 850 length(); 851 return lengthInBytes_; 852 } 853 854 861 private void checkValidity() throws SQLException { 862 if(!isValid) 863 throw new SqlException(null,new ClientMessageId(SQLState.LOB_OBJECT_INVALID)) 864 .getSQLException(); 865 } 866 867 870 private void materializeStream() 871 throws SqlException { 872 unicodeStream_ = super.materializeStream(isAsciiStream() ? 873 asciiStream_ : 874 unicodeStream_, 875 "java.sql.Clob"); 876 dataType_ = UNICODE_STREAM; 877 } 878 } 879 | Popular Tags |