// 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;
/*
 * Copyright 2017, Yahoo! Inc. Licensed under the terms of the
 * Apache License 2.0. See LICENSE file at the project root for terms.
 */

package com.yahoo.memory4;




/**
 * Allocates direct memory used to memory map files for read operations
 * (including those &gt; 2GB).
 *
 * @author Praveenkumar Venkatesan
 * @author Lee Rhodes
 */
final class AllocateDirectMap extends WritableMemoryImpl implements ResourceHandler {
  //private RandomAccessFile randomAccessFile = null; //only used in writable version
  private MappedByteBuffer dummyMbbInstance = null;
  private final Cleaner cleaner;

  private AllocateDirectMap(final MemoryState state, final RandomAccessFile raf,
      final MappedByteBuffer mbb) {
    super(state);
    //this.randomAccessFile = raf; //only used in writable version
    this.dummyMbbInstance = mbb;
    this.cleaner = Cleaner.create(this,
        new Deallocator(raf, state));
  }

  /**
   * Factory method for memory mapping a file. This should be called only if read access
   * is desired.
   *
   * <p>Memory maps a file directly in off heap leveraging native map0 method used in
   * FileChannelImpl.c. The owner will have read access to that address space.</p>
   *
   * @param file File to be mapped
   * @param offset Memory map starting from this offset in the file
   * @param capacity Memory map at most capacity bytes &gt; 0 starting from {@code offset}
   * @param readOnlyRequest true if requesting method requests read-only interface.
   * @return A new MemoryMappedFile
   * @throws Exception file not found or RuntimeException, etc.
   */
  @SuppressWarnings("resource")
  static AllocateDirectMap map(final MemoryState state) throws Exception {
    final long fileOffset = state.getFileOffset();
    final long capacity = state.getCapacity();
    checkOffsetAndCapacity(fileOffset, capacity);

    final File file = state.getFile();

// 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;

/*
 * Copyright 2017, Yahoo! Inc. Licensed under the terms of the
 * Apache License 2.0. See LICENSE file at the project root for terms.
 */

package com.yahoo.memory4;




/**
 * Allocates direct memory used to memory map files for read operations
 * (including those &gt; 2GB).
 *
 * @author Praveenkumar Venkatesan
 * @author Lee Rhodes
 */
final class AllocateDirectMap extends WritableMemoryImpl implements ResourceHandler {
  //private RandomAccessFile randomAccessFile = null; //only used in writable version
  private MappedByteBuffer dummyMbbInstance = null;
  private final Cleaner cleaner;

  private AllocateDirectMap(final MemoryState state, final RandomAccessFile raf,
      final MappedByteBuffer mbb) {
    super(state);
    //this.randomAccessFile = raf; //only used in writable version
    this.dummyMbbInstance = mbb;
    this.cleaner = Cleaner.create(this,
        new Deallocator(raf, state));
  }

  /**
   * Factory method for memory mapping a file. This should be called only if read access
   * is desired.
   *
   * <p>Memory maps a file directly in off heap leveraging native map0 method used in
   * FileChannelImpl.c. The owner will have read access to that address space.</p>
   *
   * @param file File to be mapped
   * @param offset Memory map starting from this offset in the file
   * @param capacity Memory map at most capacity bytes &gt; 0 starting from {@code offset}
   * @param readOnlyRequest true if requesting method requests read-only interface.
   * @return A new MemoryMappedFile
   * @throws Exception file not found or RuntimeException, etc.
   */
  @SuppressWarnings("resource")
  static AllocateDirectMap map(final MemoryState state) throws Exception {
    final long fileOffset = state.getFileOffset();
    final long capacity = state.getCapacity();
    checkOffsetAndCapacity(fileOffset, capacity);

    final File file = state.getFile();

    if (isFileReadOnly(file)) {