in src/main/java/org/apache/datasketches/memory/internal/WritableMemoryImpl.java [169:208]
public static WritableMemory wrapMap(
final File file,
final long fileOffsetBytes,
final long capacityBytes,
final ByteOrder byteOrder,
final boolean localReadOnly,
final Arena arena)
throws IllegalArgumentException, IOException {
Objects.requireNonNull(arena, "Arena must be non-null.");
Objects.requireNonNull(file, "File must be non-null.");
Objects.requireNonNull(byteOrder, "ByteOrder must be non-null.");
final FileChannel.MapMode mapMode;
final boolean fileCanRead = file.canRead();
if (localReadOnly) {
if (fileCanRead) { mapMode = READ_ONLY; }
else { throw new IllegalArgumentException("File must be readable."); }
} else { //!localReadOnly
if (fileCanRead && file.canWrite()) { mapMode = READ_WRITE; }
else {
throw new IllegalArgumentException("File must be readable and writable.");
}
}
final Set<OpenOption> openOptions = READ_WRITE.equals(mapMode)
? Set.of(StandardOpenOption.READ, StandardOpenOption.WRITE)
: Set.of(StandardOpenOption.READ);
final boolean nativeBOType = byteOrder == ByteOrder.nativeOrder();
final int type = MEMORY | MAP | DIRECT
| (localReadOnly ? READONLY : 0)
| (nativeBOType ? NATIVE_BO : NONNATIVE_BO);
try (final FileChannel fileChannel = FileChannel.open(file.toPath(), openOptions)) {
final MemorySegment seg = fileChannel.map(mapMode, fileOffsetBytes, capacityBytes, arena);
return nativeBOType
? new NativeWritableMemoryImpl(seg, type, null, arena)
: new NonNativeWritableMemoryImpl(seg, type, null, arena);
}
}