@Override public void unlink(DataHandler database) { if (small == null && tableId != LobStorage.TABLE_ID_SESSION_VARIABLE) { database.getLobStorage().setTable(this, LobStorage.TABLE_ID_SESSION_VARIABLE); tableId = LobStorage.TABLE_ID_SESSION_VARIABLE; } }
private static int getBufferSize(DataHandler handler, boolean compress, long remaining) { if (remaining < 0 || remaining > Integer.MAX_VALUE) { remaining = Integer.MAX_VALUE; } int inplace = handler.getMaxLengthInplaceLob(); long m = compress ? Constants.IO_BUFFER_SIZE_COMPRESS : Constants.IO_BUFFER_SIZE; if (m < remaining && m <= inplace) { // using "1L" to force long arithmetic because // inplace could be Integer.MAX_VALUE m = Math.min(remaining, inplace + 1L); // the buffer size must be bigger than the inplace lob, otherwise we // can't know if it must be stored in-place or not m = MathUtils.roundUpLong(m, Constants.IO_BUFFER_SIZE); } m = Math.min(remaining, m); m = MathUtils.convertLongToInt(m); if (m < 0) { m = Integer.MAX_VALUE; } return (int) m; }
private void checkPowerOff() { if (handler != null) { handler.checkPowerOff(); } }
@Override public void close() { if (fileName != null) { if (tempFile != null) { tempFile.stopAutoDelete(); } // synchronize on the database, to avoid concurrent temp file // creation / deletion / backup synchronized (handler.getLobSyncObject()) { FileUtils.delete(fileName); } } if (handler != null) { handler.getLobStorage().removeLob(this); } }
@Override public InputStream getInputStream() { if (small != null) { return new ByteArrayInputStream(small); } else if (fileName != null) { FileStorage fileStorage = handler.openFile(fileName, "r", true); boolean alwaysClose = SysProperties.LOB_CLOSE_BETWEEN_READS; return new BufferedInputStream(new FileStorageInputStream(fileStorage, handler, false, alwaysClose), Constants.IO_BUFFER_SIZE); } long byteCount = (type == Value.BLOB) ? precision : -1; try { return handler.getLobStorage().getInputStream(this, hmac, byteCount); } catch (IOException e) { throw DbException.convertIOException(e, toString()); } }
@Override public Value link(DataHandler database, int tabId) { if (small == null) { if (tableId == LobStorage.TABLE_TEMP) { database.getLobStorage().setTable(this, tabId); this.tableId = tabId; } else { return handler.getLobStorage().copyLob(this, tabId, getPrecision()); } } else if (small.length > database.getMaxLengthInplaceLob()) { LobStorage s = database.getLobStorage(); Value v; if (type == Value.BLOB) { v = s.createBlob(getInputStream(), getPrecision()); } else { v = s.createClob(getReader(), getPrecision()); } return v.link(database, tabId); } return this; }
boolean compress = handler.getLobCompressionAlgorithm(Value.CLOB) != null; long remaining = Long.MAX_VALUE; if (length >= 0 && length < remaining) { len = IOUtils.readFully(reader, buff, len); if (len <= handler.getMaxLengthInplaceLob()) { byte[] small = new String(buff, 0, len).getBytes(Constants.UTF8); return ValueLob.createSmallLob(Value.CLOB, small, len);
this.hmac = null; this.fileName = createTempLobFileName(handler); this.tempFile = this.handler.openFile(fileName, "rw", false); this.tempFile.autoDelete(); FileStorageOutputStream out = new FileStorageOutputStream(tempFile, null, null); long tmpPrecision = 0; boolean compress = this.handler.getLobCompressionAlgorithm(Value.BLOB) != null; try { while (true) {
private void checkWritingAllowed() { if (handler != null && checkedWriting) { handler.checkWritingAllowed(); } }
private static String createTempLobFileName(DataHandler handler) throws IOException { String path = handler.getDatabasePath(); if (path.isEmpty()) { path = SysProperties.PREFIX_TEMP_FILE; } return FileUtils.createTempFile(path, Constants.SUFFIX_TEMP_FILE, true, true); }
this.name = name; if (handler != null) { tempFileDeleter = handler.getTempFileDeleter(); } else { tempFileDeleter = null;
/** * Create a CLOB in a temporary file. */ private ValueLob(DataHandler handler, Reader in, long remaining) throws IOException { this.type = Value.CLOB; this.handler = handler; this.small = null; this.lobId = 0; this.hmac = null; this.fileName = createTempLobFileName(handler); this.tempFile = this.handler.openFile(fileName, "rw", false); this.tempFile.autoDelete(); FileStorageOutputStream out = new FileStorageOutputStream(tempFile, null, null); long tmpPrecision = 0; try { char[] buff = new char[Constants.IO_BUFFER_SIZE]; while (true) { int len = getBufferSize(this.handler, false, remaining); len = IOUtils.readFully(in, buff, len); if (len == 0) { break; } } } finally { out.close(); } this.precision = tmpPrecision; }
try { long remaining = Long.MAX_VALUE; boolean compress = handler.getLobCompressionAlgorithm(Value.BLOB) != null; if (length >= 0 && length < remaining) { remaining = length; len = IOUtils.readFully(in, buff, len); if (len <= handler.getMaxLengthInplaceLob()) { byte[] small = DataUtils.newBytes(len); System.arraycopy(buff, 0, small, 0, len);
/** * 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 = session.getDataHandler().getLobStorage().createClob(x, length); return v; }
@Override public Value createBlob(InputStream in, long maxLength) { init(); int type = Value.BLOB; if (maxLength < 0) { maxLength = Long.MAX_VALUE; } int max = (int) Math.min(maxLength, dataHandler.getMaxLengthInplaceLob()); try { if (max != 0 && max < Integer.MAX_VALUE) { BufferedInputStream b = new BufferedInputStream(in, max); b.mark(max); byte[] small = new byte[max]; int len = IOUtils.readFully(b, small, max); if (len < max) { if (len < small.length) { small = Arrays.copyOf(small, len); } return ValueLob.createSmallLob(type, small); } b.reset(); in = b; } return createLob(in, type); } catch (IllegalStateException e) { throw DbException.get(ErrorCode.OBJECT_CLOSED, e); } catch (IOException e) { throw DbException.convertIOException(e, null); } }
/** * 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 = session.getDataHandler().getLobStorage().createBlob(x, length); return v; }
maxLength = Long.MAX_VALUE; int max = (int) Math.min(maxLength, dataHandler.getMaxLengthInplaceLob()); try { if (max != 0 && max < Integer.MAX_VALUE) {
/** * Create an independent copy of this value, * that will be bound to a result. * * @return the value (this for small objects) */ @Override public ValueLob copyToResult() { if (handler == null) { return this; } LobStorage s = handler.getLobStorage(); if (s.isReadOnly()) { return this; } return s.copyLob(this, LobStorage.TABLE_RESULT, getPrecision()); }
/** * Create a new empty Clob object. * * @return the object */ @Override public Clob createClob() throws SQLException { try { int id = getNextTraceId(TraceObject.CLOB); debugCodeAssign("Clob", TraceObject.CLOB, id, "createClob()"); checkClosed(); Value v = session.getDataHandler().getLobStorage() .createClob(new InputStreamReader(new ByteArrayInputStream(Utils.EMPTY_BYTES)), 0); return new JdbcClob(this, v, id); } catch (Exception e) { throw logAndConvert(e); } }
/** * Create a new empty NClob object. * * @return the object */ // ## Java 1.6 ## @Override public NClob createNClob() throws SQLException { try { int id = getNextTraceId(TraceObject.CLOB); debugCodeAssign("NClob", TraceObject.CLOB, id, "createNClob()"); checkClosed(); Value v = session.getDataHandler().getLobStorage() .createClob(new InputStreamReader(new ByteArrayInputStream(Utils.EMPTY_BYTES)), 0); return new JdbcClob(this, v, id); } catch (Exception e) { throw logAndConvert(e); } }