java - 无法使用 MappedByteBuffer 读取块中的文件
问题描述
在读取操作期间,我得到 java.io.IOException: Channel not open for writing - cannot extend file to required size
我编写了一个简单的程序,用 MappedByteBuffer 读取文件。这个想法是根据 API 读取带有区域的文件。但是在执行过程中我得到了异常。我有包含以下内容的测试文件:
简单测试文件!
static void readFileWithChunks(final String file){
final Path pathToFile = Paths.get(file);
LOG.info("Path to file: {}", pathToFile.toString());
try (FileChannel fileChannel = (FileChannel) Files.newByteChannel(pathToFile, EnumSet.of(StandardOpenOption.READ))) {
final long fileSize = fileChannel.size();
LOG.info("Total size of the file: {} bytes", fileSize);
final int maxChunkSize = 4;
long startPosition = 0;
long endPosition = 0;
// main cycle to read chunks of data from file
while (startPosition < fileSize){
if (endPosition + maxChunkSize < fileSize){
endPosition += maxChunkSize;
} else {
endPosition = fileSize;
}
readChunk(fileChannel, startPosition, endPosition);
startPosition = endPosition;
}
} catch (IOException e) {
e.printStackTrace();
}
}
static void readChunk(final FileChannel fileChannel, final long startPosition, final long endPosition) throws IOException {
LOG.info("Start position: {}; End position: {}", startPosition, endPosition);
final MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, startPosition, endPosition);
final int bufferSize = (int) (endPosition - startPosition);
LOG.info("Buffer size: {}", bufferSize);
final byte[] buffer = new byte[bufferSize];
mappedByteBuffer.get(buffer);
LOG.info("Content of the buffer: {}", new String(buffer, StandardCharsets.UTF_8));
}
输出:
OUTPUT
21:39:39.192 [main] com.test.FileReader INFO - Path to file: /user/test/testfile.txt
21:39:39.196 [main] com.test.FileReader INFO - Total size of the file: 15 bytes
21:39:39.196 [main] com.test.FileReader INFO - Start position: 0; End position: 4
21:39:39.198 [main] com.test.FileReader INFO - Buffer size: 4
21:39:39.198 [main] com.test.FileReader INFO - Content of the buffer: Simp
21:39:39.198 [main] com.test.FileReader INFO - Start position: 4; End position: 8
21:39:39.198 [main] com.test.FileReader INFO - Buffer size: 4
21:39:39.198 [main] com.test.FileReader INFO - Content of the buffer: leTe
21:39:39.198 [main] com.test.FileReader INFO - Start position: 8; End position: 12
java.io.IOException: Channel not open for writing - cannot extend file to required size
at sun.nio.ch.FileChannelImpl.map(FileChannelImpl.java:901)
at com.test.FileReader.readChunk(FileReader.java:59)
at com.test.FileReader.readFile(FileReader.java:42)
at com.test.TestClass.main(TestClass.java:14)
解决方案
这是你的问题:
final MappedByteBuffer mappedByteBuffer =
fileChannel.map(
FileChannel.MapMode.READ_ONLY,
startPosition,
endPosition // <-- (endPosition - startPosition)
);
FileChannel.map 采用偏移量 + 长度,而不是开始 + 结束:
public abstract MappedByteBuffer map(MapMode mode,
long position, long size)
throws IOException;
当您将映射视图推进到文件长度之外时,基础文件将被扩展以适应(文件的前一个末端和新的末端之间有未定义的内容)。扩展文件需要写入权限,因此例外。
推荐阅读
- vb.net - ml.net 0.11 试图预测数组/数字序列 - 标签列“”的模式不匹配:预期 R4,得到向量
参数名称:labelCol' - ios - 不确定我是否应该使用实时数据库
- python - 如何检查值是否与python中的类型匹配?
- flutter - 如何在 Flutter 应用中保存登录名和用户信息
- vue.js - 带有子菜单的 Vuetify 导航抽屉
- mysql - MySQL如何在tsv文件中导入不同的字符集
- python - 与 PYod 的真正异常值有多大区别?
- php - Laravel 验证:只允许已知的属性/属性,否则验证失败
- python - Python 将 XML 解析为 CSV 编码问题
- excel - 工作表中的单元格以保护模式锁定,即使它们未标记为锁定