KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > client > am > Clob


1 /*
2
3    Derby - Class org.apache.derby.client.am.Clob
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20 */

21
22 package org.apache.derby.client.am;
23
24 import java.io.InputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.Reader JavaDoc;
27 import java.sql.SQLException JavaDoc;
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 JavaDoc {
32     //---------------------navigational members-----------------------------------
33

34     //-----------------------------state------------------------------------------
35
protected String JavaDoc string_ = null;
36
37     // Only used for input purposes. For output, each getXXXStream call
38
// must generate an independent stream.
39
protected java.io.InputStream JavaDoc asciiStream_ = null;
40     protected java.io.InputStream JavaDoc unicodeStream_ = null;
41     protected java.io.Reader JavaDoc characterStream_ = null;
42
43     // used for input
44
// Therefore, we always convert a String to UTF-8 before we flow it for input
45
protected byte[] utf8String_;
46
47     // the length of the clob returned by the LENGTH function.
48
protected long lengthInBytes_ = 0;
49
50     private PreparedStatement internalLengthStmt_ = null;
51
52     protected String JavaDoc encoding_ = "UNICODE";
53     
54     //This boolean variable indicates whether the Clob object has
55
//been invalidated by calling free() on it
56
private boolean isValid = true;
57
58     //---------------------constructors/finalizer---------------------------------
59
public Clob(Agent agent, String JavaDoc string) {
60         this(agent);
61         string_ = string;
62         sqlLength_ = string_.length();
63         lengthObtained_ = true;
64         dataType_ |= STRING;
65     }
66
67     // CTOR for output, when a btc isn't available; the encoding is
68
public Clob(Agent agent,
69                 byte[] unconvertedBytes,
70                 String JavaDoc charsetName,
71                 int dataOffset) throws SqlException {
72         this(agent);
73         try {
74             // check for null encoding is needed because the net layer
75
// will no longer throw an exception if the server didn't specify
76
// a mixed or double byte ccsid (ccsid = 0). this check for null in the
77
// cursor is only required for types which can have mixed or double
78
// byte ccsids.
79
if (charsetName == null) {
80                 throw new SqlException(agent.logWriter_,
81                     new ClientMessageId(SQLState.CHARACTER_CONVERTER_NOT_AVAILABLE));
82             }
83
84             string_ = new String JavaDoc(unconvertedBytes,
85                     dataOffset,
86                     unconvertedBytes.length - dataOffset,
87                     charsetName);
88             sqlLength_ = string_.length();
89             lengthObtained_ = true;
90             dataType_ |= STRING;
91         } catch (java.io.UnsupportedEncodingException JavaDoc e) {
92             throw new SqlException(agent_.logWriter_,
93                 new ClientMessageId(SQLState.UNSUPPORTED_ENCODING),
94                 "byte[]", charsetName + " String", e);
95                 
96         }
97     }
98
99     // CTOR for ascii/unicode stream input
100
//"US-ASCII", "UTF-8", or "UnicodeBigUnmarked"
101
public Clob(Agent agent,
102                 java.io.InputStream JavaDoc inputStream,
103                 String JavaDoc 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")) { // "UTF-8"
114
unicodeStream_ = inputStream;
115             dataType_ |= UNICODE_STREAM;
116         } else if (encoding.equals("UnicodeBigUnmarked")) { // "UnicodeBigUnmarked"
117
try {
118                 characterStream_ =
119                         new java.io.InputStreamReader JavaDoc(inputStream, "UnicodeBigUnmarked");
120             } catch (java.io.UnsupportedEncodingException JavaDoc 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     /**
131      * Create a <code>Clob</code> of unknown length with the specified
132      * encoding.
133      *
134      * This constructor was added to support the JDBC 4 length less overloads.
135      * Note that a <code>Clob</code> created with this constructor is made for
136      * input to the database only. Do not pass it out to the user!
137      *
138      * @param agent
139      * @param inputStream the data to insert
140      * @param encoding encoding to use for characters. Only "US-ASCII" is
141      * allowed.
142      */

143     public Clob(Agent agent, java.io.InputStream JavaDoc inputStream, String JavaDoc 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     // CTOR for character stream input
160
// THE ENCODING IS ASSUMED TO BE "UTF-16BE"
161
public Clob(Agent agent, java.io.Reader JavaDoc reader, int length) {
162         this(agent);
163         sqlLength_ = length;
164         lengthObtained_ = true;
165         characterStream_ = reader;
166         dataType_ |= CHARACTER_STREAM;
167     }
168
169     /**
170      * Create a <code>Clob</code> of unknown length.
171      *
172      * This constructor was added to support the JDBC 4 length less overloads.
173      * Note that a <code>Clob</code> created with this constructor is made for
174      * input to the database only. Do not pass it out to the user!
175      *
176      * @param agent
177      * @param reader the data to insert
178      */

179     public Clob(Agent agent, Reader JavaDoc reader) {
180         this(agent);
181         lengthObtained_ = false;
182         // Wrap reader in stream to share code.
183
unicodeStream_ = EncodedInputStream.createUTF8Stream(reader);
184         // Override type to share logic with the other stream types.
185
dataType_ |= UNICODE_STREAM;
186     }
187
188     private Clob(Agent agent) {
189         super(agent);
190     }
191
192     protected void finalize() throws java.lang.Throwable JavaDoc {
193         super.finalize();
194         if (internalLengthStmt_ != null) {
195             internalLengthStmt_.closeX();
196         }
197     }
198
199     // ---------------------------jdbc 2------------------------------------------
200
// Create another method lengthX for internal calls
201
public long length() throws SQLException JavaDoc {
202         
203         //call checkValidity to exit by throwing a SQLException if
204
//the Clob object has been freed by calling free() on it
205
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   /**
233    * Returns a copy of the specified substring
234    * in the <code>CLOB</code> value
235    * designated by this <code>Clob</code> object.
236    * The substring begins at position
237    * <code>pos</code> and has up to <code>length</code> consecutive
238    * characters. The starting position must be between 1 and the length
239    * of the CLOB plus 1. This allows for zero-length CLOB values, from
240    * which only zero-length substrings can be returned.
241    * If a larger length is requested than there are characters available,
242    * characters to the end of the CLOB are returned.
243    * @param pos the first character of the substring to be extracted.
244    * The first character is at position 1.
245    * @param length the number of consecutive characters to be copied
246    * @return a <code>String</code> that is the specified substring in
247    * the <code>CLOB</code> value designated by this <code>Clob</code> object
248    * @exception SQLException if there is an error accessing the
249    * <code>CLOB</code>
250
251    * NOTE: If the starting position is the length of the CLOB plus 1,
252    * zero characters are returned regardless of the length requested.
253    */

254     public String JavaDoc getSubString(long pos, int length) throws SQLException JavaDoc {
255         
256         //call checkValidity to exit by throwing a SQLException if
257
//the Clob object has been freed by calling free() on it
258
checkValidity();
259         
260         try
261         {
262             synchronized (agent_.connection_) {
263                 String JavaDoc 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 JavaDoc(pos));
273                 }
274                 
275                 if ( length < 0 ) {
276                     throw new SqlException(agent_.logWriter_,
277                         new ClientMessageId(SQLState.BLOB_NONPOSITIVE_LENGTH),
278                         new Integer JavaDoc(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 JavaDoc(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 JavaDoc getSubStringX(long pos, int length) throws SqlException {
301         try
302         {
303             checkForClosedConnection();
304             // actual length is the lesser of the length requested
305
// and the number of characters available from pos to the end
306
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 JavaDoc se )
310         {
311             throw new SqlException(se);
312         }
313     }
314
315     public java.io.Reader JavaDoc getCharacterStream() throws SQLException JavaDoc {
316         
317         //call checkValidity to exit by throwing a SQLException if
318
//the Clob object has been freed by calling free() on it
319
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 JavaDoc 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 JavaDoc getCharacterStreamX() throws SqlException {
342         checkForClosedConnection();
343
344         if (isCharacterStream()) // this Lob is used for input
345
{
346             return characterStream_;
347         }
348
349         return new java.io.StringReader JavaDoc(string_);
350     }
351
352     public java.io.InputStream JavaDoc getAsciiStream() throws SQLException JavaDoc {
353         
354         //call checkValidity to exit by throwing a SQLException if
355
//the Clob object has been freed by calling free() on it
356
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 JavaDoc 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 JavaDoc getAsciiStreamX() throws SqlException {
379         checkForClosedConnection();
380
381         if (isAsciiStream()) // this Lob is used for input
382
{
383             return asciiStream_;
384         }
385
386         return new AsciiStream(string_, new java.io.StringReader JavaDoc(string_));
387     }
388
389     public long position(String JavaDoc searchstr, long start) throws SQLException JavaDoc {
390         
391         //call checkValidity to exit by throwing a SQLException if
392
//the Clob object has been freed by calling free() on it
393
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 JavaDoc(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 JavaDoc 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 JavaDoc(start), "start", "Clob.position()");
435         }
436
437         int index = string_.indexOf(searchstr, (int) start - 1);
438         if (index != -1) {
439             index++; // api index starts at 1
440
}
441         return (long) index;
442     }
443
444     public long position(java.sql.Clob JavaDoc searchstr, long start) throws SQLException JavaDoc {
445         
446         //call checkValidity to exit by throwing a SQLException if
447
//the Clob object has been freed by calling free() on it
448
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 JavaDoc(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 JavaDoc 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 JavaDoc(start), "start", "Clob.position()");
489         }
490
491         // if the searchstr is longer than the source, no match
492
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 JavaDoc e) {
500             throw new SqlException(e);
501         }
502         if (index != -1) {
503             index++; // api index starts at 1
504
}
505         return (long) index;
506     }
507
508     //---------------------------- jdbc 3.0 -----------------------------------
509

510     public int setString(long pos, String JavaDoc str) throws SQLException JavaDoc {
511         
512         //call checkValidity to exit by throwing a SQLException if
513
//the Clob object has been freed by calling free() on it
514
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 JavaDoc str, int offset, int len) throws SQLException JavaDoc {
536         
537         //call checkValidity to exit by throwing a SQLException if
538
//the Clob object has been freed by calling free() on it
539
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 JavaDoc 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 JavaDoc(pos));
565         }
566         if ( pos - 1 > sqlLength_) {
567             throw new SqlException(agent_.logWriter_,
568                 new ClientMessageId(SQLState.BLOB_POSITION_TOO_LARGE),
569                 new Long JavaDoc(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 JavaDoc(offset));
575         }
576         
577         if ( len < 0 ) {
578             throw new SqlException(agent_.logWriter_,
579                 new ClientMessageId(SQLState.BLOB_NONPOSITIVE_LENGTH),
580                 new Integer JavaDoc(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 JavaDoc newString = string_.substring(0, (int) pos - 1);
590         string_ = newString.concat(str.substring(offset, offset + length));
591         asciiStream_ = new java.io.StringBufferInputStream JavaDoc(string_);
592         unicodeStream_ = new java.io.StringBufferInputStream JavaDoc(string_);
593         characterStream_ = new java.io.StringReader JavaDoc(string_);
594         sqlLength_ = string_.length();
595         return length;
596     }
597
598     public java.io.OutputStream JavaDoc setAsciiStream(long pos) throws SQLException JavaDoc {
599         
600         //call checkValidity to exit by throwing a SQLException if
601
//the Clob object has been freed by calling free() on it
602
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 JavaDoc setCharacterStream(long pos) throws SQLException JavaDoc {
625         
626         //call checkValidity to exit by throwing a SQLException if
627
//the Clob object has been freed by calling free() on it
628
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 JavaDoc {
651         
652         //call checkValidity to exit by throwing a SQLException if
653
//the Clob object has been freed by calling free() on it
654
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 JavaDoc(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 JavaDoc(len));
672                 }
673                 
674                 if (len == this.length()) {
675                     return;
676                 }
677                 String JavaDoc newstr = string_.substring(0, (int) len);
678                 string_ = newstr;
679                 asciiStream_ = new java.io.StringBufferInputStream JavaDoc(string_);
680                 unicodeStream_ = new java.io.StringBufferInputStream JavaDoc(string_);
681                 characterStream_ = new java.io.StringReader JavaDoc(string_);
682                 sqlLength_ = string_.length();
683             }
684         }
685         catch ( SqlException se )
686         {
687             throw se.getSQLException();
688         }
689     }
690
691     //---------------------------- jdbc 4.0 -------------------------------------
692
/**
693      * This method frees the <code>Clob</code> object and releases the resources the resources
694      * that it holds. The object is invalid once the <code>free</code> method
695      * is called. If <code>free</code> is called multiple times, the
696      * subsequent calls to <code>free</code> are treated as a no-op.
697      *
698      * @throws SQLException if an error occurs releasing
699      * the Clob's resources
700      */

701     public void free()
702         throws SQLException JavaDoc {
703         
704         //calling free() on a already freed object is treated as a no-op
705
if (!isValid) return;
706         
707         //now that free has been called the Blob object is no longer
708
//valid
709
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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc getCharacterStream(long pos, long length)
753         throws SQLException JavaDoc {
754         throw SQLExceptionFactory.notImplemented(
755                 "getCharacterStream(long,long");
756     }
757     
758     //----------------------------helper methods----------------------------------
759

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 JavaDoc getUnicodeStream() {
777         return unicodeStream_;
778     }
779
780     public String JavaDoc getString() {
781         return string_;
782     }
783
784     public byte[] getUtf8String() {
785         return utf8String_;
786     }
787
788     // Return the length of the equivalent UTF-8 string
789
// precondition: string_ is not null and dataType_ includes STRING
790
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 JavaDoc e) {
799             throw new SqlException(agent_.logWriter_,
800                 new ClientMessageId(SQLState.UNSUPPORTED_ENCODING),
801                 "String", "UTF8 byte[]", e);
802         }
803     }
804
805     // auxiliary method for position (Clob, long)
806
protected Clob createClobWrapper(java.sql.Clob JavaDoc clob) throws SqlException {
807         long length;
808         java.io.Reader JavaDoc rdr;
809
810         try {
811             length = clob.length();
812         } catch (java.sql.SQLException JavaDoc 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 JavaDoc(length), new Integer JavaDoc(Integer.MAX_VALUE));
820         }
821
822         try {
823             rdr = clob.getCharacterStream();
824         } catch (java.sql.SQLException JavaDoc 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 JavaDoc(asciiStream_, "US-ASCII");
835             dataType_ = CHARACTER_STREAM;
836         } catch (java.io.UnsupportedEncodingException JavaDoc e) {
837             throw new SqlException(agent_.logWriter_,
838                 new ClientMessageId(SQLState.UNSUPPORTED_ENCODING),
839                 "US-ASCII", "CharacterStream", e);
840         }
841     }
842
843     // this method is primarily for mixed clob length calculations.
844
// it was introduced to prevent recursion in the actual char length calculation
845
public long getByteLength() throws SQLException JavaDoc {
846             if (lengthObtained_ == true) {
847                 return lengthInBytes_;
848             }
849
850             length();
851             return lengthInBytes_;
852     }
853     
854     /*
855      * Checks is isValid is true. If it is not true throws
856      * a SQLException stating that a method has been called on
857      * an invalid LOB object
858      *
859      * throws SQLException if isvalid is not true.
860      */

861     private void checkValidity() throws SQLException JavaDoc{
862         if(!isValid)
863             throw new SqlException(null,new ClientMessageId(SQLState.LOB_OBJECT_INVALID))
864                                                   .getSQLException();
865     }
866
867     /**
868      * Materialize the stream used for input to the database.
869      */

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