Java 无法使用nio将BLOB写入内存映射文件

Java 无法使用nio将BLOB写入内存映射文件,java,jdbc,blob,memory-mapped-files,Java,Jdbc,Blob,Memory Mapped Files,我想从BLOB字段编写一个备忘录映射文件。该字段可以包含未压缩的gzip或bzip2压缩数据。现在,我已经使用下面的代码读取blob并使用FileOutputStream写入文件,但是我想让它更快 private static void writeBLOB( Statement myStatement, String fileName ) throws SQLException, IOException { // step 1: initialize the LOB column to s

我想从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,但在我的经验中,它通常不会有很大帮助


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

在“我尝试了不同的方法但失败了”中,失败是什么意思?实际上,BLOB的内容被缓存到本地缓存文件中,我的应用程序的多个实例将访问同一个内存映射文件。那么,获得最佳性能的最佳方法是什么?“…尝试了不同的…”我的意思是,我试图通过打开RAF的文件通道,将BLOB内容直接写入内存映射文件。在Java中,通常使用
FileChannel.map
将文件映射到内存中。这可以使用
RandomAccessFile.getChannel().map(…)
完成。所以,也许这就是您要查找的信息,如何在内存中映射文件?谢谢。实际上,在从BLOB编写文件之后,我还使用您提到的随机访问文件打开了通道。但是现在,在从BLOB编写文件时,我使用了OutputStream,而我希望使用任何更快的方法(最好是javanio)从BLOB编写文件。然后,我将像您提到的那样以内存映射文件的形式打开该文件。因此,我想要任何更快的方法将BLOB写入文件,其中BLOB可能也包含压缩数据,在这种情况下,我需要同时解压缩和写入文件。将标题更改为“创建内存映射文件的最快方法”怎么样