KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > types > ValueClob


1 package com.quadcap.sql.types;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.BufferedInputStream JavaDoc;
42 import java.io.BufferedOutputStream JavaDoc;
43 import java.io.Externalizable JavaDoc;
44 import java.io.InputStream JavaDoc;
45 import java.io.IOException JavaDoc;
46 import java.io.ObjectInput JavaDoc;
47 import java.io.ObjectOutput JavaDoc;
48 import java.io.OutputStream JavaDoc;
49 import java.io.Reader JavaDoc;
50
51 //#ifndef JDK11
52
import java.sql.Clob JavaDoc;
53 //#endif
54
import java.sql.SQLException JavaDoc;
55
56 import com.quadcap.sql.file.BlockFile;
57 import com.quadcap.sql.file.Datafile;
58 import com.quadcap.sql.file.RandomAccess;
59 import com.quadcap.sql.file.RandomAccessInputStream;
60 import com.quadcap.sql.file.RandomAccessOutputStream;
61
62 import com.quadcap.io.AsciiInputStream;
63
64 import com.quadcap.util.text.TextMatch;
65
66 import com.quadcap.util.Debug;
67 import com.quadcap.util.Util;
68
69 /**
70  * A <b>CLOB</b> value.
71  *
72  * @author Stan Bailes
73  */

74 public class ValueClob extends ValueBlob
75 //- //#ifdef JDK11
76
//- implements Externalizable
77
//#else
78
implements Clob JavaDoc, Externalizable JavaDoc
79     //#endif
80
{
81     public ValueClob() { }
82
83     public ValueClob(Datafile file, long transId,
84              Reader JavaDoc r, int length)
85     throws IOException JavaDoc
86     {
87     super(file, transId, new CharStream(r), length*2);
88     }
89
90     public ValueClob(String JavaDoc s) {
91     super(Util.strCharsAsBytes(s));
92     }
93
94     public Value unop(int op) throws ValueException {
95     switch (op) {
96     case Op.NULL: // isnull(String) -> ValueBoolean.FALSE
97
return ValueBoolean.falseBoolean;
98     case Op.PATTERN: // pattern(String) -> ValuePattern
99
return new ValuePattern(toString(), ValuePattern.defaultEscape);
100     default:
101         throw new ValueException("Unary op: " + Op.toString(op) +
102                      " not implemented for this type");
103     }
104     }
105     
106     public Value binop(int op, Value l) throws ValueException {
107         return l.binop(op, this);
108     }
109
110     public Value binop(int op, ValueString r) throws ValueException {
111         return ValueString.binop(op, convert(TypeVarChar.typeVarChar), r);
112     }
113
114     public Object JavaDoc asJavaObject() {
115     return this;
116     }
117
118     public void fromJavaObject(Object JavaDoc obj) throws ValueException {
119     throw new ValueException("bad type: " + obj);
120     }
121
122     public Type getType() throws SQLException JavaDoc {
123     return new TypeClob();
124     }
125
126     /**
127      * Return a sub-string of the CLOB.
128      */

129     public char[] getChars(long cpos, int clen) throws SQLException JavaDoc {
130         cpos--; // convert one-based parameter to zero-based for simplicity
131
try {
132             if (clen > 1024 * 1024) throw new SQLException JavaDoc("CLOB too long");
133         char[] buf = new char[clen];
134             if (bytes == null) {
135                 getRandomAccess();
136                 byte[] tbuf = new byte[4096];
137                 int bpos = (int)(cpos * 2);
138                 int cbufpos = 0;
139                 int blen = clen * 2;
140                 while (blen > 0) {
141                     int len = blen > 4096 ? 4096 : blen;
142                     ra.read(bpos, tbuf, 0, len);
143                     bpos += len;
144                     blen -= len;
145                     Util.bytesToChars(tbuf, 0, buf, cbufpos, len/2);
146                     cbufpos += (len/2);
147                 }
148             } else {
149                 Util.bytesToChars(bytes, (int)(cpos*2), buf, 0, clen);
150             }
151         return buf;
152     } catch (IOException JavaDoc e) {
153         throw new SQLException JavaDoc(e.toString(), "Q001G");
154     }
155     }
156     
157     public InputStream JavaDoc getAsciiStream() throws SQLException JavaDoc {
158     return getBinaryStream();
159     }
160
161     public Reader JavaDoc getCharacterStream() throws SQLException JavaDoc {
162     return new ByteReader(getBinaryStream());
163     }
164
165     public String JavaDoc getSubString(long pos, int length) throws SQLException JavaDoc {
166     return new String JavaDoc(getChars(pos, length));
167     }
168     
169     public long length() throws SQLException JavaDoc {
170     return super.length() / 2;
171     }
172
173     //#ifndef JDK11
174
public long position(Clob JavaDoc clob, long start) throws SQLException JavaDoc {
175     String JavaDoc pattern = clob.getSubString(0, (int)clob.length());
176     return position(pattern, start);
177     }
178     //#endif
179

180     public long position(String JavaDoc pattern, long start) throws SQLException JavaDoc {
181     return super.position(pattern.getBytes(), start);
182     }
183
184     public void readExternal(ObjectInput JavaDoc in)
185     throws IOException JavaDoc, ClassNotFoundException JavaDoc
186     {
187     super.readExternal(in);
188     }
189     
190     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
191     super.writeExternal(out);
192     }
193
194     public Value convert(TypeVarChar type) throws ValueException {
195         int len = 0;
196         try {
197             len = (int)length();
198             if (type.getMax() < 0 || type.getMax() >= len) {
199                 return new ValueString(toString());
200             } else {
201                 return new ValueString(getSubString(1, type.getMax()));
202             }
203         } catch (SQLException JavaDoc e) {
204             Debug.print(e);
205             throw new ValueException(e.toString());
206         }
207     }
208     
209     public Value convert(TypeClob type) throws ValueException {
210     return this;
211     }
212
213     public String JavaDoc toString() {
214     try {
215         return getSubString(1L, (int)length());
216     } catch (SQLException JavaDoc e) {
217         return "";
218     }
219     }
220
221
222     //---------------------------- jdbc 3.0 -----------------------------------
223

