我想从BLOB字段写一个memoery映射文件。该字段可以包含未压缩,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()
任何人都可以帮助我吗?我试过不同的方法,但失败了。
答案 0 :(得分:0)
如果您根本不创建文件,将获得最佳加速: - )
除此之外,为了获得良好的性能,bufferSize
应为8 * 1024或更大。你可以使用NIO,但通常它对我的经验无济于事。
但是有一个错误:您的程序没有考虑read
方法没有完全读取所有字节。您需要使用myOutputStream.write(byteBuffer, 0, bytesRead);
代替myOutputStream.write(byteBuffer);