KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > jdbc > JdbcClob


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.jdbc;
6
7 import java.io.InputStream JavaDoc;
8 import java.io.OutputStream JavaDoc;
9 import java.io.Reader JavaDoc;
10 import java.io.Writer JavaDoc;
11 import java.sql.Clob JavaDoc;
12 import java.sql.SQLException JavaDoc;
13
14 import org.h2.engine.Constants;
15 import org.h2.engine.SessionInterface;
16 import org.h2.message.Message;
17 import org.h2.message.TraceObject;
18 import org.h2.util.IOUtils;
19 import org.h2.value.Value;
20
21 //#ifdef JDK16
22
/*
23 import java.sql.NClob;
24 */

25 //#endif
26

27 /**
28  * Represents a CLOB value.
29  */

30 public class JdbcClob extends TraceObject implements Clob JavaDoc
31 //#ifdef JDK16
32
/*
33 , NClob
34 */

35 //#endif
36
{
37
38     private Value value;
39     private JdbcConnection conn;
40
41     public JdbcClob(SessionInterface session, JdbcConnection conn, Value value, int id) {
42         setTrace(session.getTrace(), TraceObject.CLOB, id);
43         this.conn = conn;
44         this.value = value;
45     }
46
47     /**
48      * Returns the length.
49      *
50      * @return the length
51      */

52     public long length() throws SQLException JavaDoc {
53         try {
54             debugCodeCall("length");
55             checkClosed();
56             if(value.getType() == Value.CLOB) {
57                 long precision = value.getPrecision();
58                 if(precision > 0) {
59                     return precision;
60                 }
61             }
62             Reader JavaDoc in = value.getReader();
63             try {
64                 long size = 0;
65                 char[] buff = new char[Constants.FILE_BLOCK_SIZE];
66                 while(true) {
67                     int len = in.read(buff, 0, Constants.FILE_BLOCK_SIZE);
68                     if(len <= 0) {
69                         break;
70                     }
71                     size += len;
72                 }
73                 return size;
74             } finally {
75                 in.close();
76             }
77         } catch(Throwable JavaDoc e) {
78             throw Message.convert(e);
79         }
80     }
81
82     /**
83      * Truncates the object.
84      *
85      * @throws SQLException Unsupported Feature (SQL State 0A000)
86      */

87     public void truncate(long len) throws SQLException JavaDoc {
88         debugCodeCall("truncate", len);
89         throw Message.getUnsupportedException();
90     }
91
92     /**
93      * Returns the input stream.
94      *
95      * @return the input stream
96      */

97     public InputStream JavaDoc getAsciiStream() throws SQLException JavaDoc {
98         try {
99             debugCodeCall("getAsciiStream");
100             checkClosed();
101             String JavaDoc s = value.getString();
102             return IOUtils.getInputStream(s);
103         } catch(Throwable JavaDoc e) {
104             throw logAndConvert(e);
105         }
106     }
107
108     /**
109      * Returns an output stream.
110      *
111      * @throws SQLException Unsupported Feature (SQL State 0A000)
112      */

113     public OutputStream JavaDoc setAsciiStream(long pos) throws SQLException JavaDoc {
114         debugCodeCall("setAsciiStream", pos);
115         throw Message.getUnsupportedException();
116     }
117
118     /**
119      * Returns the reader.
120      *
121      * @return the reader
122      */

123     public Reader JavaDoc getCharacterStream() throws SQLException JavaDoc {
124         try {
125             debugCodeCall("getCharacterStream");
126             checkClosed();
127             return value.getReader();
128         } catch(Throwable JavaDoc e) {
129             throw logAndConvert(e);
130         }
131     }
132
133     /**
134      * Returns a writer.
135      *
136      * @throws SQLException Unsupported Feature (SQL State 0A000)
137      */

138     public Writer JavaDoc setCharacterStream(long pos) throws SQLException JavaDoc {
139         debugCodeCall("setCharacterStream", pos);
140         throw Message.getUnsupportedException();
141     }
142
143     /**
144      * Returns a substring.
145      *
146      * @param pos the position (the first character is at position 1)
147      * @param length the number of characters
148      * @return the string
149      */

150     public String JavaDoc getSubString(long pos, int length) throws SQLException JavaDoc {
151         try {
152             debugCode("getSubString("+pos+", "+length+");");
153             checkClosed();
154             if(pos < 1) {
155                 throw Message.getInvalidValueException("pos", ""+pos);
156             }
157             if(length < 0) {
158                 throw Message.getInvalidValueException("length", ""+length);
159             }
160             StringBuffer JavaDoc buff = new StringBuffer JavaDoc(length);
161             Reader JavaDoc reader = value.getReader();
162             try {
163                 IOUtils.skipFully(reader, pos - 1);
164                 for(int i=0; i<length; i++) {
165                     int ch = reader.read();
166                     if(ch < 0) {
167                         break;
168                     }
169                     buff.append((char) ch);
170                 }
171             } finally {
172                 reader.close();
173             }
174             return buff.toString();
175         } catch(Throwable JavaDoc e) {
176             throw logAndConvert(e);
177         }
178     }
179
180     /**
181      * Sets a substring.
182      *
183      * @throws SQLException Unsupported Feature (SQL State 0A000)
184      */

185     public int setString(long pos, String JavaDoc str) throws SQLException JavaDoc {
186         debugCode("setString("+pos+", "+quote(str)+");");
187         throw Message.getUnsupportedException();
188     }
189
190     /**
191      * Sets a substring.
192      *
193      * @throws SQLException Unsupported Feature (SQL State 0A000)
194      */

195     public int setString(long pos, String JavaDoc str, int offset, int len) throws SQLException JavaDoc {
196         debugCode("setString("+pos+", "+quote(str)+", "+offset+", "+len+");");
197         throw Message.getUnsupportedException();
198     }
199
200     /**
201      * Searches a pattern and return the position.
202      *
203      * @throws SQLException Unsupported Feature (SQL State 0A000)
204      */

205     public long position(String JavaDoc pattern, long start) throws SQLException JavaDoc {
206         debugCode("position("+quote(pattern)+", "+start+");");
207         throw Message.getUnsupportedException();
208     }
209
210     /**
211      * Searches a pattern and return the position.
212      *
213      * @throws SQLException Unsupported Feature (SQL State 0A000)
214      */

215     public long position(Clob JavaDoc clobPattern, long start) throws SQLException JavaDoc {
216         debugCode("position(clobPattern, "+start+");");
217         throw Message.getUnsupportedException();
218     }
219
220     /**
221      * Release all resources of this object.
222      */

223     public void free() throws SQLException JavaDoc {
224         debugCodeCall("free");
225         value = null;
226     }
227
228     /**
229      * Returns the reader, starting from an offset.
230      *
231      * @throws SQLException Unsupported Feature (SQL State 0A000)
232      */

233     public Reader JavaDoc getCharacterStream(long pos, long length) throws SQLException JavaDoc {
234         debugCode("getCharacterStream("+pos+", "+length+");");
235         throw Message.getUnsupportedException();
236     }
237
238     private void checkClosed() throws SQLException JavaDoc {
239         conn.checkClosed();
240         if (value == null) {
241             throw Message.getSQLException(Message.OBJECT_CLOSED);
242         }
243     }
244
245 }
246
Popular Tags