/** * 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 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 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()); }
/** * Fully maps a file read-only in to memory as per * {@link FileChannel#map(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. * * <p>Similar to {@link Files#map(File)}, but returns {@link MappedByteBufferHandler}, that makes it easier to unmap * the buffer within try-with-resources pattern: * <pre>{@code * try (MappedByteBufferHandler fileMappingHandler = FileUtils.map(file)) { * ByteBuffer fileMapping = fileMappingHandler.get(); * // use mapped buffer * }}</pre> * * @param file the file to map * * @return a {@link MappedByteBufferHandler}, wrapping 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(FileChannel.MapMode, long, long) */ public static MappedByteBufferHandler map(File file) throws IOException { MappedByteBuffer mappedByteBuffer = Files.map(file); return new MappedByteBufferHandler(mappedByteBuffer); }
/** * 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);
/** * Returns a mapped buffer of the smooshed file with the given name. Buffer's contents from 0 to capacity() are the * whole mapped file contents, limit() is equal to capacity(). */ public ByteBuffer mapFile(String name) throws IOException { final Metadata metadata = internalFiles.get(name); if (metadata == null) { return null; } final int fileNum = metadata.getFileNum(); while (buffersList.size() <= fileNum) { buffersList.add(null); } MappedByteBuffer mappedBuffer = buffersList.get(fileNum); if (mappedBuffer == null) { mappedBuffer = Files.map(outFiles.get(fileNum)); buffersList.set(fileNum, mappedBuffer); } ByteBuffer retVal = mappedBuffer.duplicate(); retVal.position(metadata.getStartOffset()).limit(metadata.getEndOffset()); return retVal.slice(); }
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);
public void testMap_readWrite_max_value_plus_1() throws IOException { // Setup File file = createTempFile(); // Test try { Files.map(file, MapMode.READ_WRITE, (long) Integer.MAX_VALUE + 1); fail("Should throw when size exceeds Integer.MAX_VALUE"); } catch (IllegalArgumentException expected) { } }
public void testMap() throws IOException { // Test data int size = 1024; byte[] bytes = newPreFilledByteArray(size); // Setup File file = createTempFile(); Files.write(bytes, file); // Test MappedByteBuffer actual = Files.map(file); // Verify ByteBuffer expected = ByteBuffer.wrap(bytes); assertTrue("ByteBuffers should be equal.", expected.equals(actual)); }
public void testMap_noSuchFile() throws IOException { // Setup File file = createTempFile(); boolean deleted = file.delete(); assertTrue(deleted); // Test try { Files.map(file); fail("Should have thrown FileNotFoundException."); } catch (FileNotFoundException expected) { } }
public void testMap_readWrite() throws IOException { // Test data int size = 1024; byte[] expectedBytes = new byte[size]; byte[] bytes = newPreFilledByteArray(1024); // Setup File file = createTempFile(); Files.write(bytes, file); Random random = new Random(); random.nextBytes(expectedBytes); // Test MappedByteBuffer map = Files.map(file, MapMode.READ_WRITE); map.put(expectedBytes); // Verify byte[] actualBytes = Files.toByteArray(file); assertTrue(Arrays.equals(expectedBytes, actualBytes)); }
@Test public void testUnmapDoesntCrashJVM() throws Exception { final File file = temporaryFolder.newFile("some_mmap_file"); try (final OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) { final byte[] data = new byte[4096]; Arrays.fill(data, (byte) 0x5A); os.write(data); } final MappedByteBuffer mappedByteBuffer = Files.map(file); Assert.assertEquals((byte) 0x5A, mappedByteBuffer.get(0)); ByteBufferUtils.unmap(mappedByteBuffer); ByteBufferUtils.unmap(mappedByteBuffer); }
public void testMap_readWrite_creates() throws IOException { // Test data int size = 1024; byte[] expectedBytes = newPreFilledByteArray(1024); // Setup File file = createTempFile(); boolean deleted = file.delete(); assertTrue(deleted); assertFalse(file.exists()); // Test MappedByteBuffer map = Files.map(file, MapMode.READ_WRITE, size); map.put(expectedBytes); // Verify assertTrue(file.exists()); assertTrue(file.isFile()); assertEquals(size, file.length()); byte[] actualBytes = Files.toByteArray(file); assertTrue(Arrays.equals(expectedBytes, actualBytes)); }
private void updateHash(MessageDigest digest, String extension) throws IOException { File file = getFile(extension); if (file.exists()) { digest.update(Files.map(file, MapMode.READ_ONLY)); } }
public static Slice mapFileReadOnly(File file) throws IOException { return Slice.toUnsafeSlice(Files.map(file)); }
private DirectBuffer mapFrom(File file) throws IOException { ByteBuffer toClean = byteBuffer; byteBuffer = Files.map(file, PRIVATE); DirectByteBufferCleaner.clean(toClean); return this; }
@Override public TftpByteBufferData load(File key) throws Exception { MappedByteBuffer buffer = Files.map(key, FileChannel.MapMode.READ_ONLY); return new TftpByteBufferData(buffer); } });
public void testMap_noSuchFile() throws IOException { // Setup File file = createTempFile(); boolean deleted = file.delete(); assertTrue(deleted); // Test try { Files.map(file); fail("Should have thrown FileNotFoundException."); } catch (FileNotFoundException expected) { } }