public class MappedFileBuffer extends Object implements BufferFacade, Cloneable
ByteBuffer, while supporing files larger than 2 GB. Unlike
normal byte buffers, all access via absolute index, and indexes are
long values.
This is achieved using a set of overlapping buffers, based on the "segment
size" passed during construction. Segment size is the largest contiguous
sub-buffer that may be accessed (via getBytes(long, int) and putBytes(long, byte[])),
and may be no larger than 1 GB.
Warning:
This class is not thread-safe. Caller must explicitly synchronize access,
or call clone() to create a distinct buffer for each thread.
| Constructor and Description |
|---|
MappedFileBuffer(File file)
Opens and memory-maps the specified file for read-only access, using
the maximum segment size.
|
MappedFileBuffer(File file,
boolean readWrite)
Opens and memory-maps the specified file for read-only or read-write
access, using the maximum segment size.
|
MappedFileBuffer(File file,
int segmentSize,
boolean readWrite)
Opens and memory-maps the specified file, for read-only or read-write
access, with a specified segment size.
|
| Modifier and Type | Method and Description |
|---|---|
protected ByteBuffer |
buffer(long index) |
long |
capacity()
Returns the buffer's capacity -- the size of the mapped file.
|
MappedFileBuffer |
clone()
Creates a new buffer referencing the same file, but with a copy of the
original underlying mappings.
|
File |
file()
Returns the file that is mapped by this buffer.
|
void |
force()
Iterates through the underlying buffers, calling
force()
on each; this will cause the buffers' contents to be written to disk. |
byte |
get(long index)
Retrieves a single byte from the specified index.
|
ByteOrder |
getByteOrder()
Returns the byte-order of this buffer (actually, the order of the first
child buffer; they should all be the same).
|
byte[] |
getBytes(long index,
byte[] array,
int off,
int len)
Retrieves
len bytes starting at the specified index,
storing them in an existing byte[] at the specified
offset. |
byte[] |
getBytes(long index,
int len)
Retrieves
len bytes starting at the specified index,
storing them in a newly created byte[]. |
char |
getChar(long index)
Retrieves a two-byte character starting at the specified index (note
that a Unicode code point may require calling this method twice).
|
double |
getDouble(long index)
Retrieves an eight-byte floating-point number starting at the specified
index.
|
float |
getFloat(long index)
Retrieves a four-byte floating-point number starting at the specified
index.
|
int |
getInt(long index)
Retrieves a four-byte integer starting at the specified index.
|
long |
getLong(long index)
Retrieves an eight-byte integer starting at the specified index.
|
short |
getShort(long index)
Retrieves a four-byte integer starting at the specified index.
|
boolean |
isWritable()
Indicates whether this buffer is read-write or read-only.
|
long |
limit()
Returns the buffer's limit -- the maximum index in the buffer + 1.
|
void |
put(long index,
byte value)
Stores a single byte at the specified index.
|
void |
putBytes(long index,
byte[] value)
Stores the contents of the passed byte array, starting at the given index.
|
void |
putBytes(long index,
byte[] value,
int off,
int len)
Stores a section of the passed byte array, defined by
off and
len, starting at the given index. |
void |
putChar(long index,
char value)
Stores a two-byte character starting at the specified index.
|
void |
putDouble(long index,
double value)
Stores an eight-byte floating-point number starting at the specified
index.
|
void |
putFloat(long index,
float value)
Stores a four-byte floating-point number starting at the specified
index.
|
void |
putInt(long index,
int value)
Stores a four-byte integer starting at the specified index.
|
void |
putLong(long index,
long value)
Stores an eight-byte integer starting at the specified index.
|
void |
putShort(long index,
short value)
Stores a four-byte integer starting at the specified index.
|
void |
setByteOrder(ByteOrder order)
Sets the order of this buffer (propagated to all child buffers).
|
ByteBuffer |
slice(long index)
Creates a new buffer, whose size will be >= segment size, starting at
the specified offset.
|
public MappedFileBuffer(File file) throws IOException
file - The file to open; must be accessible to user.IllegalArgumentException - if segmentSize is > 1GB.IOExceptionpublic MappedFileBuffer(File file, boolean readWrite) throws IOException
file - The file to open; must be accessible to user.readWrite - Pass true to open the file with
read-write access, false to open
with read-only access.IllegalArgumentException - if segmentSize is > 1GB.IOExceptionpublic MappedFileBuffer(File file, int segmentSize, boolean readWrite) throws IOException
file - The file to open; must be accessible to user.segmentSize - The largest contiguous sub-buffer that can be
created using slice(long). The maximum size
is 2^30 - 1.readWrite - Pass true to open the file with
read-write access, false to open
with read-only access.IllegalArgumentException - if segmentSize is > 1GB.IOExceptionpublic long capacity()
capacity in interface BufferFacadepublic long limit()
This returns the same value as capacity(); it exists as part of
the BufferFacade interface.
limit in interface BufferFacadepublic File file()
public boolean isWritable()
public ByteOrder getByteOrder()
public void setByteOrder(ByteOrder order)
public byte get(long index)
get in interface BufferFacadepublic void put(long index,
byte value)
put in interface BufferFacadepublic int getInt(long index)
getInt in interface BufferFacadepublic void putInt(long index,
int value)
putInt in interface BufferFacadepublic long getLong(long index)
getLong in interface BufferFacadepublic void putLong(long index,
long value)
putLong in interface BufferFacadepublic short getShort(long index)
getShort in interface BufferFacadepublic void putShort(long index,
short value)
putShort in interface BufferFacadepublic float getFloat(long index)
getFloat in interface BufferFacadepublic void putFloat(long index,
float value)
putFloat in interface BufferFacadepublic double getDouble(long index)
getDouble in interface BufferFacadepublic void putDouble(long index,
double value)
putDouble in interface BufferFacadepublic char getChar(long index)
getChar in interface BufferFacadepublic void putChar(long index,
char value)
putChar in interface BufferFacadepublic byte[] getBytes(long index,
int len)
len bytes starting at the specified index,
storing them in a newly created byte[]. Will span
segments if necessary to retrieve the requested number of bytes.getBytes in interface BufferFacadeIndexOutOfBoundsException - if the request would read past
the end of file.public byte[] getBytes(long index,
byte[] array,
int off,
int len)
len bytes starting at the specified index,
storing them in an existing byte[] at the specified
offset. Returns the array as a convenience. Will span segments as
needed.IndexOutOfBoundsException - if the request would read past
the end of file.public void putBytes(long index,
byte[] value)
putBytes in interface BufferFacadeIndexOutOfBoundsException - if the request would write past
the end of file.public void putBytes(long index,
byte[] value,
int off,
int len)
off and
len, starting at the given index. Will span segments as needed.IndexOutOfBoundsException - if the request would write past
the end of file.public ByteBuffer slice(long index)
slice in interface BufferFacadepublic void force()
force()
on each; this will cause the buffers' contents to be written to disk.
Note, however, that the OS may not physically write the buffers until
a future time.public MappedFileBuffer clone()
protected ByteBuffer buffer(long index)