我正在學習java nio,我正在使用MappedByteBuffer和ExecutorService異步複製文件。我的問題是方法MappedByteBuffer.put()拋出java.nio.BufferOverflowException。但在我的調試中,我沒有複製到我的目標文件上的位置。這是代碼的一部分,我用它來創建該文件的新副本:MappedByteBuffer拋出一個java.nio.BufferOverflowException
for (Future<?> f : futures) {
Message message = (Message) f.get();
try (FileChannel fileChannel = (FileChannel) Files
.newByteChannel(pathWrite, EnumSet.of(
StandardOpenOption.READ,
StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING))) {
MappedByteBuffer mbb = fileChannel.map(
FileChannel.MapMode.READ_WRITE, message.getCod(),
message.getValue());
if (mbb != null) {
System.out.println("start: " + message.getCod()
+ " - end: " + message.getValue());
ByteBuffer encode = Charset.forName(charEncoding)
.encode(message.getCharBuffer());
mbb.put(encode); // here
}
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
和例外是:
java.nio.BufferOverflowException
at java.nio.DirectByteBuffer.put(DirectByteBuffer.java:363)
at java.nio.DirectByteBuffer.put(DirectByteBuffer.java:342)
at memo.MultiThreadingMappedByteBufferExample.multiThreadedWayWithExecutorService(MultiThreadingMappedByteBufferExample.java:139)
正如我正在學習java nio我想使用它的方式來提高使用併發和異步作業的大型程序的性能。這只是第一次嘗試幫助我進行大嘗試。如果你對MappedByteBuffer有一些例子,它會對我非常有幫助。不管怎麼說,還是要謝謝你。 – 2015-02-10 00:56:25
它不會改善性能。它會使情況變得更糟。 [Javadoc](https://docs.oracle.com/javase/7/docs/api/java/nio/channels/FileChannel.html#map(java.nio.channels.FileChannel.MapMode,%20long,%20long ))這樣說:「對於大多數操作系統來說,將文件映射到內存中比使用通常的讀寫方法讀取或寫入幾十千字節的數據要昂貴。從性能的角度來看,它通常只值得相對映射大文件進入內存「。如果你只是在學習Java,那麼你應該首先以簡單的方式做事。 – EJP 2015-02-10 01:00:05