Skip to content

Commit

Permalink
[Java] Add the ability to set a mapping mode for MappedResizableBuffe…
Browse files Browse the repository at this point in the history
…r. Issue #168.
  • Loading branch information
mjpt777 committed Feb 21, 2019
1 parent b38614b commit 497f9af
Showing 1 changed file with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ public class MappedResizeableBuffer implements AutoCloseable
private long addressOffset;
private long capacity;
private FileChannel fileChannel;
private FileChannel.MapMode mapMode;

/**
* Attach a view to an off-heap memory region by address.
* Attach a view to an off-heap memory region by address. Defaults to {@link FileChannel.MapMode#READ_WRITE}.
*
* @param fileChannel the file to map
* @param offset the offset of the file to start the mapping
Expand All @@ -53,6 +54,23 @@ public class MappedResizeableBuffer implements AutoCloseable
public MappedResizeableBuffer(final FileChannel fileChannel, final long offset, final long initialLength)
{
this.fileChannel = fileChannel;
this.mapMode = FileChannel.MapMode.READ_WRITE;
map(offset, initialLength);
}

/**
* Attach a view to an off-heap memory region by address.
*
* @param fileChannel the file to map
* @param mapMode for the mapping
* @param offset the offset of the file to start the mapping
* @param initialLength of the buffer from the given address
*/
public MappedResizeableBuffer(
final FileChannel fileChannel, final FileChannel.MapMode mapMode, final long offset, final long initialLength)
{
this.fileChannel = fileChannel;
this.mapMode = mapMode;
map(offset, initialLength);
}

Expand Down Expand Up @@ -89,7 +107,7 @@ public void wrap(final long offset, final long length)
}

/**
* Remap the buffer based on a new file, offset and a length
* Remap the buffer based on a new file, offset, and a length
*
* @param fileChannel the file to map
* @param offset the offset of the file to start the mapping
Expand All @@ -102,6 +120,23 @@ public void wrap(final FileChannel fileChannel, final long offset, final long le
map(offset, length);
}

/**
* 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);
}

/**
* Address offset in memory at which the mapping begins.
*
Expand All @@ -122,6 +157,16 @@ public FileChannel fileChannel()
return fileChannel;
}

/**
* The {@link FileChannel.MapMode} that will be used when mapping the file.
*
* @return {@link FileChannel.MapMode} that will be used when mapping the file.
*/
public FileChannel.MapMode mapMode()
{
return mapMode;
}

public void setMemory(final long index, final int length, final byte value)
{
if (SHOULD_BOUNDS_CHECK)
Expand Down Expand Up @@ -934,7 +979,7 @@ private void boundsCheck0(final long index, final int length)
private void map(final long offset, final long length)
{
capacity = length;
addressOffset = IoUtil.map(fileChannel, FileChannel.MapMode.READ_WRITE, offset, length);
addressOffset = IoUtil.map(fileChannel, mapMode, offset, length);
}

private void unmap()
Expand Down

0 comments on commit 497f9af

Please sign in to comment.