/** * Fully maps a file read-only in to memory as per {@link * FileChannel#map(java.nio.channels.FileChannel.MapMode, long, long)}. * * <p>Files are mapped from offset 0 to its length. * * <p>This only works for files ≤ {@link Integer#MAX_VALUE} bytes. * * @param file the file to map * @return a read-only buffer reflecting {@code file} * @throws FileNotFoundException if the {@code file} does not exist * @throws IOException if an I/O error occurs * @see FileChannel#map(MapMode, long, long) * @since 2.0 */ public static MappedByteBuffer map(File file) throws IOException { checkNotNull(file); return map(file, MapMode.READ_ONLY); }
/** * Fully maps a file in to memory as per {@link * FileChannel#map(java.nio.channels.FileChannel.MapMode, long, long)} using the requested {@link * MapMode}. * * <p>Files are mapped from offset 0 to its length. * * <p>This only works for files ≤ {@link Integer#MAX_VALUE} bytes. * * @param file the file to map * @param mode the mode to use when mapping {@code file} * @return a buffer reflecting {@code file} * @throws FileNotFoundException if the {@code file} does not exist * @throws IOException if an I/O error occurs * @see FileChannel#map(MapMode, long, long) * @since 2.0 */ public static MappedByteBuffer map(File file, MapMode mode) throws IOException { checkNotNull(file); checkNotNull(mode); if (!file.exists()) { throw new FileNotFoundException(file.toString()); } return map(file, mode, file.length()); }
RandomAccessFile raf = closer.register(new RandomAccessFile(file, mode == MapMode.READ_ONLY ? "r" : "rw")); return map(raf, mode, size); } catch (Throwable e) { throw closer.rethrow(e);