1 5 package org.h2.jdbc; 6 7 import java.io.InputStream ; 8 import java.io.OutputStream ; 9 import java.io.Reader ; 10 import java.io.Writer ; 11 import java.sql.Clob ; 12 import java.sql.SQLException ; 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 25 27 30 public class JdbcClob extends TraceObject implements Clob 31 35 { 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 52 public long length() throws SQLException { 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 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 e) { 78 throw Message.convert(e); 79 } 80 } 81 82 87 public void truncate(long len) throws SQLException { 88 debugCodeCall("truncate", len); 89 throw Message.getUnsupportedException(); 90 } 91 92 97 public InputStream getAsciiStream() throws SQLException { 98 try { 99 debugCodeCall("getAsciiStream"); 100 checkClosed(); 101 String s = value.getString(); 102 return IOUtils.getInputStream(s); 103 } catch(Throwable e) { 104 throw logAndConvert(e); 105 } 106 } 107 108 113 public OutputStream setAsciiStream(long pos) throws SQLException { 114 debugCodeCall("setAsciiStream", pos); 115 throw Message.getUnsupportedException(); 116 } 117 118 123 public Reader getCharacterStream() throws SQLException { 124 try { 125 debugCodeCall("getCharacterStream"); 126 checkClosed(); 127 return value.getReader(); 128 } catch(Throwable e) { 129 throw logAndConvert(e); 130 } 131 } 132 133 138 public Writer setCharacterStream(long pos) throws SQLException { 139 debugCodeCall("setCharacterStream", pos); 140 throw Message.getUnsupportedException(); 141 } 142 143 150 public String getSubString(long pos, int length) throws SQLException { 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 buff = new StringBuffer (length); 161 Reader 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 e) { 176 throw logAndConvert(e); 177 } 178 } 179 180 185 public int setString(long pos, String str) throws SQLException { 186 debugCode("setString("+pos+", "+quote(str)+");"); 187 throw Message.getUnsupportedException(); 188 } 189 190 195 public int setString(long pos, String str, int offset, int len) throws SQLException { 196 debugCode("setString("+pos+", "+quote(str)+", "+offset+", "+len+");"); 197 throw Message.getUnsupportedException(); 198 } 199 200 205 public long position(String pattern, long start) throws SQLException { 206 debugCode("position("+quote(pattern)+", "+start+");"); 207 throw Message.getUnsupportedException(); 208 } 209 210 215 public long position(Clob clobPattern, long start) throws SQLException { 216 debugCode("position(clobPattern, "+start+");"); 217 throw Message.getUnsupportedException(); 218 } 219 220 223 public void free() throws SQLException { 224 debugCodeCall("free"); 225 value = null; 226 } 227 228 233 public Reader getCharacterStream(long pos, long length) throws SQLException { 234 debugCode("getCharacterStream("+pos+", "+length+");"); 235 throw Message.getUnsupportedException(); 236 } 237 238 private void checkClosed() throws SQLException { 239 conn.checkClosed(); 240 if (value == null) { 241 throw Message.getSQLException(Message.OBJECT_CLOSED); 242 } 243 } 244 245 } 246 | Popular Tags |