Java Code Examples for java.nio.channels.FileChannel#MapMode

The following examples show how to use java.nio.channels.FileChannel#MapMode . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IoUtil.java    From agrona with Apache License 2.0 6 votes vote down vote up
/**
 * Check that file exists, open file, and return MappedByteBuffer for entire file for a given
 * {@link java.nio.channels.FileChannel.MapMode}.
 * <p>
 * The file itself will be closed, but the mapping will persist.
 *
 * @param location         of the file to map.
 * @param mapMode          for the mapping.
 * @param descriptionLabel to be associated for any exceptions.
 * @return {@link java.nio.MappedByteBuffer} for the file.
 */
public static MappedByteBuffer mapExistingFile(
    final File location, final FileChannel.MapMode mapMode, final String descriptionLabel)
{
    checkFileExists(location, descriptionLabel);

    MappedByteBuffer mappedByteBuffer = null;
    try (RandomAccessFile file = new RandomAccessFile(location, getFileMode(mapMode));
        FileChannel channel = file.getChannel())
    {
        mappedByteBuffer = channel.map(mapMode, 0, channel.size());
    }
    catch (final IOException ex)
    {
        LangUtil.rethrowUnchecked(ex);
    }

    return mappedByteBuffer;
}
 
Example 2
Source File: FileBytes.java    From atomix with Apache License 2.0 5 votes vote down vote up
private static MappedByteBuffer mapFile(RandomAccessFile randomAccessFile, int offset, int size, FileChannel.MapMode mode) {
  try {
    return randomAccessFile.getChannel().map(mode, offset, size);
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example 3
Source File: MappedMemoryAllocator.java    From catalyst with Apache License 2.0 5 votes vote down vote up
private static String parseMode(FileChannel.MapMode mode) {
  if (mode == FileChannel.MapMode.READ_ONLY) {
    return "r";
  } else if (mode == FileChannel.MapMode.READ_WRITE) {
    return "rw";
  }
  throw new IllegalArgumentException("unsupported map mode");
}
 
Example 4
Source File: MappedResizeableBuffer.java    From agrona with Apache License 2.0 5 votes vote down vote up
/**
 * Remap the buffer based on a new file, mapping mode, offset, and a length
 *
 * @param fileChannel the file to map
 * @param mapMode     for the file when mapping.
 * @param offset      the offset of the file to start the mapping
 * @param length      of the buffer from the given address
 */
public void wrap(
    final FileChannel fileChannel, final FileChannel.MapMode mapMode, final long offset, final long length)
{
    unmap();
    this.fileChannel = fileChannel;
    this.mapMode = mapMode;
    map(offset, length);
}
 
Example 5
Source File: MappedMemoryAllocator.java    From atomix with Apache License 2.0 5 votes vote down vote up
private static String parseMode(FileChannel.MapMode mode) {
  if (mode == FileChannel.MapMode.READ_ONLY) {
    return "r";
  } else if (mode == FileChannel.MapMode.READ_WRITE) {
    return "rw";
  }
  throw new IllegalArgumentException("unsupported map mode");
}
 
Example 6
Source File: MMapFastBitSet.java    From imhotep with Apache License 2.0 5 votes vote down vote up
public MMapFastBitSet(File file, int size, FileChannel.MapMode mapMode) throws IOException {
    this.size = size;
    this.arraySize = (size + 64) >> 6;
    this.bufferLength = arraySize * 8;
    this.buffer = new MMapBuffer(file, 0, bufferLength, mapMode, ByteOrder.LITTLE_ENDIAN);
    this.bits = buffer.memory().longArray(0, arraySize);
}
 
Example 7
Source File: MMapBuffer.java    From util with Apache License 2.0 4 votes vote down vote up
public MMapBuffer(File file, long offset, long length, FileChannel.MapMode mapMode, ByteOrder order) throws IOException {
    this(file.toPath(), offset, length, mapMode, order);
}
 
Example 8
Source File: SafeMappedByteBuffer.java    From tracecompass with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Maps a region of this channel's file directly into memory. On Windows, this
 * will allocate a new ByteBuffer and read the file.
 *
 * @param fc
 *            the file channel
 * @param mode
 *            the mapping mode
 * @param position
 *            the position within the file
 * @param size
 *            the size of the region to be mapped (or read)
 * @return the mapped ByteBuffer
 * @throws IOException
 *             on FileChannel operations failures
 * @throws CTFException
 *             the file mapping refused to map
 */
public static @NonNull ByteBuffer map(FileChannel fc, FileChannel.MapMode mode, long position, long size) throws IOException, CTFException {
    ByteBuffer byteBuffer = null;
    long fileSize = fc.size();
    if (IS_WIN32) {
        byteBuffer = ByteBuffer.allocate((int) size);
        fc.read(byteBuffer, position);
        byteBuffer.flip();
    } else {
        if (position + size <= fileSize) {
            byteBuffer = fc.map(mode, position, size);
        }
        if (byteBuffer == null) {
            throw new CTFException("Failed to allocate mapped byte buffer at " + position + " of size " + size + " on a file of size " + fileSize); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        }
    }
    return byteBuffer;
}
 
Example 9
Source File: MappedMemoryAllocator.java    From atomix with Apache License 2.0 4 votes vote down vote up
public MappedMemoryAllocator(File file, FileChannel.MapMode mode, long offset) {
  this(createFile(file, mode), mode, offset);
}
 
Example 10
Source File: FakeFileSystem.java    From bazel with Apache License 2.0 4 votes vote down vote up
@Override
public MappedByteBuffer map(FileChannel.MapMode mode, long position, long size)
    throws IOException {
  throw new UnsupportedOperationException("Not supported yet.");
}
 
Example 11
Source File: MMapBuffer.java    From util with Apache License 2.0 4 votes vote down vote up
public MMapBuffer(Path path, FileChannel.MapMode mapMode, ByteOrder order) throws IOException {
    this(path, 0, Files.size(path), mapMode, order);
}
 
Example 12
Source File: MMapBuffer.java    From util with Apache License 2.0 4 votes vote down vote up
public MMapBuffer(File file, FileChannel.MapMode mapMode, ByteOrder order) throws IOException {
    this(file, 0, file.length(), mapMode, order);
}
 
Example 13
Source File: MMapBuffer.java    From util with Apache License 2.0 4 votes vote down vote up
public MMapBuffer(RandomAccessFile raf, File file, long offset, long length, FileChannel.MapMode mapMode, ByteOrder order, boolean closeFile) throws IOException {
    this(raf, file.toPath(), offset, length, mapMode, order, closeFile);
}
 
Example 14
Source File: FakeFileSystem.java    From bazel with Apache License 2.0 4 votes vote down vote up
@Override
public MappedByteBuffer map(FileChannel.MapMode mode, long position, long size)
    throws IOException {
  throw new UnsupportedOperationException("Not supported yet.");
}
 
Example 15
Source File: UnsafeMappedBuffer.java    From catalyst with Apache License 2.0 3 votes vote down vote up
/**
 * Allocates a mapped buffer.
 * <p>
 * Memory will be mapped by opening and expanding the given {@link java.io.File} to the desired {@code count} and mapping the
 * file contents into memory via {@link java.nio.channels.FileChannel#map(java.nio.channels.FileChannel.MapMode, long, long)}.
 * <p>
 * The resulting buffer will have a capacity of {@code initialCapacity}. The underlying {@link UnsafeMappedBytes} will be
 * initialized to the next power of {@code 2}. As bytes are written to the buffer, the buffer's capacity will double
 * as long as {@code maxCapacity > capacity}.
 *
 * @param file The file to map into memory. If the file doesn't exist it will be automatically created.
 * @param mode The mode with which to map the file.
 * @param initialCapacity The initial capacity of the buffer.
 * @param maxCapacity The maximum capacity of the buffer.
 * @return The mapped buffer.
 * @throws NullPointerException If {@code file} is {@code null}
 * @throws IllegalArgumentException If the {@code capacity} or {@code maxCapacity} is greater than
 *         {@link Integer#MAX_VALUE}.
 *
 * @see UnsafeMappedBuffer#allocate(java.io.File)
 * @see UnsafeMappedBuffer#allocate(java.io.File, java.nio.channels.FileChannel.MapMode)
 * @see UnsafeMappedBuffer#allocate(java.io.File, long)
 * @see UnsafeMappedBuffer#allocate(java.io.File, java.nio.channels.FileChannel.MapMode, long)
 * @see UnsafeMappedBuffer#allocate(java.io.File, long, long)
 */
public static UnsafeMappedBuffer allocate(File file, FileChannel.MapMode mode, long initialCapacity, long maxCapacity) {
  if (file == null)
    throw new NullPointerException("file cannot be null");
  if (mode == null)
    mode = MappedMemoryAllocator.DEFAULT_MAP_MODE;
  if (initialCapacity > maxCapacity)
    throw new IllegalArgumentException("initial capacity cannot be greater than maximum capacity");
  if (initialCapacity > MappedMemory.MAX_SIZE)
    throw new IllegalArgumentException("initial capacity for MappedBuffer cannot be greater than " + MappedMemory.MAX_SIZE);
  if (maxCapacity > MappedMemory.MAX_SIZE)
    throw new IllegalArgumentException("maximum capacity for MappedBuffer cannot be greater than " + MappedMemory.MAX_SIZE);
  return new UnsafeMappedBuffer(new UnsafeMappedBytes(file, MappedMemory.allocate(file, mode, Memory.Util.toPow2(initialCapacity))), 0, initialCapacity, maxCapacity);
}
 
Example 16
Source File: MappedResizeableBuffer.java    From agrona with Apache License 2.0 2 votes vote down vote up
/**
 * The {@link java.nio.channels.FileChannel.MapMode} that will be used when mapping the file.
 *
 * @return {@link java.nio.channels.FileChannel.MapMode} that will be used when mapping the file.
 */
public FileChannel.MapMode mapMode()
{
    return mapMode;
}
 
Example 17
Source File: UnsafeMappedBuffer.java    From catalyst with Apache License 2.0 2 votes vote down vote up
/**
 * Allocates a fixed capacity mapped buffer in the given {@link java.nio.channels.FileChannel.MapMode}.
 * <p>
 * Memory will be mapped by opening and expanding the given {@link java.io.File} to the desired {@code capacity} and mapping the
 * file contents into memory via {@link java.nio.channels.FileChannel#map(java.nio.channels.FileChannel.MapMode, long, long)}.
 * <p>
 * The resulting buffer will have a capacity of {@code capacity}. The underlying {@link UnsafeMappedBytes} will be
 * initialized to the next power of {@code 2}.
 *
 * @param file The file to map into memory.
 * @param mode The mode with which to map the file.
 * @param capacity The fixed capacity of the buffer to allocate (in bytes).
 * @return The mapped buffer.
 * @throws NullPointerException If {@code file} is {@code null}
 * @throws IllegalArgumentException If the {@code capacity} is greater than {@link Integer#MAX_VALUE}.
 *
 * @see UnsafeMappedBuffer#allocate(java.io.File)
 * @see UnsafeMappedBuffer#allocate(java.io.File, java.nio.channels.FileChannel.MapMode)
 * @see UnsafeMappedBuffer#allocate(java.io.File, long)
 * @see UnsafeMappedBuffer#allocate(java.io.File, long, long)
 * @see UnsafeMappedBuffer#allocate(java.io.File, java.nio.channels.FileChannel.MapMode, long, long)
 */
public static UnsafeMappedBuffer allocate(File file, FileChannel.MapMode mode, long capacity) {
  return allocate(file, mode, capacity, capacity);
}
 
Example 18
Source File: MappedBuffer.java    From atomix with Apache License 2.0 2 votes vote down vote up
/**
 * Allocates a fixed capacity mapped buffer in the given {@link FileChannel.MapMode}.
 * <p>
 * Memory will be mapped by opening and expanding the given {@link File} to the desired {@code capacity} and mapping the
 * file contents into memory via {@link FileChannel#map(FileChannel.MapMode, long, long)}.
 * <p>
 * The resulting buffer will have a capacity of {@code capacity}. The underlying {@link MappedBytes} will be
 * initialized to the next power of {@code 2}.
 *
 * @param file     The file to map into memory.
 * @param mode     The mode with which to map the file.
 * @param capacity The fixed capacity of the buffer to allocate (in bytes).
 * @return The mapped buffer.
 * @throws NullPointerException     If {@code file} is {@code null}
 * @throws IllegalArgumentException If the {@code capacity} is greater than {@link Integer#MAX_VALUE}.
 * @see #allocate(File)
 * @see #allocate(File, FileChannel.MapMode)
 * @see #allocate(File, int)
 * @see #allocate(File, int, int)
 * @see #allocate(File, FileChannel.MapMode, int, int)
 */
public static MappedBuffer allocate(File file, FileChannel.MapMode mode, int capacity) {
  return allocate(file, mode, capacity, capacity);
}
 
Example 19
Source File: FileBuffer.java    From catalyst with Apache License 2.0 2 votes vote down vote up
/**
 * Maps a portion of the underlying file into memory starting at the current position up to the given {@code count}.
 *
 * @param size The count of the bytes to map into memory.
 * @param mode The mode in which to map the bytes into memory.
 * @return The mapped buffer.
 * @throws IllegalArgumentException If {@code count} is greater than the maximum allowed
 *         {@link java.nio.MappedByteBuffer} count: {@link Integer#MAX_VALUE}
 */
public UnsafeMappedBuffer map(long size, FileChannel.MapMode mode) {
  return map(position(), size, mode);
}
 
Example 20
Source File: UnsafeMappedBuffer.java    From catalyst with Apache License 2.0 2 votes vote down vote up
/**
 * Allocates a dynamic capacity mapped buffer in {@link java.nio.channels.FileChannel.MapMode#READ_WRITE} mode with an initial capacity
 * of {@code 16MiB} and a maximum capacity of {@link Integer#MAX_VALUE}.
 * <p>
 * The resulting buffer will be initialized to a capacity of {@code 4096} and have a maximum capacity of
 * {@link Integer#MAX_VALUE}. As bytes are written to the buffer its capacity will double in count each time the current
 * capacity is reached. Memory will be mapped by opening and expanding the given {@link java.io.File} to the desired
 * {@code capacity} and mapping the file contents into memory via
 * {@link java.nio.channels.FileChannel#map(java.nio.channels.FileChannel.MapMode, long, long)}.
 *
 * @param file The file to map into memory.
 * @param mode The mode with which to map the file.
 * @return The mapped buffer.
 * @throws NullPointerException If {@code file} is {@code null}
 *
 * @see UnsafeMappedBuffer#allocate(java.io.File)
 * @see UnsafeMappedBuffer#allocate(java.io.File, long)
 * @see UnsafeMappedBuffer#allocate(java.io.File, java.nio.channels.FileChannel.MapMode, long)
 * @see UnsafeMappedBuffer#allocate(java.io.File, long, long)
 * @see UnsafeMappedBuffer#allocate(java.io.File, java.nio.channels.FileChannel.MapMode, long, long)
 */
public static UnsafeMappedBuffer allocate(File file, FileChannel.MapMode mode) {
  return allocate(file, mode, DEFAULT_INITIAL_CAPACITY, Long.MAX_VALUE);
}