java.lang.Object
net.metanotion.io.RAIFile
- All Implemented Interfaces:
Closeable, DataInput, DataOutput, AutoCloseable, RandomAccessInterface
Implementation of RandomAccessInterface using
RandomAccessFile. Provides file-based random access with DataInput and DataOutput capabilities.Field Summary
Constructor Summary
ConstructorsConstructorDescriptionOpens the file, deriving the open mode from the flags.RAIFile(RandomAccessFile file) Wraps an open file, treating it as readable and writable.Method Summary
All MethodsModifier and TypeMethodDescriptionbooleancanWrite()Is the file writable? (I2P) Only valid if the File constructor was used, not the RAF constructorvoidclose()Closes the file.longCurrent read/write position.longlength()File length in bytes.intread()Reads one byte and advances the file pointer past it.intread(byte[] b) Reads bytes into b and advances the file pointer past them.intread(byte[] b, int off, int len) Reads bytes into b at off and advances the file pointer past them.booleanReads a single byte, reporting it as a boolean.bytereadByte()Reads one byte as a signed value.charreadChar()Reads two bytes as a big-endian char.doubleReads eight bytes as a big-endian double.floatReads four bytes as a big-endian float.voidreadFully(byte[] b) Fills b, failing unless every byte of it is read.voidreadFully(byte[] b, int off, int len) Fills len bytes of b at off, failing unless all are read.intreadInt()Reads four bytes as a big-endian int.readLine()Reads bytes up to a line terminator, discarding the terminator.longreadLong()Reads eight bytes as a big-endian long.shortReads two bytes as a big-endian short.intReads one byte, widening it to an unsigned int.intReads an unsigned 32-bit integer from the file.intReads two bytes, widening them to an unsigned int.readUTF()Read a UTF encoded string with 4-byte length prefix.voidseek(long pos) Moves the file pointer to an absolute offset.voidsetLength(long newLength) Truncates or extends the file to the given size.intskipBytes(int n) Advances the file pointer without reading the bytes skipped.toString()File name for debugging.voidwrite(byte[] b) Writes every byte of b.voidwrite(byte[] b, int off, int len) Writes len bytes from b, starting at off.voidwrite(int b) Writes the low eight bits of b.voidwriteBoolean(boolean v) Writes a single byte holding 1 for true, 0 for false.voidwriteByte(int v) Writes the low eight bits of v.voidwriteBytes(String s) Writes the low byte of each character of s.voidwriteChar(int v) Writes the low sixteen bits of v as a big-endian char.voidwriteChars(String s) Writes each character of s as two big-endian bytes.voidwriteDouble(double v) Writes v as eight big-endian bytes.voidwriteFloat(float v) Writes v as four big-endian bytes.voidwriteInt(int v) Writes v as four big-endian bytes.voidwriteLong(long v) Writes v as eight big-endian bytes.voidwriteShort(int v) Writes the low sixteen bits of v as a big-endian short.voidWrite a UTF encoded string with 4-byte length prefix.
Field Details
f
The file on disk.delegate
The access this class forwards to.r
private final boolean rTrue if the file was opened for reading.w
private final boolean wTrue if the file was opened for writing.
Constructor Details
RAIFile
Wraps an open file, treating it as readable and writable.- Parameters:
file- the underlying file- Throws:
FileNotFoundException- if the file cannot be opened
RAIFile
Opens the file, deriving the open mode from the flags.- Parameters:
file- the file to openread- must be truewrite- true to open the file for writing- Throws:
FileNotFoundException- if the file cannot be opened
Method Details
canWrite
public boolean canWrite()Is the file writable? (I2P) Only valid if the File constructor was used, not the RAF constructor- Specified by:
canWritein interfaceRandomAccessInterface- Returns:
- true if the file was opened for writing
toString
getFilePointer
Current read/write position.- Specified by:
getFilePointerin interfaceRandomAccessInterface- Returns:
- the file pointer
- Throws:
IOException- if the pointer cannot be determined
length
File length in bytes.- Specified by:
lengthin interfaceRandomAccessInterface- Returns:
- the file length
- Throws:
IOException- if the length cannot be determined
read
Reads one byte and advances the file pointer past it.- Specified by:
readin interfaceRandomAccessInterface- Returns:
- the byte read, or -1 at the end of file
- Throws:
IOException- if the file cannot be read- See Also:
read
Reads bytes into b and advances the file pointer past them.- Specified by:
readin interfaceRandomAccessInterface- Parameters:
b- the buffer to fill- Returns:
- the number of bytes read, or -1 at the end of file
- Throws:
IOException- if the file cannot be read- See Also:
read
Reads bytes into b at off and advances the file pointer past them.- Specified by:
readin interfaceRandomAccessInterface- Parameters:
b- the buffer to filloff- the index in b at which to start writinglen- the maximum number of bytes to read- Returns:
- the number of bytes read, or -1 at the end of file
- Throws:
IOException- if the file cannot be read- See Also:
seek
Moves the file pointer to an absolute offset.- Specified by:
seekin interfaceRandomAccessInterface- Parameters:
pos- the absolute offset, in bytes, to position at- Throws:
IOException- if pos is negative or the seek fails- See Also:
setLength
Truncates or extends the file to the given size.- Specified by:
setLengthin interfaceRandomAccessInterface- Parameters:
newLength- the size, in bytes, the file should have- Throws:
IOException- if the file cannot be resized- See Also:
close
Closes the file.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceRandomAccessInterface- Throws:
IOException- See Also:
readBoolean
Reads a single byte, reporting it as a boolean.- Specified by:
readBooleanin interfaceDataInput- Specified by:
readBooleanin interfaceRandomAccessInterface- Returns:
- true if the byte read is nonzero
- Throws:
IOException- if the file cannot be read- See Also:
readByte
Reads one byte as a signed value.- Specified by:
readBytein interfaceDataInput- Specified by:
readBytein interfaceRandomAccessInterface- Returns:
- the byte read
- Throws:
IOException- if the file cannot be read- See Also:
readChar
Reads two bytes as a big-endian char.- Specified by:
readCharin interfaceDataInput- Specified by:
readCharin interfaceRandomAccessInterface- Returns:
- the char read
- Throws:
IOException- if the file cannot be read- See Also:
readDouble
Reads eight bytes as a big-endian double.- Specified by:
readDoublein interfaceDataInput- Specified by:
readDoublein interfaceRandomAccessInterface- Returns:
- the double read
- Throws:
IOException- if the file cannot be read- See Also:
readFloat
Reads four bytes as a big-endian float.- Specified by:
readFloatin interfaceDataInput- Specified by:
readFloatin interfaceRandomAccessInterface- Returns:
- the float read
- Throws:
IOException- if the file cannot be read- See Also:
readFully
Fills b, failing unless every byte of it is read.- Specified by:
readFullyin interfaceDataInput- Specified by:
readFullyin interfaceRandomAccessInterface- Parameters:
b- the buffer to fill- Throws:
IOException- if the read fails or the end of file comes first- See Also:
readFully
Fills len bytes of b at off, failing unless all are read.- Specified by:
readFullyin interfaceDataInput- Specified by:
readFullyin interfaceRandomAccessInterface- Parameters:
b- the buffer to filloff- the index in b at which to start writinglen- the number of bytes required- Throws:
IOException- if the read fails or the end of file comes first- See Also:
readInt
Reads four bytes as a big-endian int.- Specified by:
readIntin interfaceDataInput- Specified by:
readIntin interfaceRandomAccessInterface- Returns:
- the int read
- Throws:
IOException- if the file cannot be read- See Also:
readLine
Reads bytes up to a line terminator, discarding the terminator.- Specified by:
readLinein interfaceDataInput- Specified by:
readLinein interfaceRandomAccessInterface- Returns:
- the line read, or null at the end of file
- Throws:
IOException- if the file cannot be read- See Also:
readLong
Reads eight bytes as a big-endian long.- Specified by:
readLongin interfaceDataInput- Specified by:
readLongin interfaceRandomAccessInterface- Returns:
- the long read
- Throws:
IOException- if the file cannot be read- See Also:
readShort
Reads two bytes as a big-endian short.- Specified by:
readShortin interfaceDataInput- Specified by:
readShortin interfaceRandomAccessInterface- Returns:
- the short read
- Throws:
IOException- if the file cannot be read- See Also:
readUnsignedByte
Reads one byte, widening it to an unsigned int.- Specified by:
readUnsignedBytein interfaceDataInput- Specified by:
readUnsignedBytein interfaceRandomAccessInterface- Returns:
- the byte read, 0 to 255
- Throws:
IOException- if the file cannot be read- See Also:
readUnsignedShort
Reads two bytes, widening them to an unsigned int.- Specified by:
readUnsignedShortin interfaceDataInput- Specified by:
readUnsignedShortin interfaceRandomAccessInterface- Returns:
- the short read, 0 to 65535
- Throws:
IOException- if the file cannot be read- See Also:
readUnsignedInt
Reads an unsigned 32-bit integer from the file. (I2P)- Specified by:
readUnsignedIntin interfaceRandomAccessInterface- Returns:
- the value read, which must be non-negative
- Throws:
IOException- if the read value is negative
readUTF
Read a UTF encoded string with 4-byte length prefix. (I2P)This method uses a 4-byte length prefix instead of Java's standard 2-byte prefix, allowing for strings up to 16MB in length. The upper byte must be zero for compatibility.
- Specified by:
readUTFin interfaceDataInput- Specified by:
readUTFin interfaceRandomAccessInterface- Returns:
- the UTF-8 decoded string
- Throws:
IOException- if the length encoding is invalid or EOF is reached- See Also:
skipBytes
Advances the file pointer without reading the bytes skipped.- Specified by:
skipBytesin interfaceDataInput- Specified by:
skipBytesin interfaceRandomAccessInterface- Parameters:
n- the number of bytes to skip- Returns:
- the number of bytes actually skipped
- Throws:
IOException- if the file cannot be read- See Also:
write
Writes the low eight bits of b.- Specified by:
writein interfaceDataOutput- Specified by:
writein interfaceRandomAccessInterface- Parameters:
b- the value to write, of which only the low byte is used- Throws:
IOException- if the file cannot be written- See Also:
write
Writes every byte of b.- Specified by:
writein interfaceDataOutput- Specified by:
writein interfaceRandomAccessInterface- Parameters:
b- the bytes to write- Throws:
IOException- if the file cannot be written- See Also:
write
Writes len bytes from b, starting at off.- Specified by:
writein interfaceDataOutput- Specified by:
writein interfaceRandomAccessInterface- Parameters:
b- the buffer the bytes are taken fromoff- the index in b at which to start readinglen- the number of bytes to write- Throws:
IOException- if the file cannot be written- See Also:
writeBoolean
Writes a single byte holding 1 for true, 0 for false.- Specified by:
writeBooleanin interfaceDataOutput- Specified by:
writeBooleanin interfaceRandomAccessInterface- Parameters:
v- the boolean to write- Throws:
IOException- if the file cannot be written- See Also:
writeByte
Writes the low eight bits of v.- Specified by:
writeBytein interfaceDataOutput- Specified by:
writeBytein interfaceRandomAccessInterface- Parameters:
v- the value to write, truncated to eight bits- Throws:
IOException- if the file cannot be written- See Also:
writeShort
Writes the low sixteen bits of v as a big-endian short.- Specified by:
writeShortin interfaceDataOutput- Specified by:
writeShortin interfaceRandomAccessInterface- Parameters:
v- the value to write, truncated to sixteen bits- Throws:
IOException- if the file cannot be written- See Also:
writeChar
Writes the low sixteen bits of v as a big-endian char.- Specified by:
writeCharin interfaceDataOutput- Specified by:
writeCharin interfaceRandomAccessInterface- Parameters:
v- the value to write, truncated to sixteen bits- Throws:
IOException- if the file cannot be written- See Also:
writeInt
Writes v as four big-endian bytes.- Specified by:
writeIntin interfaceDataOutput- Specified by:
writeIntin interfaceRandomAccessInterface- Parameters:
v- the value to write- Throws:
IOException- if the file cannot be written- See Also:
writeLong
Writes v as eight big-endian bytes.- Specified by:
writeLongin interfaceDataOutput- Specified by:
writeLongin interfaceRandomAccessInterface- Parameters:
v- the value to write- Throws:
IOException- if the file cannot be written- See Also:
writeFloat
Writes v as four big-endian bytes.- Specified by:
writeFloatin interfaceDataOutput- Specified by:
writeFloatin interfaceRandomAccessInterface- Parameters:
v- the value to write- Throws:
IOException- if the file cannot be written- See Also:
writeDouble
Writes v as eight big-endian bytes.- Specified by:
writeDoublein interfaceDataOutput- Specified by:
writeDoublein interfaceRandomAccessInterface- Parameters:
v- the value to write- Throws:
IOException- if the file cannot be written- See Also:
writeBytes
Writes the low byte of each character of s.- Specified by:
writeBytesin interfaceDataOutput- Specified by:
writeBytesin interfaceRandomAccessInterface- Parameters:
s- the string whose characters are written, one byte each- Throws:
IOException- if the file cannot be written- See Also:
writeChars
Writes each character of s as two big-endian bytes.- Specified by:
writeCharsin interfaceDataOutput- Specified by:
writeCharsin interfaceRandomAccessInterface- Parameters:
s- the string to write- Throws:
IOException- if the file cannot be written- See Also:
writeUTF
Write a UTF encoded string with 4-byte length prefix. (I2P)This method uses a 4-byte length prefix instead of Java's standard 2-byte prefix, allowing for strings up to 16MB in length.
- Specified by:
writeUTFin interfaceDataOutput- Specified by:
writeUTFin interfaceRandomAccessInterface- Parameters:
str- the string to write- Throws:
IOException- if the string is too long for encoding- See Also: