Skip to content

Commit 35d4069

Browse files
committed
Add ability to report absolute positions (relative to the root stream)
Fix kaitai-io#26
1 parent 105f51e commit 35d4069

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/main/java/io/kaitai/struct/ByteBufferKaitaiStream.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,47 @@ public ByteBufferKaitaiStream(String fileName) throws IOException {
5959
/**
6060
* Initializes a stream that will get data from given byte array when read.
6161
* Internally, ByteBuffer wrapping given array will be used.
62+
*
6263
* @param arr byte array to read
6364
*/
6465
public ByteBufferKaitaiStream(byte[] arr) {
66+
this(arr, 0);
67+
}
68+
69+
/**
70+
* Initializes a stream that will get data from given byte array when read.
71+
* Internally, ByteBuffer wrapping given array will be used.
72+
*
73+
* @param arr byte array to read
74+
* @param offset offset from the root stream where this stream begins
75+
*
76+
* @since 0.11
77+
*/
78+
public ByteBufferKaitaiStream(byte[] arr, long offset) {
79+
super(offset);
6580
fc = null;
6681
bb = ByteBuffer.wrap(arr);
6782
}
6883

6984
/**
7085
* Initializes a stream that will get data from given ByteBuffer when read.
86+
*
7187
* @param buffer ByteBuffer to read
7288
*/
7389
public ByteBufferKaitaiStream(ByteBuffer buffer) {
90+
this(buffer, 0);
91+
}
92+
93+
/**
94+
* Initializes a stream that will get data from given ByteBuffer when read.
95+
*
96+
* @param buffer ByteBuffer to read
97+
* @param offset offset from the root stream where this stream begins
98+
*
99+
* @since 0.11
100+
*/
101+
public ByteBufferKaitaiStream(ByteBuffer buffer, long offset) {
102+
super(offset);
74103
fc = null;
75104
bb = buffer;
76105
}

src/main/java/io/kaitai/struct/KaitaiStream.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,24 @@
5656
public abstract class KaitaiStream implements Closeable {
5757
protected int bitsLeft = 0;
5858
protected long bits = 0;
59+
/**
60+
* Offset from the root stream where this stream begins.
61+
*
62+
* @since 0.11
63+
*/
64+
protected final long offset;
65+
66+
/** Initializes a stream with zero offset from the root stream. */
67+
public KaitaiStream() { this(0); }
68+
69+
/**
70+
* Initializes a stream with specified offset from the root stream.
71+
*
72+
* @param offset offset from the root stream where this stream begins
73+
*
74+
* @since 0.11
75+
*/
76+
public KaitaiStream(long offset) { this.offset = offset; }
5977

6078
@Override
6179
abstract public void close() throws IOException;
@@ -80,6 +98,16 @@ public abstract class KaitaiStream implements Closeable {
8098
*/
8199
abstract public void seek(long newPos);
82100

101+
/**
102+
* Get position of a stream pointer relative to the root stream in the stream hierarchy.
103+
* Root stream is a stream without parent stream.
104+
*
105+
* @return the pointer position, number of bytes from the beginning of the root stream
106+
*
107+
* @since 0.11
108+
*/
109+
public long offset() { return this.offset; }
110+
83111
/**
84112
* Get current position of a stream pointer.
85113
* @return pointer position, number of bytes from the beginning of the stream

0 commit comments

Comments
 (0)