// Path: src/main/java/com/yahoo/memory4/AllocateDirectWritableMap.java
// static final boolean isFileReadOnly(final File file) {
//   if (System.getProperty("os.name").startsWith("Windows")) {
//     return !file.canWrite();
//   }
//   //All Unix-like OSes
//   final Path path = Paths.get(file.getAbsolutePath());
//   PosixFileAttributes attributes = null;
//   try {
//     attributes = Files.getFileAttributeView(path, PosixFileAttributeView.class).readAttributes();
//   } catch (final IOException e) {
//     // File presence is guaranteed. Ignore
//     e.printStackTrace();
//   }
//   if (attributes != null) {
//     // A file is read-only in Linux only when it has 0444 permissions.
//     // Here we are going to ignore the Owner W,E bits to allow root/owner testing.
//     final Set<PosixFilePermission> permissions = attributes.permissions();
//     int bits = 0;
//     bits |= ((permissions.contains(PosixFilePermission.OWNER_READ))     ? 1 << 8 : 0);
//     //bits |= ((permissions.contains(PosixFilePermission.OWNER_WRITE))    ? 1 << 7 : 0);
//     //bits |= ((permissions.contains(PosixFilePermission.OWNER_EXECUTE))  ? 1 << 6 : 0);
//     bits |= ((permissions.contains(PosixFilePermission.GROUP_READ))     ? 1 << 5 : 0);
//     bits |= ((permissions.contains(PosixFilePermission.GROUP_WRITE))    ? 1 << 4 : 0);
//     bits |= ((permissions.contains(PosixFilePermission.GROUP_EXECUTE))  ? 1 << 3 : 0);
//     bits |= ((permissions.contains(PosixFilePermission.OTHERS_READ))    ? 1 << 2 : 0);
//     bits |= ((permissions.contains(PosixFilePermission.OTHERS_WRITE))   ? 1 << 1 : 0);
//     bits |= ((permissions.contains(PosixFilePermission.OTHERS_EXECUTE)) ? 1      : 0);
//     //System.out.println(Util.zeroPad(Integer.toBinaryString(bits), 32));
//     //System.out.println(Util.zeroPad(Integer.toOctalString(bits), 4));
//     if (bits == 0444) {
//       return true;
//     }
//   }
//   return false;
// }
// 
// Path: src/main/java/com/yahoo/memory4/UnsafeUtil.java
// public static final Unsafe unsafe;


import static com.yahoo.memory4.AllocateDirectWritableMap.isFileReadOnly;
import static com.yahoo.memory4.UnsafeUtil.unsafe;
import java.io.File;
import java.io.FileDescriptor;
import java.io.RandomAccessFile;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import sun.misc.Cleaner;
import sun.nio.ch.FileChannelImpl;
    final long capacity = state.getCapacity();
    checkOffsetAndCapacity(fileOffset, capacity);

    final File file = state.getFile();

    if (isFileReadOnly(file)) {
      state.setResourceReadOnly(); //The file itself could be writable
    }

    final String mode = "rw"; //we can't map it unless we use rw mode
    final RandomAccessFile raf = new RandomAccessFile(file, mode);
    final FileChannel fc = raf.getChannel();
    final long nativeBaseOffset = map(fc, fileOffset, capacity);
    state.putNativeBaseOffset(nativeBaseOffset);

    // len can be more than the file.length
    raf.setLength(capacity);
    final MappedByteBuffer mbb = createDummyMbbInstance(nativeBaseOffset);
    return new AllocateDirectMap(state, raf, mbb);
  }

  @Override
  public Memory get() {
    return this;
  }

  @Override
  public void load() {
    madvise();
    // Read a byte from each page to bring it into memory.
// Path: src/main/java/com/yahoo/memory4/AllocateDirectWritableMap.java
// static final boolean isFileReadOnly(final File file) {
//   if (System.getProperty("os.name").startsWith("Windows")) {
//     return !file.canWrite();
//   }
//   //All Unix-like OSes
//   final Path path = Paths.get(file.getAbsolutePath());
//   PosixFileAttributes attributes = null;
//   try {
//     attributes = Files.getFileAttributeView(path, PosixFileAttributeView.class).readAttributes();
//   } catch (final IOException e) {
//     // File presence is guaranteed. Ignore
//     e.printStackTrace();
//   }
//   if (attributes != null) {
//     // A file is read-only in Linux only when it has 0444 permissions.
//     // Here we are going to ignore the Owner W,E bits to allow root/owner testing.
//     final Set<PosixFilePermission> permissions = attributes.permissions();
//     int bits = 0;
//     bits |= ((permissions.contains(PosixFilePermission.OWNER_READ))     ? 1 << 8 : 0);
//     //bits |= ((permissions.contains(PosixFilePermission.OWNER_WRITE))    ? 1 << 7 : 0);
//     //bits |= ((permissions.contains(PosixFilePermission.OWNER_EXECUTE))  ? 1 << 6 : 0);
//     bits |= ((permissions.contains(PosixFilePermission.GROUP_READ))     ? 1 << 5 : 0);
//     bits |= ((permissions.contains(PosixFilePermission.GROUP_WRITE))    ? 1 << 4 : 0);
//     bits |= ((permissions.contains(PosixFilePermission.GROUP_EXECUTE))  ? 1 << 3 : 0);
//     bits |= ((permissions.contains(PosixFilePermission.OTHERS_READ))    ? 1 << 2 : 0);
//     bits |= ((permissions.contains(PosixFilePermission.OTHERS_WRITE))   ? 1 << 1 : 0);
//     bits |= ((permissions.contains(PosixFilePermission.OTHERS_EXECUTE)) ? 1      : 0);
//     //System.out.println(Util.zeroPad(Integer.toBinaryString(bits), 32));
//     //System.out.println(Util.zeroPad(Integer.toOctalString(bits), 4));
//     if (bits == 0444) {
//       return true;
//     }
//   }
//   return false;
// }
// 
// Path: src/main/java/com/yahoo/memory4/UnsafeUtil.java
// public static final Unsafe unsafe;


// Path: src/main/java/com/yahoo/memory4/AllocateDirectMap.java
import static com.yahoo.memory4.AllocateDirectWritableMap.isFileReadOnly;
import static com.yahoo.memory4.UnsafeUtil.unsafe;
import java.io.File;
import java.io.FileDescriptor;
import java.io.RandomAccessFile;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import sun.misc.Cleaner;
import sun.nio.ch.FileChannelImpl;

    final long capacity = state.getCapacity();
    checkOffsetAndCapacity(fileOffset, capacity);

    final File file = state.getFile();

    if (isFileReadOnly(file)) {
      state.setResourceReadOnly(); //The file itself could be writable
    }

    final String mode = "rw"; //we can't map it unless we use rw mode
    final RandomAccessFile raf = new RandomAccessFile(file, mode);
    final FileChannel fc = raf.getChannel();
    final long nativeBaseOffset = map(fc, fileOffset, capacity);
    state.putNativeBaseOffset(nativeBaseOffset);

    // len can be more than the file.length
    raf.setLength(capacity);
    final MappedByteBuffer mbb = createDummyMbbInstance(nativeBaseOffset);
    return new AllocateDirectMap(state, raf, mbb);
  }

  @Override
  public Memory get() {
    return this;
  }

  @Override
  public void load() {
    madvise();
    // Read a byte from each page to bring it into memory.
    final int ps = unsafe.pageSize();