/** * Create a BLOB value from a stream. * * @param in the input stream * @param length the number of characters to read, or -1 for no limit * @param handler the data handler * @return the lob value */ private static ValueLob createBlob(InputStream in, long length, DataHandler handler) { try { if (handler == null) { byte[] data = IOUtils.readBytesAndClose(in, (int) length); return createSmallLob(Value.BLOB, data); } long remaining = Long.MAX_VALUE; boolean compress = handler.getLobCompressionAlgorithm(Value.BLOB) != null; if (length >= 0 && length < remaining) { remaining = length; } int len = getBufferSize(handler, compress, remaining); byte[] buff; if (len >= Integer.MAX_VALUE) { buff = IOUtils.readBytesAndClose(in, -1); len = buff.length; } else { buff = DataUtils.newBytes(len); len = IOUtils.readFully(in, buff, len); } if (len <= handler.getMaxLengthInplaceLob()) { byte[] small = DataUtils.newBytes(len); System.arraycopy(buff, 0, small, 0, len); return ValueLob.createSmallLob(Value.BLOB, small); } ValueLob lob = new ValueLob(Value.BLOB, null); lob.createFromStream(buff, len, in, remaining, handler); return lob; } catch (IOException e) { throw DbException.convertIOException(e, null); } }
/** * Create a CLOB value from a stream. * * @param in the reader * @param length the number of characters to read, or -1 for no limit * @param handler the data handler * @return the lob value */ private static ValueLob createClob(Reader in, long length, DataHandler handler) { try { if (handler == null) { String s = IOUtils.readStringAndClose(in, (int) length); return createSmallLob(Value.CLOB, s.getBytes(Constants.UTF8)); } boolean compress = handler.getLobCompressionAlgorithm(Value.CLOB) != null; long remaining = Long.MAX_VALUE; if (length >= 0 && length < remaining) { remaining = length; } int len = getBufferSize(handler, compress, remaining); char[] buff; if (len >= Integer.MAX_VALUE) { String data = IOUtils.readStringAndClose(in, -1); buff = data.toCharArray(); len = buff.length; } else { buff = new char[len]; len = IOUtils.readFully(in, buff, len); } if (len <= handler.getMaxLengthInplaceLob()) { byte[] small = new String(buff, 0, len).getBytes(Constants.UTF8); return ValueLob.createSmallLob(Value.CLOB, small); } ValueLob lob = new ValueLob(Value.CLOB, null); lob.createFromReader(buff, len, in, remaining, handler); return lob; } catch (IOException e) { throw DbException.convertIOException(e, null); } }
/** * Create an independent copy of this temporary value. The file will not be deleted automatically. * * @return the value */ @Override public ValueLob copyToTemp() { ValueLob lob; if (type == CLOB) { lob = ValueLob.createClob(getReader(), precision, handler); } else { lob = ValueLob.createBlob(getInputStream(), precision, handler); } return lob; }
@Override public Value convertPrecision(long precision, boolean force) { if (this.precision <= precision) { return this; } ValueLob lob; if (type == CLOB) { lob = ValueLob.createClob(getReader(), precision, handler); } else { lob = ValueLob.createBlob(getInputStream(), precision, handler); } return lob; }
/** * 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) { ValueLob copy = ValueLob.createClob(getReader(), -1, handler); return copy; } else if (t == Value.BLOB) { ValueLob copy = ValueLob.createBlob(getInputStream(), -1, handler); return copy; } return super.convertTo(t); }
private static ValueLob copy(ValueLob lob) { ValueLob copy = new ValueLob( lob.type, lob.handler, lob.fileName, lob.tableId, lob.objectId, lob.linked, lob.precision, lob.compressed); copy.small = lob.small; copy.hash = lob.hash; return copy; }
@Override public Value link(DataHandler h, int tabId) { if (fileName == null) { this.tableId = tabId; return this; } if (linked) { ValueLob copy = ValueLob.copy(this); copy.objectId = getNewObjectId(h); copy.tableId = tabId; String live = getFileName(h, copy.tableId, copy.objectId); copyFileTo(h, fileName, live); copy.fileName = live; copy.linked = true; return copy; } if (!linked) { this.tableId = tabId; String live = getFileName(h, tableId, objectId); if (tempFile != null) { tempFile.stopAutoDelete(); tempFile = null; } renameFile(h, fileName, live); fileName = live; linked = true; } return this; }