[英]Not able to write BLOB to Memory mapped file using nio
我想從BLOB字段中寫一個備忘錄映射文件。 該字段可以包含未壓縮的gzip或bzip2壓縮數據。 現在,我已經使用下面的代碼讀取blob並使用FileOutputStream寫入文件,但是我想使其更快。
private static void writeBLOB(
Statement myStatement,
String fileName
) throws SQLException, IOException {
// step 1: initialize the LOB column to set the LOB locator
myStatement.executeUpdate(
"INSERT INTO EDR_RRP_INFO(file_name, DEC_EDR_INFO) " +
"VALUES ('" + fileName + "', EMPTY_BLOB())"
);
// step 2: retrieve the row containing the LOB locator
ResultSet blobResultSet = myStatement.executeQuery(
"SELECT DEC_EDR_INFO " +
"FROM EDR_RRP_INFO " +
"WHERE file_name = '" + fileName + "' " +
"FOR UPDATE"
);
blobResultSet.next();
// step 3: create a LOB object and read the LOB locator
BLOB myBlob =
((OracleResultSet) blobResultSet).getBLOB("DEC_EDR_INFO");
// step 4: get the buffer size of the LOB from the LOB object
int bufferSize = myBlob.getBufferSize();
// step 5: create a buffer to hold a block of data from the file
byte [] byteBuffer = new byte[bufferSize];
// step 6: create a file object
File myFile = new File(fileName);
// step 7: create a file input stream object to read
// the file contents
FileInputStream myFileInputStream = new FileInputStream(myFile);
// step 8: create an input stream object and call the appropriate
// LOB object output stream function
OutputStream myOutputStream = 3myBlob.getBinaryOutputStream();
// step 9: while the end of the file has not been reached,
// read a block from the file into the buffer, and write the
// buffer contents to the LOB object via the output stream
int bytesRead;
while ((bytesRead = myFileInputStream.read(byteBuffer)) != -1) {
// write the buffer contents to the output stream
// using the write() method
myOutputStream.write(byteBuffer);
} // end of while
// step 10: close the stream objects
myFileInputStream.close();
myOutputStream.close();
System.out.println("Wrote content from file " +
fileName + " to BLOB");
} // end of writeBLOB()
有人可以幫我嗎? 我嘗試了不同的方法,但是失敗了。
如果根本不創建文件,則將獲得最佳的速度:-)
除此之外,為了獲得良好的性能, bufferSize
應該為8 * 1024或更大。 您可以使用NIO,但通常對我的經驗沒有太大幫助。
但是,有一個錯誤:您的程序沒有考慮到read
方法不能完全讀取所有字節。 您需要使用myOutputStream.write(byteBuffer, 0, bytesRead);
而不是myOutputStream.write(byteBuffer);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.