设置和问题
MappedByteBuffer太小,将引发java.nio.BufferOverflowException。MappedByteBuffer太大,则结果文件将有尾随零。MappedByteBuffer或切断尾随的零?一个小例子
FileChannel inChan = FileChannel.open(Paths.get(inString), StandardOpenOption.READ);
FileChannel outChan = FileChannel.open(Paths.get(outString), StandardOpenOption.READ, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
MappedByteBuffer inMBB = inChan.map(MapMode.READ_ONLY, 0, inChan.size());
// The following will buffer overflow after the call to transform
MappedByteBuffer outMBB = outChan.map(MapMode.READ_WRITE, 0, 0);
// This will have trailing zeros after the call to transform
MappedByteBuffer outMBB = outChan.map(MapMode.READ_WRITE, 0, inChan.size() + 1024);
transform(inMBB, outMBB);发布于 2017-03-22 09:18:19
要创建不同大小的映射,您需要创建一个新的内存映射。这意味着要么创建更多的映射并将它们拼接在一起,要么创建一个新的更大的映射,注意ByteBuffer仅限于Integer.MAX_VALUE大小。
我们开发了一个随需增长的库MappedBytes (按您定义的块大小),一旦您知道文件长度是什么,您就可以截断它。
例如:
Bytes bytes = MappedBytes.mappedFile(filename, 64 << 20); // 64 MB注意:字节支持>2GB的块/文件,加密和压缩可能很有用。
https://stackoverflow.com/questions/42946012
复制相似问题