Package io.kaitai.struct
Class RandomAccessFileKaitaiStream
- java.lang.Object
-
- io.kaitai.struct.KaitaiStream
-
- io.kaitai.struct.RandomAccessFileKaitaiStream
-
- All Implemented Interfaces:
Closeable,AutoCloseable
public class RandomAccessFileKaitaiStream extends KaitaiStream
An implementation ofKaitaiStreambacked by aRandomAccessFile. Allows reading from local files. Generally, one would want to useByteBufferKaitaiStreaminstead, as it most likely would be faster, but there are two situations when one should consider this one instead:- Processing many small files. Every ByteBuffer invocation requires a mmap call, which can be relatively expensive (per file).
- Accessing extra-long files (>31 bits positioning). Unfortunately, Java's implementation of mmap uses ByteBuffer, which is not addressable beyond 31 bit offsets, even if you use a 64-bit platform.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class io.kaitai.struct.KaitaiStream
KaitaiStream.KaitaiStructError, KaitaiStream.UndecidedEndiannessError, KaitaiStream.UnexpectedDataError, KaitaiStream.ValidationExprError, KaitaiStream.ValidationFailedError, KaitaiStream.ValidationGreaterThanError, KaitaiStream.ValidationLessThanError, KaitaiStream.ValidationNotAnyOfError, KaitaiStream.ValidationNotEqualError, KaitaiStream.WriteBackHandler
-
-
Field Summary
Fields Modifier and Type Field Description protected RandomAccessFileraf-
Fields inherited from class io.kaitai.struct.KaitaiStream
bits, bitsLe, bitsLeft, bitsWriteMode, childStreams, writeBackHandler
-
-
Constructor Summary
Constructors Constructor Description RandomAccessFileKaitaiStream(RandomAccessFile raf)RandomAccessFileKaitaiStream(String fileName)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()protected voidensureBytesLeftToWrite(long n)booleanisEof()Check if stream pointer is at the end of stream.intpos()Get current position of a stream pointer.byte[]readBytesFull()Reads all the remaining bytes in a stream as byte array.protected byte[]readBytesNotAligned(long n)Internal method to read the specified number of bytes from the stream.byte[]readBytesTerm(byte term, boolean includeTerm, boolean consumeTerm, boolean eosError)floatreadF4be()floatreadF4le()doublereadF8be()doublereadF8le()bytereadS1()Reads one signed 1-byte integer, returning it properly as Java's "byte" type.shortreadS2be()shortreadS2le()intreadS4be()intreadS4le()longreadS8be()longreadS8le()intreadU1()intreadU2be()intreadU2le()longreadU4be()longreadU4le()voidseek(int newPos)Set stream pointer to designated position (int).voidseek(long newPos)Set stream pointer to designated position (long).longsize()Get total size of the stream in bytes.KaitaiStreamsubstream(long n)Reserves next `n` bytes from current stream as a KaitaiStream-compatible substream.protected voidwriteBytesNotAligned(byte[] buf)Internal method to write the given byte array to the stream.voidwriteF4be(float v)voidwriteF4le(float v)voidwriteF8be(double v)voidwriteF8le(double v)voidwriteS1(byte v)Writes one signed 1-byte integer.voidwriteS2be(short v)voidwriteS2le(short v)voidwriteS4be(int v)voidwriteS4le(int v)voidwriteS8be(long v)voidwriteS8le(long v)-
Methods inherited from class io.kaitai.struct.KaitaiStream
addChildStream, alignToByte, byteArrayCompare, byteArrayIndexOf, byteArrayMax, byteArrayMin, bytesStripRight, bytesTerminate, ensureBytesLeftToWrite, ensureFixedContents, mod, mod, processRotateLeft, processXor, processXor, processZlib, readBitsInt, readBitsIntBe, readBitsIntLe, readBytes, readU8be, readU8le, setWriteBackHandler, toByteArray, toByteArrayLength, unprocessZlib, writeAlignToByte, writeBack, writeBackChildStreams, writeBackChildStreams, writeBitsIntBe, writeBitsIntLe, writeBytes, writeBytesLimit, writeStream, writeU1, writeU2be, writeU2le, writeU4be, writeU4le, writeU8be, writeU8le
-
-
-
-
Field Detail
-
raf
protected RandomAccessFile raf
-
-
Constructor Detail
-
RandomAccessFileKaitaiStream
public RandomAccessFileKaitaiStream(String fileName) throws IOException
- Throws:
IOException
-
RandomAccessFileKaitaiStream
public RandomAccessFileKaitaiStream(RandomAccessFile raf)
-
-
Method Detail
-
close
public void close() throws IOException- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein classKaitaiStream- Throws:
IOException
-
isEof
public boolean isEof()
Description copied from class:KaitaiStreamCheck if stream pointer is at the end of stream.- Specified by:
isEofin classKaitaiStream- Returns:
- true if we are located at the end of the stream
-
seek
public void seek(int newPos)
Description copied from class:KaitaiStreamSet stream pointer to designated position (int).- Specified by:
seekin classKaitaiStream- Parameters:
newPos- new position (offset in bytes from the beginning of the stream)
-
seek
public void seek(long newPos)
Description copied from class:KaitaiStreamSet stream pointer to designated position (long).- Specified by:
seekin classKaitaiStream- Parameters:
newPos- new position (offset in bytes from the beginning of the stream)
-
pos
public int pos()
Description copied from class:KaitaiStreamGet current position of a stream pointer.- Specified by:
posin classKaitaiStream- Returns:
- pointer position, number of bytes from the beginning of the stream
-
size
public long size()
Description copied from class:KaitaiStreamGet total size of the stream in bytes.- Specified by:
sizein classKaitaiStream- Returns:
- size of the stream in bytes
-
readS1
public byte readS1()
Description copied from class:KaitaiStreamReads one signed 1-byte integer, returning it properly as Java's "byte" type.- Specified by:
readS1in classKaitaiStream- Returns:
- 1-byte integer read from a stream
-
readS2be
public short readS2be()
- Specified by:
readS2bein classKaitaiStream
-
readS4be
public int readS4be()
- Specified by:
readS4bein classKaitaiStream
-
readS8be
public long readS8be()
- Specified by:
readS8bein classKaitaiStream
-
readS2le
public short readS2le()
- Specified by:
readS2lein classKaitaiStream
-
readS4le
public int readS4le()
- Specified by:
readS4lein classKaitaiStream
-
readS8le
public long readS8le()
- Specified by:
readS8lein classKaitaiStream
-
readU1
public int readU1()
- Specified by:
readU1in classKaitaiStream
-
readU2be
public int readU2be()
- Specified by:
readU2bein classKaitaiStream
-
readU4be
public long readU4be()
- Specified by:
readU4bein classKaitaiStream
-
readU2le
public int readU2le()
- Specified by:
readU2lein classKaitaiStream
-
readU4le
public long readU4le()
- Specified by:
readU4lein classKaitaiStream
-
readF4be
public float readF4be()
- Specified by:
readF4bein classKaitaiStream
-
readF8be
public double readF8be()
- Specified by:
readF8bein classKaitaiStream
-
readF4le
public float readF4le()
- Specified by:
readF4lein classKaitaiStream
-
readF8le
public double readF8le()
- Specified by:
readF8lein classKaitaiStream
-
readBytesNotAligned
protected byte[] readBytesNotAligned(long n)
Description copied from class:KaitaiStreamInternal method to read the specified number of bytes from the stream. UnlikeKaitaiStream.readBytes(long), it doesn't align the bit position to the next byte boundary.- Specified by:
readBytesNotAlignedin classKaitaiStream- Parameters:
n- number of bytes to read- Returns:
- read bytes as a byte array
-
readBytesFull
public byte[] readBytesFull()
Description copied from class:KaitaiStreamReads all the remaining bytes in a stream as byte array.- Specified by:
readBytesFullin classKaitaiStream- Returns:
- all remaining bytes in a stream as byte array
-
readBytesTerm
public byte[] readBytesTerm(byte term, boolean includeTerm, boolean consumeTerm, boolean eosError)- Specified by:
readBytesTermin classKaitaiStream
-
substream
public KaitaiStream substream(long n)
Description copied from class:KaitaiStreamReserves next `n` bytes from current stream as a KaitaiStream-compatible substream. Substream has its own pointer and addressing in the range of [0, n) bytes. This stream's pointer is advanced to the position right after this substream.- Specified by:
substreamin classKaitaiStream- Parameters:
n- number of bytes to reserve for a substream- Returns:
- substream covering n bytes from the current position
-
ensureBytesLeftToWrite
protected void ensureBytesLeftToWrite(long n) throws IOException- Throws:
IOException
-
writeS1
public void writeS1(byte v)
Writes one signed 1-byte integer.- Specified by:
writeS1in classKaitaiStream
-
writeS2be
public void writeS2be(short v)
- Specified by:
writeS2bein classKaitaiStream
-
writeS4be
public void writeS4be(int v)
- Specified by:
writeS4bein classKaitaiStream
-
writeS8be
public void writeS8be(long v)
- Specified by:
writeS8bein classKaitaiStream
-
writeS2le
public void writeS2le(short v)
- Specified by:
writeS2lein classKaitaiStream
-
writeS4le
public void writeS4le(int v)
- Specified by:
writeS4lein classKaitaiStream
-
writeS8le
public void writeS8le(long v)
- Specified by:
writeS8lein classKaitaiStream
-
writeF4be
public void writeF4be(float v)
- Specified by:
writeF4bein classKaitaiStream
-
writeF8be
public void writeF8be(double v)
- Specified by:
writeF8bein classKaitaiStream
-
writeF4le
public void writeF4le(float v)
- Specified by:
writeF4lein classKaitaiStream
-
writeF8le
public void writeF8le(double v)
- Specified by:
writeF8lein classKaitaiStream
-
writeBytesNotAligned
protected void writeBytesNotAligned(byte[] buf)
Description copied from class:KaitaiStreamInternal method to write the given byte array to the stream. UnlikeKaitaiStream.writeBytes(byte[]), it doesn't align the bit position to the next byte boundary.- Specified by:
writeBytesNotAlignedin classKaitaiStream- Parameters:
buf- byte array to write
-
-