本文整理汇总了Java中java.nio.channels.FileChannel.MapMode方法的典型用法代码示例。如果您正苦于以下问题:Java FileChannel.MapMode方法的具体用法?Java FileChannel.MapMode怎么用?Java FileChannel.MapMode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.channels.FileChannel
的用法示例。
在下文中一共展示了FileChannel.MapMode方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: processRegions
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
private void processRegions(File file, RegionProcessor regionProcessor, String randomAccessFileMode, FileChannel.MapMode mapMode) throws IOException {
try (
RandomAccessFile randomAccessFile = new RandomAccessFile(file.getAbsolutePath().toFile(), randomAccessFileMode);
FileChannel channel = randomAccessFile.getChannel()
) {
RegionCalculator.calculateForSize(file, file.getSize());
for (Region region : file.getRegions().values()) {
Hasher hasher = Hashing.sha256().newHasher();
MappedByteBuffer mappedByteBuffer = channel.map(mapMode, region.getOffset(), region.getSize());
int sum = regionProcessor.processRegion(region, hasher, mappedByteBuffer);
region.setQuickDigest(sum);
byte[] slowDigest = hasher.hash().asBytes();
region.setSlowDigest(slowDigest);
clientMessageHandler.submitClientRegionMessage(clientId, file, region.getOffset(), region.getSize(), sum, slowDigest);
}
}
}
示例2: RandomAccessMmapObject
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
public RandomAccessMmapObject(final RandomAccessFile randomAccessFile, String mode)
throws IOException, IllegalArgumentException {
if (randomAccessFile.length() > Integer.MAX_VALUE) {
throw new IllegalArgumentException("Only files up to 2GiB in size are supported.");
}
FileChannel.MapMode mapMode;
if (mode.equals("r")) {
mapMode = FileChannel.MapMode.READ_ONLY;
} else {
mapMode = FileChannel.MapMode.READ_WRITE;
}
mFileChannel = randomAccessFile.getChannel();
mByteBuffer = mFileChannel.map(mapMode, 0, randomAccessFile.length());
mByteBuffer.position(0);
mShouldDeleteFileOnRelease = false;
mFile = null;
}