1 30 31 32 package org.hsqldb.jdbc; 33 34 import java.io.ByteArrayInputStream ; 35 import java.io.InputStream ; 36 import java.io.OutputStream ; 37 import java.sql.Blob ; 38 import java.sql.SQLException ; 39 40 import org.hsqldb.Trace; 41 42 48 78 public class jdbcBlob implements Blob { 79 80 volatile byte[] data; 81 82 95 public jdbcBlob(final byte[] data) throws SQLException { 96 97 if (data == null) { 98 throw Util.sqlException(Trace.INVALID_JDBC_ARGUMENT, "null"); 99 } 100 101 this.data = data; } 103 104 114 public long length() throws SQLException { 115 116 final byte[] ldata = data; 117 118 return ldata.length; 119 } 120 121 152 public byte[] getBytes(long pos, final int length) throws SQLException { 153 154 final byte[] ldata = data; 155 final int dlen = ldata.length; 156 157 pos--; 158 159 if (pos < 0 || pos > dlen) { 160 throw Util.sqlException(Trace.INVALID_JDBC_ARGUMENT, 161 "pos: " + (pos + 1)); 162 } 163 164 if (length < 0 || length > dlen - pos) { 165 throw Util.sqlException(Trace.INVALID_JDBC_ARGUMENT, 166 "length: " + length); 167 } 168 169 final byte[] out = new byte[length]; 170 171 System.arraycopy(ldata, (int) pos, out, 0, length); 172 173 return out; 174 } 175 176 187 public InputStream getBinaryStream() throws SQLException { 188 189 final byte[] ldata = data; 190 191 return new ByteArrayInputStream (ldata); 192 } 193 194 210 public long position(final byte[] pattern, 211 long start) throws SQLException { 212 213 final byte[] ldata = data; 214 final int dlen = ldata.length; 215 216 if (start > dlen || pattern == null) { 217 return -1; 218 } else if (start < 1) { 219 start = 0; 220 } else { 221 start--; 222 } 223 224 final int plen = pattern.length; 225 226 if (plen == 0 || start > dlen - plen) { 227 return -1; 228 } 229 230 final int stop = dlen - plen; 231 final byte b0 = pattern[0]; 232 233 outer_loop: 234 for (int i = (int) start; i <= stop; i++) { 235 if (ldata[i] != b0) { 236 continue; 237 } 238 239 int len = plen; 240 int doffset = i; 241 int poffset = 0; 242 boolean match = true; 243 244 while (len-- > 0) { 245 if (ldata[doffset++] != pattern[poffset++]) { 246 continue outer_loop; 247 } 248 } 249 250 return i + 1; 251 } 252 253 return -1; 254 } 255 256 272 public long position(final Blob pattern, long start) throws SQLException { 273 274 final byte[] ldata = data; 275 final int dlen = ldata.length; 276 277 if (start > dlen || pattern == null) { 278 return -1; 279 } else if (start < 1) { 280 start = 0; 281 } else { 282 start--; 283 } 284 285 final long plen = pattern.length(); 286 287 if (plen == 0 || start > ((long) dlen) - plen) { 288 return -1; 289 } 290 291 final int iplen = (int) plen; 293 byte[] bap; 294 295 if (pattern instanceof jdbcBlob) { 296 bap = ((jdbcBlob) pattern).data; 297 } else { 298 bap = pattern.getBytes(1, iplen); 299 } 300 301 final int stop = dlen - iplen; 302 final byte b0 = bap[0]; 303 304 outer_loop: 305 for (int i = (int) start; i <= stop; i++) { 306 if (ldata[i] != b0) { 307 continue; 308 } 309 310 int len = iplen; 311 int doffset = i; 312 int poffset = 0; 313 314 while (len-- > 0) { 315 if (ldata[doffset++] != bap[poffset++]) { 316 continue outer_loop; 317 } 318 } 319 320 return i + 1; 321 } 322 323 return -1; 324 } 325 326 328 354 public int setBytes(long pos, byte[] bytes) throws SQLException { 355 throw Util.notSupported(); 356 } 357 358 390 public int setBytes(long pos, byte[] bytes, int offset, 391 int len) throws SQLException { 392 throw Util.notSupported(); 393 } 394 395 420 public OutputStream setBinaryStream(long pos) throws SQLException { 421 throw Util.notSupported(); 422 } 423 424 444 public void truncate(final long len) throws SQLException { 445 446 final byte[] ldata = data; 447 448 if (len < 0 || len > ldata.length) { 449 throw Util.sqlException(Trace.INVALID_JDBC_ARGUMENT, 450 Long.toString(len)); 451 } 452 453 if (len == ldata.length) { 454 return; 455 } 456 457 byte[] newData = new byte[(int) len]; 458 459 System.arraycopy(ldata, 0, newData, 0, (int) len); 460 461 data = newData; 462 } 463 464 } 494 | Popular Tags |