Class ByteBufferKaitaiStream
- java.lang.Object
-
- io.kaitai.struct.KaitaiStream
-
- io.kaitai.struct.ByteBufferKaitaiStream
-
- All Implemented Interfaces:
Closeable,AutoCloseable
public class ByteBufferKaitaiStream extends KaitaiStream
An implementation ofKaitaiStreambacked by aByteBuffer. It can be either aMappedByteBufferbacked byFileChannel, or a regular wrapper over a given byte array).
-
-
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
-
-
Field Summary
-
Fields inherited from class io.kaitai.struct.KaitaiStream
bits, bitsLeft
-
-
Constructor Summary
Constructors Constructor Description ByteBufferKaitaiStream(byte[] arr)Initializes a stream that will get data from given byte array when read.ByteBufferKaitaiStream(String fileName)Initializes a stream, reading from a local file with specified fileName.ByteBufferKaitaiStream(ByteBuffer buffer)Initializes a stream that will get data from given ByteBuffer when read.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ByteBufferasRoBuffer()Provide a read-only version of theByteBufferbacking the data of this instance.voidclose()Closes the stream safely.booleanisEof()Check if stream pointer is at the end of stream.intpos()Get current position of a stream pointer.byte[]readBytes(long n)Reads designated number of bytes from the stream.byte[]readBytesFull()Reads all the remaining bytes in a stream as byte array.byte[]readBytesTerm(int 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.-
Methods inherited from class io.kaitai.struct.KaitaiStream
alignToByte, byteArrayCompare, byteArrayMax, byteArrayMin, bytesStripRight, bytesTerminate, ensureFixedContents, mod, mod, processRotateLeft, processXor, processXor, processZlib, readBitsInt, readBitsIntBe, readBitsIntLe, readU8be, readU8le, toByteArrayLength
-
-
-
-
Constructor Detail
-
ByteBufferKaitaiStream
public ByteBufferKaitaiStream(String fileName) throws IOException
Initializes a stream, reading from a local file with specified fileName. Internally, FileChannel + MappedByteBuffer will be used.- Parameters:
fileName- file to read- Throws:
IOException- if file can't be read
-
ByteBufferKaitaiStream
public ByteBufferKaitaiStream(byte[] arr)
Initializes a stream that will get data from given byte array when read. Internally, ByteBuffer wrapping given array will be used.- Parameters:
arr- byte array to read
-
ByteBufferKaitaiStream
public ByteBufferKaitaiStream(ByteBuffer buffer)
Initializes a stream that will get data from given ByteBuffer when read.- Parameters:
buffer- ByteBuffer to read
-
-
Method Detail
-
asRoBuffer
public ByteBuffer asRoBuffer()
Provide a read-only version of theByteBufferbacking the data of this instance.This way one can access the underlying raw bytes associated with this structure, but it is important to note that the caller needs to know what this raw data is: Depending on the hierarchy of user types, how the format has been described and how a user type is actually used, it might be that one accesses all data of some format or only a special substream view of it. We can't know currently, so one needs to keep that in mind when authoring a KSY and e.g. use substreams with user types whenever such a type most likely needs to access its underlying raw data. Using a substream in KSY and directly passing some raw data to a user type outside of normal KS parse order is equivalent and will provide the same results. If no substream is used instead, the here provided data might differ depending on the context in which the associated type was parsed, because the underlying
ByteBuffermight contain the data of all parent types and such as well and not only the one the caller is actually interested in.The returned
ByteBufferis always rewinded to position 0, because this stream was most likely used to parse a type already, in which case the former position would have been at the end of the buffer. Such a position doesn't help a common reading user much and that fact can easily be forgotten, repositioning to another index than the start is pretty easy as well. Rewinding/repositioning doesn't even harm performance in any way.- Returns:
- read-only
ByteBufferto access raw data for the associated type.
-
close
public void close() throws IOExceptionCloses the stream safely. If there was an open file associated with it, closes that file. For streams that were reading from in-memory array, does nothing.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein classKaitaiStream- Implementation Note:
- Unfortunately, there is no simple way to close memory-mapped ByteBuffer in
Java and unmap underlying file. As
MappedByteBufferdocumentation suggests, "mapped byte buffer and the file mapping that it represents remain valid until the buffer itself is garbage-collected". Thus, the best we can do is to delete all references to it, which breaks all subsequentread..methods withNullPointerException. Afterwards, a call toSystem.gc()will typically release the mmap, if garbage collection will be triggered.There is a JDK-4724038 request for adding unmap method filed at Java bugtracker since 2002, but as of 2018, it is still unresolved.
A couple of unsafe approaches (such as using JNI, or using reflection to invoke JVM internal APIs) have been suggested and used with some success, but these are either unportable or dangerous (may crash JVM), so we're not using them in this general purpose code.
For more examples and suggestions, see: https://stackoverflow.com/questions/2972986/how-to-unmap-a-file-from-memory-mapped-using-filechannel-in-java
- Throws:
IOException- if FileChannel can't be closed
-
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()
Reads 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
-
readBytes
public byte[] readBytes(long n)
Reads designated number of bytes from the stream.- Specified by:
readBytesin classKaitaiStream- Parameters:
n- number of bytes to read- Returns:
- read bytes as byte array
-
readBytesFull
public byte[] readBytesFull()
Reads 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(int term, boolean includeTerm, boolean consumeTerm, boolean eosError)- Specified by:
readBytesTermin classKaitaiStream
-
-