public class FileBytes extends AbstractBytes
File bytes wrap a simple RandomAccessFile instance to provide random access to a randomAccessFile on local disk. All
operations are delegated directly to the RandomAccessFile interface, and limitations are dependent on the
semantics of the underlying randomAccessFile.
Bytes are always stored in the underlying randomAccessFile in ByteOrder.BIG_ENDIAN order.
To flip the byte order to read or write to/from a randomAccessFile in ByteOrder.LITTLE_ENDIAN order use
Bytes.order(ByteOrder).
| Modifier and Type | Method and Description |
|---|---|
static FileBytes |
allocate(File file)
Allocates a randomAccessFile buffer of unlimited count.
|
static FileBytes |
allocate(File file,
int size)
Allocates a randomAccessFile buffer.
|
static FileBytes |
allocate(File file,
String mode,
int size)
Allocates a randomAccessFile buffer.
|
void |
close() |
void |
delete()
Deletes the underlying file.
|
File |
file()
Returns the underlying file object.
|
Bytes |
flush()
Flushes the bytes to the underlying persistence layer.
|
boolean |
isFile()
Returns a boolean value indicating whether the bytes are backed by a file.
|
MappedBytes |
map(int offset,
int size)
Maps a portion of the randomAccessFile into memory in
FileChannel.MapMode#READ_WRITE mode and returns
a UnsafeMappedBytes instance. |
MappedBytes |
map(int offset,
int size,
FileChannel.MapMode mode)
Maps a portion of the randomAccessFile into memory and returns a
UnsafeMappedBytes instance. |
String |
mode()
Returns the file mode.
|
ByteOrder |
order()
Returns the byte order.
|
Bytes |
read(int position,
byte[] bytes,
int offset,
int length)
Reads bytes into the given byte array starting at the given offset up to the given length.
|
Bytes |
read(int position,
Bytes bytes,
int offset,
int length)
Reads bytes into the given byte array starting at the given offset up to the given length.
|
int |
readByte(int offset)
Reads a byte from the buffer at the given offset.
|
char |
readChar(int offset)
Reads a 16-bit character from the buffer at the given offset.
|
double |
readDouble(int offset)
Reads a double-precision 64-bit floating point number from the buffer at the given offset.
|
float |
readFloat(int offset)
Reads a single-precision 32-bit floating point number from the buffer at the given offset.
|
int |
readInt(int offset)
Reads a 32-bit signed integer from the buffer at the given offset.
|
long |
readLong(int offset)
Reads a 64-bit signed integer from the buffer at the given offset.
|
short |
readShort(int offset)
Reads a 16-bit signed integer from the buffer at the given offset.
|
Bytes |
resize(int newSize)
Resizes the bytes.
|
int |
size()
Returns the count of the bytes.
|
Bytes |
write(int position,
byte[] bytes,
int offset,
int length)
Writes an array of bytes to the buffer.
|
Bytes |
write(int position,
Bytes bytes,
int offset,
int length)
Writes an array of bytes to the buffer.
|
Bytes |
writeByte(int offset,
int b)
Writes a byte to the buffer at the given offset.
|
Bytes |
writeChar(int offset,
char c)
Writes a 16-bit character to the buffer at the given offset.
|
Bytes |
writeDouble(int offset,
double d)
Writes a double-precision 64-bit floating point number to the buffer at the given offset.
|
Bytes |
writeFloat(int offset,
float f)
Writes a single-precision 32-bit floating point number to the buffer at the given offset.
|
Bytes |
writeInt(int offset,
int i)
Writes a 32-bit signed integer to the buffer at the given offset.
|
Bytes |
writeLong(int offset,
long l)
Writes a 64-bit signed integer to the buffer at the given offset.
|
Bytes |
writeShort(int offset,
short s)
Writes a 16-bit signed integer to the buffer at the given offset.
|
Bytes |
zero()
Zeros out all bytes in the array.
|
Bytes |
zero(int offset)
Zeros out all bytes starting at the given offset in the array.
|
Bytes |
zero(int offset,
int length)
Zeros out bytes starting at the given offset up to the given length.
|
isDirect, order, readBoolean, readMedium, readString, readString, readUnsignedByte, readUnsignedInt, readUnsignedMedium, readUnsignedShort, readUTF8, writeBoolean, writeMedium, writeString, writeString, writeUnsignedByte, writeUnsignedInt, writeUnsignedMedium, writeUnsignedShort, writeUTF8public static FileBytes allocate(File file)
The buffer will be allocated with Long.MAX_VALUE bytes. As bytes are written to the buffer, the underlying
RandomAccessFile will expand.
file - The randomAccessFile to allocate.public static FileBytes allocate(File file, int size)
If the underlying randomAccessFile is empty, the randomAccessFile count will expand dynamically as bytes are written to the randomAccessFile.
file - The randomAccessFile to allocate.size - The count of the bytes to allocate.public static FileBytes allocate(File file, String mode, int size)
If the underlying randomAccessFile is empty, the randomAccessFile count will expand dynamically as bytes are written to the randomAccessFile.
file - The randomAccessFile to allocate.mode - The mode in which to open the underlying RandomAccessFile.size - The count of the bytes to allocate.public File file()
public String mode()
public int size()
Bytespublic Bytes resize(int newSize)
Bytes
When the bytes are resized, underlying memory addresses in copies of this instance may no longer be valid. Additionally,
if the newSize is smaller than the current count then some data may be lost during the resize. Use
with caution.
newSize - The count to which to resize this instance.public boolean isFile()
BytesisFile in interface BytesisFile in class AbstractBytespublic MappedBytes map(int offset, int size)
FileChannel.MapMode#READ_WRITE mode and returns
a UnsafeMappedBytes instance.offset - The offset from which to map the randomAccessFile into memory.size - The count of the bytes to map into memory.IllegalArgumentException - If count is greater than the maximum allowed
MappedByteBuffer count: Integer.MAX_VALUEpublic MappedBytes map(int offset, int size, FileChannel.MapMode mode)
UnsafeMappedBytes instance.offset - The offset from which to map the randomAccessFile into memory.size - The count of the bytes to map into memory.mode - The mode in which to map the randomAccessFile into memory.IllegalArgumentException - If count is greater than the maximum allowed
MappedByteBuffer count: Integer.MAX_VALUEpublic ByteOrder order()
Bytes
For consistency with ByteBuffer, all bytes implementations are initially in ByteOrder.BIG_ENDIAN order.
order in interface Bytesorder in class AbstractBytespublic Bytes zero()
BytesOutputpublic Bytes zero(int offset)
BytesOutputoffset - The offset at which to start zeroing out bytes.public Bytes zero(int offset, int length)
BytesOutputoffset - The offset at which to start zeroing out bytes.length - THe total number of bytes to zero out.public Bytes read(int position, Bytes bytes, int offset, int length)
BytesInputposition - The offset from which to start reading bytes.bytes - The byte array into which to read bytes.offset - The offset at which to write bytes into the given buffer.length - The total number of bytes to read.public Bytes read(int position, byte[] bytes, int offset, int length)
BytesInputposition - The offset from which to start reading bytes.bytes - The byte array into which to read bytes.offset - The offset at which to write bytes into the given bufferlength - The total number of bytes to read.public int readByte(int offset)
BytesInputoffset - The offset at which to read the byte.public char readChar(int offset)
BytesInputoffset - The offset at which to read the character.public short readShort(int offset)
BytesInputoffset - The offset at which to read the short.public int readInt(int offset)
BytesInputoffset - The offset at which to read the integer.public long readLong(int offset)
BytesInputoffset - The offset at which to read the long.public float readFloat(int offset)
BytesInputoffset - The offset at which to read the float.public double readDouble(int offset)
BytesInputoffset - The offset at which to read the double.public Bytes write(int position, Bytes bytes, int offset, int length)
BytesOutputposition - The offset at which to start writing the bytes.bytes - The array of bytes to write.offset - The offset at which to start reading bytes from the given source.length - The number of bytes from the provided byte array to write to the buffer.public Bytes write(int position, byte[] bytes, int offset, int length)
BytesOutputposition - The offset at which to start writing the bytes.bytes - The array of bytes to write.offset - The offset at which to start reading bytes from the given source.length - The number of bytes from the provided byte array to write to the buffer.public Bytes writeByte(int offset, int b)
BytesOutputoffset - The offset at which to write the byte.b - The byte to write.public Bytes writeChar(int offset, char c)
BytesOutputoffset - The offset at which to write the character.c - The character to write.public Bytes writeShort(int offset, short s)
BytesOutputoffset - The offset at which to write the short.s - The short to write.public Bytes writeInt(int offset, int i)
BytesOutputoffset - The offset at which to write the integer.i - The integer to write.public Bytes writeLong(int offset, long l)
BytesOutputoffset - The offset at which to write the long.l - The long to write.public Bytes writeFloat(int offset, float f)
BytesOutputoffset - The offset at which to write the float.f - The float to write.public Bytes writeDouble(int offset, double d)
BytesOutputoffset - The offset at which to write the double.d - The double to write.public Bytes flush()
BytesOutputflush in interface BytesOutput<Bytes>flush in class AbstractBytespublic void close()
close in interface Bytesclose in interface AutoCloseableclose in class AbstractBytespublic void delete()
Copyright © 2013–2017. All rights reserved.