Class RandomAccessFileKaitaiStream

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public class RandomAccessFileKaitaiStream
    extends KaitaiStream
    An implementation of KaitaiStream backed by a RandomAccessFile. Allows reading from local files. Generally, one would want to use ByteBufferKaitaiStream instead, 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.
    • Constructor Detail

      • RandomAccessFileKaitaiStream

        public RandomAccessFileKaitaiStream​(RandomAccessFile raf)
    • Method Detail

      • isEof

        public boolean isEof()
        Description copied from class: KaitaiStream
        Check if stream pointer is at the end of stream.
        Specified by:
        isEof in class KaitaiStream
        Returns:
        true if we are located at the end of the stream
      • seek

        public void seek​(int newPos)
        Description copied from class: KaitaiStream
        Set stream pointer to designated position (int).
        Specified by:
        seek in class KaitaiStream
        Parameters:
        newPos - new position (offset in bytes from the beginning of the stream)
      • seek

        public void seek​(long newPos)
        Description copied from class: KaitaiStream
        Set stream pointer to designated position (long).
        Specified by:
        seek in class KaitaiStream
        Parameters:
        newPos - new position (offset in bytes from the beginning of the stream)
      • pos

        public int pos()
        Description copied from class: KaitaiStream
        Get current position of a stream pointer.
        Specified by:
        pos in class KaitaiStream
        Returns:
        pointer position, number of bytes from the beginning of the stream
      • size

        public long size()
        Description copied from class: KaitaiStream
        Get total size of the stream in bytes.
        Specified by:
        size in class KaitaiStream
        Returns:
        size of the stream in bytes
      • readS1

        public byte readS1()
        Description copied from class: KaitaiStream
        Reads one signed 1-byte integer, returning it properly as Java's "byte" type.
        Specified by:
        readS1 in class KaitaiStream
        Returns:
        1-byte integer read from a stream
      • readBytesNotAligned

        protected byte[] readBytesNotAligned​(long n)
        Description copied from class: KaitaiStream
        Internal method to read the specified number of bytes from the stream. Unlike KaitaiStream.readBytes(long), it doesn't align the bit position to the next byte boundary.
        Specified by:
        readBytesNotAligned in class KaitaiStream
        Parameters:
        n - number of bytes to read
        Returns:
        read bytes as a byte array
      • readBytesFull

        public byte[] readBytesFull()
        Description copied from class: KaitaiStream
        Reads all the remaining bytes in a stream as byte array.
        Specified by:
        readBytesFull in class KaitaiStream
        Returns:
        all remaining bytes in a stream as byte array
      • readBytesTerm

        public byte[] readBytesTerm​(byte term,
                                    boolean includeTerm,
                                    boolean consumeTerm,
                                    boolean eosError)
        Specified by:
        readBytesTerm in class KaitaiStream
      • substream

        public KaitaiStream substream​(long n)
        Description copied from class: KaitaiStream
        Reserves 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:
        substream in class KaitaiStream
        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:
        writeS1 in class KaitaiStream
      • writeBytesNotAligned

        protected void writeBytesNotAligned​(byte[] buf)
        Description copied from class: KaitaiStream
        Internal method to write the given byte array to the stream. Unlike KaitaiStream.writeBytes(byte[]), it doesn't align the bit position to the next byte boundary.
        Specified by:
        writeBytesNotAligned in class KaitaiStream
        Parameters:
        buf - byte array to write