0

我想从 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()

任何人都可以帮助我吗?我尝试过不同的方法但失败了。

4

1 回答 1

0

如果您根本不创建文件,您将获得最佳加速:-)

除此之外,为了获得良好的性能,bufferSize应该是 8 * 1024 或更大。您可以使用 NIO,但根据我的经验,它通常不会有太大帮助。

但是有一个错误:您的程序没有考虑到该read方法没有完全读取所有字节。您需要使用myOutputStream.write(byteBuffer, 0, bytesRead);而不是myOutputStream.write(byteBuffer);

于 2013-01-28T13:43:55.370 回答