1 30 31 32 package org.hsqldb.jdbc; 33 34 import java.io.StringReader ; 35 import java.sql.Clob ; 36 import java.sql.SQLException ; 37 38 import org.hsqldb.Trace; 39 import org.hsqldb.lib.AsciiStringInputStream; 40 41 52 81 public final class jdbcClob implements Clob { 82 83 volatile String data; 84 85 102 public jdbcClob(final String data) throws SQLException { 103 104 if (data == null) { 105 throw Util.sqlException(Trace.INVALID_JDBC_ARGUMENT, "null"); 106 } 107 108 this.data = data; 109 } 110 111 121 public long length() throws SQLException { 122 123 final String ldata = data; 124 125 return ldata.length(); 126 } 127 128 157 public String getSubString(long pos, 158 final int length) throws SQLException { 159 160 final String ldata = data; 161 final int dlen = ldata.length(); 162 163 pos--; 164 165 if (pos < 0 || pos > dlen) { 166 Util.sqlException(Trace.INVALID_JDBC_ARGUMENT, 167 "pos: " + (pos + 1L)); 168 } 169 170 if (length < 0 || length > dlen - pos) { 171 throw Util.sqlException(Trace.INVALID_JDBC_ARGUMENT, 172 "length: " + length); 173 } 174 175 if (pos == 0 && length == dlen) { 176 return ldata; 177 } 178 179 return ldata.substring((int) pos, (int) pos + length); 180 } 181 182 195 public java.io.Reader getCharacterStream() throws SQLException { 196 197 final String ldata = data; 198 199 return new StringReader (ldata); 200 } 201 202 214 public java.io.InputStream getAsciiStream() throws SQLException { 215 216 final String ldata = data; 217 218 return new AsciiStringInputStream(ldata); 219 } 220 221 237 public long position(final String searchstr, 238 long start) throws SQLException { 239 240 if (searchstr == null || start > Integer.MAX_VALUE) { 241 return -1; 242 } 243 244 final String ldata = data; 245 final int pos = ldata.indexOf(searchstr, (int) --start); 246 247 return (pos < 0) ? -1 248 : pos + 1; 249 } 250 251 267 public long position(final Clob searchstr, 268 long start) throws SQLException { 269 270 if (searchstr == null) { 271 return -1; 272 } 273 274 final String ldata = data; 275 final long dlen = ldata.length(); 276 final long sslen = searchstr.length(); 277 278 start--; 280 if (start > dlen - sslen) { 285 return -1; 286 } 287 288 String s; 290 291 if (searchstr instanceof jdbcClob) { 292 s = ((jdbcClob) searchstr).data; 293 } else { 294 s = searchstr.getSubString(1L, (int) sslen); 295 } 296 297 final int pos = ldata.indexOf(s, (int) start); 298 299 return (pos < 0) ? -1 300 : pos + 1; 301 } 302 303 305 331 public int setString(long pos, String str) throws SQLException { 332 throw Util.notSupported(); 333 } 334 335 363 public int setString(long pos, String str, int offset, 364 int len) throws SQLException { 365 throw Util.notSupported(); 366 } 367 368 392 public java.io.OutputStream setAsciiStream(long pos) throws SQLException { 393 throw Util.notSupported(); 394 } 395 396 421 public java.io.Writer setCharacterStream(long pos) throws SQLException { 422 throw Util.notSupported(); 423 } 424 425 446 public void truncate(final long len) throws SQLException { 447 448 final String ldata = data; 449 final long dlen = ldata.length(); 450 final long chars = len >> 1; 451 452 if (chars == dlen) { 453 454 } else if (len < 0 || chars > dlen) { 456 throw Util.sqlException(Trace.INVALID_JDBC_ARGUMENT, 457 Long.toString(len)); 458 } else { 459 460 data = new String (ldata.substring(0, (int) chars)); 462 } 463 } 464 } 465 | Popular Tags |