@Override public Value convertPrecision(long precision, boolean force) { if (this.precision <= precision) { return this; } ValueLobDb lob; if (type == CLOB) { lob = ValueLobDb.createTempClob(getReader(), precision); } else { lob = ValueLobDb.createTempBlob(getInputStream(), precision); } return lob; }
/** * Create a LOB object that fits in memory. * * @param type the type (Value.BLOB or CLOB) * @param small the byte array * @param precision the precision * @return the LOB */ public static ValueLobDb createSmallLob(int type, byte[] small, long precision) { return new ValueLobDb(type, small, precision); }
@Override public boolean equals(Object other) { return other instanceof ValueLobDb && compareSecure((Value) other, null) == 0; }
@Override public int hashCode() { if (hash == 0) { if (precision > 4096) { // TODO: should calculate the hash code when saving, and store // it in the database file return (int) (precision ^ (precision >>> 32)); } if (type == CLOB) { hash = getString().hashCode(); } else { hash = Utils.getByteArrayHash(getBytes()); } } return hash; }
/** * Create a CLOB in a temporary file. */ private ValueLobDb(Reader in, long remaining) throws IOException { this.type = Value.CLOB; this.small = null; this.lobId = 0; this.fileName = createTempLobFileName(); long tmpPrecision = 0; try { char[] buff = new char[Constants.IO_BUFFER_SIZE]; while (true) { int len = getBufferSize(remaining); len = IOUtils.readFully(in, buff, len); if (len == 0) { break; } } } finally { //tempFile.close(); } this.precision = tmpPrecision; }
remaining = length; int len = getBufferSize(remaining); char[] buff; if (len >= Integer.MAX_VALUE) { len = IOUtils.readFully(reader, buff, len); if (len <= getMaxLengthInplaceLob()) { byte[] small = new String(buff, 0, len).getBytes(Constants.UTF8); return ValueLobDb.createSmallLob(Value.CLOB, small, len); ValueLobDb lob = new ValueLobDb(reader, remaining); return lob; } catch (IOException e) {
} else if (x instanceof java.io.Reader) { Reader r = new BufferedReader((java.io.Reader) x); return ValueLobDb.createTempClob(r, -1); } else if (x instanceof java.sql.Clob) { try { Reader r = new BufferedReader( ((java.sql.Clob) x).getCharacterStream()); return ValueLobDb.createTempClob(r, -1); } catch (SQLException e) { throw DbException.convert(e); return ValueLobDb.createTempBlob((java.io.InputStream) x, -1); } else if (x instanceof java.sql.Blob) { try { return ValueLobDb.createTempBlob(((java.sql.Blob) x).getBinaryStream(), -1); } catch (SQLException e) { throw DbException.convert(e);
/** * Create a Clob value from this reader. * * @param x the reader * @param length the length (if smaller or equal than 0, all data until the * end of file is read) * @return the value */ public Value createClob(Reader x, long length) { if (x == null) { return ValueNull.INSTANCE; } if (length <= 0) { length = -1; } Value v = ValueLobDb.createTempClob(x, length); session.addTemporaryLob(v); return v; }
/** * Create a Blob value from this input stream. * * @param x the input stream * @param length the length (if smaller or equal than 0, all data until the * end of file is read) * @return the value */ public Value createBlob(InputStream x, long length) { if (x == null) { return ValueNull.INSTANCE; } if (length <= 0) { length = -1; } Value v = ValueLobDb.createTempBlob(x, length); session.addTemporaryLob(v); return v; }
/** * Create a LOB object that fits in memory. * * @param type the type (Value.BLOB or CLOB) * @param small the byte array * @return the LOB */ public static Value createSmallLob(int type, byte[] small) { int precision; if (type == Value.CLOB) { precision = new String(small, Constants.UTF8).length(); } else { precision = small.length; } return createSmallLob(type, small, precision); }
private static String createTempLobFileName() throws IOException { String path = getDatabasePath(); if (path.length() == 0) { path = SysProperties.PREFIX_TEMP_FILE; } return FileUtils.createTempFile(path, Constants.SUFFIX_TEMP_FILE, true, true); }
@Override public byte[] getBytes() { if (type == CLOB) { // convert hex to string return super.getBytes(); } byte[] data = getBytesNoCopy(); return Utils.cloneByteArray(data); }
remaining = length; int len = getBufferSize(remaining); byte[] buff; if (len >= Integer.MAX_VALUE) { len = IOUtils.readFully(in, buff, len); if (len <= getMaxLengthInplaceLob()) { byte[] small = DataUtils.newBytes(len); System.arraycopy(buff, 0, small, 0, len); return ValueLobDb.createSmallLob(Value.BLOB, small, small.length); ValueLobDb lob = new ValueLobDb(buff, len, in, remaining); return lob; } catch (IOException e) {
v = ValueNull.INSTANCE; } else { v = ValueLobDb.createTempClob(new BufferedReader(in), -1); v = (in == null) ? (Value) ValueNull.INSTANCE : ValueLobDb.createTempBlob(in, -1); break;
/** * Create a new empty Clob object. * * @return the object */ @Override public Clob createClob() throws SQLException { try { int id = getNextId(TraceObject.CLOB); debugCodeAssign("Clob", TraceObject.CLOB, id, "createClob()"); checkClosed(); try { Value v = ValueLobDb.createTempClob(new InputStreamReader(new ByteArrayInputStream(Utils.EMPTY_BYTES)), 0); session.addTemporaryLob(v); return new JdbcClob(this, v, id); } finally { afterWriting(); } } catch (Exception e) { throw logAndConvert(e); } }
/** * Create a new empty Blob object. * * @return the object */ @Override public Blob createBlob() throws SQLException { try { int id = getNextId(TraceObject.BLOB); debugCodeAssign("Blob", TraceObject.BLOB, id, "createClob()"); checkClosed(); try { Value v = ValueLobDb.createTempBlob( new ByteArrayInputStream(Utils.EMPTY_BYTES), 0); session.addTemporaryLob(v); return new JdbcBlob(this, v, id); } finally { afterWriting(); } } catch (Exception e) { throw logAndConvert(e); } }
/** * Create a BLOB in a temporary file. */ private ValueLobDb(byte[] buff, int len, InputStream in, long remaining) throws IOException { this.type = Value.BLOB; this.small = null; this.lobId = 0; this.fileName = createTempLobFileName(); RandomAccessFile tempFile = new RandomAccessFile(fileName, "rw"); long tmpPrecision = 0; try { while (true) { tmpPrecision += len; tempFile.write(buff, 0, len); remaining -= len; if (remaining <= 0) { break; } len = getBufferSize(remaining); len = IOUtils.readFully(in, buff, len); if (len <= 0) { break; } } } finally { tempFile.close(); } this.precision = tmpPrecision; }
switch (getType()) { case BYTES: return ValueLobDb.createSmallLob( Value.BLOB, getBytesNoCopy()); return ValueFloat.get(Float.parseFloat(s.trim())); case CLOB: return ValueLobDb.createSmallLob( CLOB, s.getBytes(Constants.UTF8)); case BLOB: return ValueLobDb.createSmallLob( BLOB, StringUtils.convertHexToBytes(s.trim())); case ARRAY:
/** * Convert a lob to another data type. The data is fully read in memory * except when converting to BLOB or CLOB. * * @param t the new type * @return the converted value */ @Override public Value convertTo(int t) { if (t == type) { return this; } else if (t == Value.CLOB) { if (small != null) { return ValueLobDb.createSmallLob(t, small); } else { return ValueLobDb.createTempClob(getReader(), -1); } } else if (t == Value.BLOB) { if (small != null) { return ValueLobDb.createSmallLob(t, small); } else { return ValueLobDb.createTempBlob(getInputStream(), -1); } } return super.convertTo(t); }