私はJava nioを学んでおり、MappedByteBufferとExecutorServiceを使用してファイルを非同期にコピーしています。私の問題は、メソッド MappedByteBuffer.put() が 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)