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(java.nio.ByteOrder).
| Modifier and Type | Method and Description |
|---|---|
static FileBytes |
allocate(File file)
Allocates a randomAccessFile buffer of unlimited count.
|
static FileBytes |
allocate(File file,
long size)
Allocates a randomAccessFile buffer.
|
static FileBytes |
allocate(File file,
String mode,
long 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(long offset,
long size)
Maps a portion of the randomAccessFile into memory in
FileChannel.MapMode.READ_WRITE mode and returns
a MappedBytes instance. |
MappedBytes |
map(long offset,
long size,
FileChannel.MapMode mode)
Maps a portion of the randomAccessFile into memory and returns a
MappedBytes instance. |
ByteOrder |
order()
Returns the byte order.
|
Bytes |
read(long position,
byte[] bytes,
long offset,
long length)
Reads bytes into the given byte array starting at the given offset up to the given length.
|
Bytes |
read(long position,
Bytes bytes,
long offset,
long length)
Reads bytes into the given byte array starting at the given offset up to the given length.
|
boolean |
readBoolean(long offset)
Reads a 1 byte boolean from the buffer at the given offset.
|
int |
readByte(long offset)
Reads a byte from the buffer at the given offset.
|
char |
readChar(long offset)
Reads a 16-bit character from the buffer at the given offset.
|
double |
readDouble(long offset)
Reads a double-precision 64-bit floating point number from the buffer at the given offset.
|
float |
readFloat(long offset)
Reads a single-precision 32-bit floating point number from the buffer at the given offset.
|
int |
readInt(long offset)
Reads a 32-bit signed integer from the buffer at the given offset.
|
long |
readLong(long offset)
Reads a 64-bit signed integer from the buffer at the given offset.
|
int |
readMedium(long offset)
Reads a 24-bit signed integer from the buffer at the given offset.
|
short |
readShort(long offset)
Reads a 16-bit signed integer from the buffer at the given offset.
|
String |
readString(long offset)
Reads a string from the buffer at the given offset.
|
int |
readUnsignedByte(long offset)
Reads an unsigned byte from the buffer at the given offset.
|
long |
readUnsignedInt(long offset)
Reads a 32-bit unsigned integer from the buffer at the given offset.
|
int |
readUnsignedMedium(long offset)
Reads a 24-bin unsigned integer from the buffer at the given offset.
|
int |
readUnsignedShort(long offset)
Reads a 16-bit unsigned integer from the buffer at the given offset.
|
String |
readUTF8(long offset)
Reads a UTF-8 string from the buffer at the given offset.
|
Bytes |
resize(long newSize)
Resizes the bytes.
|
long |
size()
Returns the count of the bytes.
|
Bytes |
write(long position,
byte[] bytes,
long offset,
long length)
Writes an array of bytes to the buffer.
|
Bytes |
write(long position,
Bytes bytes,
long offset,
long length)
Writes an array of bytes to the buffer.
|
Bytes |
writeBoolean(long offset,
boolean b)
Writes a 1 byte boolean to the buffer at the given offset.
|
Bytes |
writeByte(long offset,
int b)
Writes a byte to the buffer at the given offset.
|
Bytes |
writeChar(long offset,
char c)
Writes a 16-bit character to the buffer at the given offset.
|
Bytes |
writeDouble(long offset,
double d)
Writes a double-precision 64-bit floating point number to the buffer at the given offset.
|
Bytes |
writeFloat(long offset,
float f)
Writes a single-precision 32-bit floating point number to the buffer at the given offset.
|
Bytes |
writeInt(long offset,
int i)
Writes a 32-bit signed integer to the buffer at the given offset.
|
Bytes |
writeLong(long offset,
long l)
Writes a 64-bit signed integer to the buffer at the given offset.
|
Bytes |
writeMedium(long offset,
int m)
Writes a 24-bit signed integer to the buffer at the given offset.
|
Bytes |
writeShort(long offset,
short s)
Writes a 16-bit signed integer to the buffer at the given offset.
|
Bytes |
writeString(long offset,
String s)
Writes a string to the buffer at the given offset.
|
Bytes |
writeUnsignedByte(long offset,
int b)
Writes an unsigned byte to the buffer at the given position.
|
Bytes |
writeUnsignedInt(long offset,
long i)
Writes a 32-bit unsigned integer to the buffer at the given offset.
|
Bytes |
writeUnsignedMedium(long offset,
int m)
Writes a 24-bit unsigned integer to the buffer at the given offset.
|
Bytes |
writeUnsignedShort(long offset,
int s)
Writes a 16-bit unsigned integer to the buffer at the given offset.
|
Bytes |
writeUTF8(long offset,
String s)
Writes a UTF-8 string to the buffer at the given offset.
|
Bytes |
zero()
Zeros out all bytes in the array.
|
Bytes |
zero(long offset)
Zeros out all bytes starting at the given offset in the array.
|
Bytes |
zero(long offset,
long length)
Zeros out bytes starting at the given offset up to the given length.
|
isDirect, orderpublic 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, long 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, long 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 long size()
Bytespublic Bytes resize(long 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(long offset, long size)
FileChannel.MapMode.READ_WRITE mode and returns
a MappedBytes 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(long offset, long size, FileChannel.MapMode mode)
MappedBytes 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(long offset)
BytesOutputoffset - The offset at which to start zeroing out bytes.public Bytes zero(long offset, long length)
BytesOutputoffset - The offset at which to start zeroing out bytes.length - THe total number of bytes to zero out.public Bytes read(long position, Bytes bytes, long offset, long 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(long position, byte[] bytes, long offset, long 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(long offset)
BytesInputoffset - The offset at which to read the byte.public int readUnsignedByte(long offset)
BytesInputoffset - The offset at which to read the byte.public char readChar(long offset)
BytesInputoffset - The offset at which to read the character.public short readShort(long offset)
BytesInputoffset - The offset at which to read the short.public int readUnsignedShort(long offset)
BytesInputoffset - The offset at which to read the short.public int readMedium(long offset)
BytesInputoffset - The offset at which to read the integer.public int readUnsignedMedium(long offset)
BytesInputoffset - The offset at which to read the integer.public int readInt(long offset)
BytesInputoffset - The offset at which to read the integer.public long readUnsignedInt(long offset)
BytesInputoffset - The offset at which to read the integer.public long readLong(long offset)
BytesInputoffset - The offset at which to read the long.public float readFloat(long offset)
BytesInputoffset - The offset at which to read the float.public double readDouble(long offset)
BytesInputoffset - The offset at which to read the double.public boolean readBoolean(long offset)
BytesInputoffset - The offset at which to read the boolean.public String readString(long offset)
BytesInputoffset - The offset at which to read the string.public String readUTF8(long offset)
BytesInputoffset - The offset at which to read the string.public Bytes write(long position, Bytes bytes, long offset, long 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(long position, byte[] bytes, long offset, long 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(long offset, int b)
BytesOutputoffset - The offset at which to write the byte.b - The byte to write.public Bytes writeUnsignedByte(long offset, int b)
BytesOutputoffset - The offset at which to write the byte.b - The byte to write.public Bytes writeChar(long offset, char c)
BytesOutputoffset - The offset at which to write the character.c - The character to write.public Bytes writeShort(long offset, short s)
BytesOutputoffset - The offset at which to write the short.s - The short to write.public Bytes writeUnsignedShort(long offset, int s)
BytesOutputoffset - The offset at which to write the short.s - The short to write.public Bytes writeMedium(long offset, int m)
BytesOutputoffset - The offset at which to write the short.m - The short to write.public Bytes writeUnsignedMedium(long offset, int m)
BytesOutputoffset - The offset at which to write the short.m - The short to write.public Bytes writeInt(long offset, int i)
BytesOutputoffset - The offset at which to write the integer.i - The integer to write.public Bytes writeUnsignedInt(long offset, long i)
BytesOutputoffset - The offset at which to write the integer.i - The integer to write.public Bytes writeLong(long offset, long l)
BytesOutputoffset - The offset at which to write the long.l - The long to write.public Bytes writeFloat(long offset, float f)
BytesOutputoffset - The offset at which to write the float.f - The float to write.public Bytes writeDouble(long offset, double d)
BytesOutputoffset - The offset at which to write the double.d - The double to write.public Bytes writeBoolean(long offset, boolean b)
BytesOutputoffset - The offset at which to write the boolean.b - The boolean to write.public Bytes writeString(long offset, String s)
BytesOutputoffset - The offset at which to write the string.s - The string to write.public Bytes writeUTF8(long offset, String s)
BytesOutputoffset - The offset at which to write the string.s - The string to write.public Bytes flush()
BytesOutputpublic void close()
close in interface AutoCloseableclose in interface Bytesclose in class AbstractBytespublic void delete()
Copyright © 2013–2015. All rights reserved.