224     /**
225      * Writes the given Java <code>String</code> to the <code>CLOB</code>
226      * value that this <code>Clob</code> object designates at the position
227      * <code>pos</code>.
228      *
229      * @param pos the position at which to start writing to the <code>CLOB</code>
230      * value that this <code>Clob</code> object represents
231      * @param str the string to be written to the <code>CLOB</code>
232      * value that this <code>Clob</code> designates
233      * @return the number of characters written
234      * @exception SQLException if there is an error accessing the
235      * <code>CLOB</code> value
236      *
237      * @since 1.4
238      */

239     public int setString(long pos, String JavaDoc str)
240         throws SQLException JavaDoc
241     {
242         throw new SQLException JavaDoc("Not implemented");
243     }
244
245     /**
246      * Writes <code>len</code> characters of <code>str</code>, starting
247      * at character <code>offset</code>, to the <code>CLOB</code> value
248      * that this <code>Clob</code> represents.
249      *
250      * @param pos the position at which to start writing to this
251      * <code>CLOB</code> object
252      * @param str the string to be written to the <code>CLOB</code>
253      * value that this <code>Clob</code> object represents
254      * @param offset the offset into <code>str</code> to start reading
255      * the characters to be written
256      * @param len the number of characters to be written
257      * @return the number of characters written
258      * @exception SQLException if there is an error accessing the
259      * <code>CLOB</code> value
260      *
261      * @since 1.4
262      */

263     public int setString(long pos, String JavaDoc str, int offset, int len)
264         throws SQLException JavaDoc
265     {
266         throw new SQLException JavaDoc("Not implemented");
267     }
268
269     /**
270      * Retrieves a stream to be used to write Ascii characters to the
271      * <code>CLOB</code> value that this <code>Clob</code> object represents,
272      * starting at position <code>pos</code>.
273      *
274      * @param pos the position at which to start writing to this
275      * <code>CLOB</code> object
276      * @return the stream to which ASCII encoded characters can be written
277      * @exception SQLException if there is an error accessing the
278      * <code>CLOB</code> value
279      * @see #getAsciiStream
280      *
281      * @since 1.4
282      */

283     public java.io.OutputStream JavaDoc setAsciiStream(long pos)
284         throws SQLException JavaDoc
285     {
286         throw new SQLException JavaDoc("Not implemented");
287     }
288
289     /**
290      * Retrieves a stream to be used to write a stream of Unicode characters
291      * to the <code>CLOB</code> value that this <code>Clob</code> object
292      * represents, at position <code>pos</code>.
293      *
294      * @param pos the position at which to start writing to the
295      * <code>CLOB</code> value
296      *
297      * @return a stream to which Unicode encoded characters can be written
298      * @exception SQLException if there is an error accessing the
299      * <code>CLOB</code> value
300      * @see #getCharacterStream
301      *
302      * @since 1.4
303      */

304     public java.io.Writer JavaDoc setCharacterStream(long pos)
305         throws SQLException JavaDoc
306     {
307         throw new SQLException JavaDoc("Not implemented");
308     }
309
310 }
311
Popular Tags