oracleocilob.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 954 行 · 第 1/2 页
JAVA
954 行
} else if (_lob instanceof Clob) { return readInternalClob(env, -1); } break; case OracleModule.OCI_D_ROWID: break; } } catch (Exception ex) { log.log(Level.FINE, ex.toString(), ex); } return null; } /** * Reads part of the large object */ @ReturnNullAsFalse public Object read(Env env, long length) { try { switch (_type) { case OracleModule.OCI_D_FILE: break; case OracleModule.OCI_D_LOB: if (_lob instanceof Blob) { return readInternalBlob(env, length); } else if (_lob instanceof Clob) { return readInternalClob(env, length); } break; case OracleModule.OCI_D_ROWID: break; } } catch (Exception ex) { log.log(Level.FINE, ex.toString(), ex); } return null; } /** * Moves the internal pointer to the beginning of the large object */ public boolean rewind(Env env) { return seek(env, 0, OracleModule.OCI_SEEK_SET); } /** * Saves data to the large object */ public boolean save(Env env, @NotNull String data, @Optional("0") long offset) { try { switch (_type) { case OracleModule.OCI_D_FILE: break; case OracleModule.OCI_D_LOB: if (_lob instanceof Blob) { Blob blob = (Blob) _lob; if (_outputStream != null) { _outputStream.close(); } _outputStream = blob.setBinaryStream(offset); _outputStream.write(data.getBytes()); _outputStream.close(); _outputStream = null; } else if (_lob instanceof Clob) { Clob clob = (Clob) _lob; if (_writer != null) { _writer.close(); } _writer = clob.setCharacterStream(offset); _writer.write(data); _writer.close(); // php/4408 _writer = null; } _currentPointer = offset + data.length(); break; case OracleModule.OCI_D_ROWID: break; } return true; } catch (Exception ex) { log.log(Level.FINE, ex.toString(), ex); return false; } } /** * Alias of import() */ public boolean saveFile(Env env, Path file) { return q_import(env, file); } /** * Sets the internal pointer of the large object */ public boolean seek(Env env, long offset, @Optional("-1") int whence) { try { switch (whence) { case OracleModule.OCI_SEEK_SET: _currentPointer = offset; break; case OracleModule.OCI_SEEK_END: long length = 0; if (_lob instanceof Blob) { length = ((Blob) _lob).length(); } else if (_lob instanceof Clob) { length = ((Clob) _lob).length(); } else { L.l("Unable to determine large object's length trying to seek with OCI_SEEK_END"); return false; } _currentPointer = length + offset; break; default: // OCI_SEEK_CUR _currentPointer += offset; } return true; } catch (Exception ex) { log.log(Level.FINE, ex.toString(), ex); return false; } } /** * Changes current state of buffering for the large object */ public boolean setBuffering(Env env, boolean onOff) { // XXX: we assume buffering is always turned on. return true; } /** * Sets the underlying LOB */ protected void setLob(Object lob) { _lob = lob; } /** * Returns size of large object */ @ReturnNullAsFalse public LongValue size(Env env) { try { switch (_type) { case OracleModule.OCI_D_FILE: break; case OracleModule.OCI_D_LOB: if (_lob instanceof Blob) { Blob blob = (Blob) _lob; return LongValue.create(blob.length()); } else if (_lob instanceof Clob) { Clob clob = (Clob) _lob; return LongValue.create(clob.length()); } break; case OracleModule.OCI_D_ROWID: break; } } catch (Exception ex) { log.log(Level.FINE, ex.toString(), ex); } return null; } /** * Returns current position of internal pointer of large object */ @ReturnNullAsFalse public LongValue tell(Env env) { try { return LongValue.create(_currentPointer); } catch (Exception ex) { log.log(Level.FINE, ex.toString(), ex); return null; } } public String toString() { String typeName = "UNKNOWN"; switch (_type) { case OracleModule.OCI_D_FILE: typeName = "OCI_D_FILE"; break; case OracleModule.OCI_D_LOB: typeName = "OCI_D_LOB"; break; case OracleModule.OCI_D_ROWID: typeName = "OCI_D_ROWID"; break; } return "OracleOciLob("+typeName+")"; } /** * Truncates large object */ public boolean truncate(Env env, @Optional("0") long length) { try { switch (_type) { case OracleModule.OCI_D_FILE: break; case OracleModule.OCI_D_LOB: if (_lob instanceof Blob) { Blob blob = (Blob) _lob; blob.truncate(length); } else if (_lob instanceof Clob) { Clob clob = (Clob) _lob; clob.truncate(length); } break; case OracleModule.OCI_D_ROWID: break; } return true; } catch (Exception ex) { log.log(Level.FINE, ex.toString(), ex); return false; } } /** * Writes data to the large object */ @ReturnNullAsFalse public LongValue write(Env env, String data, @Optional("-1") long length) { try { long dataLength = data.length(); if ((length < 0) || (length > dataLength)) { length = dataLength; } switch (_type) { case OracleModule.OCI_D_FILE: break; case OracleModule.OCI_D_LOB: long written = 0; if (_lob instanceof Blob) { Blob blob = (Blob) _lob; if (_outputStream == null) { _outputStream = blob.setBinaryStream(0); } _outputStream.write(data.getBytes()); } else if (_lob instanceof Clob) { Clob clob = (Clob) _lob; if (_writer == null) _writer = clob.setCharacterStream(0); _writer.write(data); } _currentPointer += length; break; case OracleModule.OCI_D_ROWID: break; } } catch (Exception ex) { log.log(Level.FINE, ex.toString(), ex); } return null; } /** * Writes temporary large object */ public boolean writeTemporary(Env env, String data, @Optional("-1") int lobType) { try { if (_type != OracleModule.OCI_D_LOB) { L.l("Unable to write a temporary LOB into a non-lob object"); return false; } if (lobType == OracleModule.OCI_TEMP_BLOB) { _lob = createTemporaryBLOB.invoke(classOracleBLOB, new Object[] {_conn, true, BLOB_DURATION_SESSION}); } else { _lob = createTemporaryCLOB.invoke(classOracleCLOB, new Object[] {_conn, true, CLOB_DURATION_SESSION}); } return true; } catch (Exception ex) { log.log(Level.FINE, ex.toString(), ex); return false; } } /** * Alias of export() */ public boolean writeToFile(Env env, Path file, @Optional("0") long start, @Optional("-1") long length) { return export(env, file, start, length); } private boolean appendInternalBlob(Env env, OracleOciLob lobFrom) { try { Blob blob = (Blob) _lob; long blobLength = blob.length(); if (_currentPointer != blobLength) { if (_outputStream != null) { _outputStream.close(); } _outputStream = blob.setBinaryStream(blobLength); _currentPointer = blobLength; } Blob blobFrom = (Blob) lobFrom; InputStream is = blobFrom.getBinaryStream(); long nbytes; byte buffer[] = new byte[128]; while ((nbytes = is.read(buffer)) > 0) { _outputStream.write(buffer, 0, (int) nbytes); _currentPointer += nbytes; } is.close(); // Keep this output stream open to be reused. return true; } catch (Exception ex) { log.log(Level.FINE, ex.toString(), ex); return false; } } private boolean appendInternalClob(Env env, OracleOciLob lobFrom) { try { Clob clob = (Clob) _lob; long clobLength = clob.length(); if (_currentPointer != clobLength) { if (_writer != null) _writer.close(); _writer = clob.setCharacterStream(clobLength); _currentPointer = clobLength; } Clob clobFrom = (Clob) lobFrom; Reader reader = clobFrom.getCharacterStream(); long nchars; char buffer[] = new char[128]; while ((nchars = reader.read(buffer)) > 0) { _writer.write(buffer, 0, (int) nchars); _currentPointer += nchars; } reader.close(); // Keep this writer open to be reused. return true; } catch (Exception ex) { log.log(Level.FINE, ex.toString(), ex); return false; } } private StringValue readInternalBlob(Env env, long length) { try { StringValue bb = env.createBinaryBuilder(); Blob blob = (Blob) _lob; InputStream is = blob.getBinaryStream(); is.skip(_currentPointer); if (length < 0) length = Integer.MAX_VALUE; bb.appendReadAll(is, length); is.close(); return bb; } catch (Exception ex) { log.log(Level.FINE, ex.toString(), ex); return null; } } private StringValue readInternalClob(Env env, long length) { try { StringValue sb = env.createUnicodeBuilder(); Clob clob = (Clob) _lob; Reader reader = clob.getCharacterStream(); reader.skip(_currentPointer); if (length < 0) length = Integer.MAX_VALUE; sb.append(reader, length); reader.close(); return sb; } catch (Exception ex) { log.log(Level.FINE, ex.toString(), ex); return null; } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?