Skip to content

Commit dadfd82

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

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,20 @@ public ByteBufferKaitaiStream(String fileName) throws IOException {
6363
* @param arr byte array to read from or write to
6464
*/
6565
public ByteBufferKaitaiStream(byte[] arr) {
66+
this(arr, 0);
67+
}
68+
69+
/**
70+
* Initializes a stream that will get data from the 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);
6680
fc = null;
6781
bb = ByteBuffer.wrap(arr);
6882
}
@@ -73,6 +87,19 @@ public ByteBufferKaitaiStream(byte[] arr) {
7387
* @param buffer {@link ByteBuffer} to read from or write to
7488
*/
7589
public ByteBufferKaitaiStream(ByteBuffer buffer) {
90+
this(buffer, 0);
91+
}
92+
93+
/**
94+
* Initializes a stream that will get data from given {@link ByteBuffer} on read.
95+
*
96+
* @param buffer ByteBuffer to read from
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);
76103
fc = null;
77104
bb = buffer;
78105
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,31 @@
6060
public abstract class KaitaiStream implements Closeable {
6161
protected int bitsLeft = 0;
6262
protected long bits = 0;
63+
/**
64+
* Offset from the root stream where this stream begins.
65+
*
66+
* @since 0.11
67+
*/
68+
protected final long offset;
6369
protected boolean bitsLe = false;
6470
protected boolean bitsWriteMode = false;
6571

6672
protected WriteBackHandler writeBackHandler;
6773

6874
protected List<KaitaiStream> childStreams = new ArrayList<>();
6975

76+
/** Initializes a stream with zero offset from the root stream. */
77+
public KaitaiStream() { this(0); }
78+
79+
/**
80+
* Initializes a stream with specified offset from the root stream.
81+
*
82+
* @param offset offset from the root stream where this stream begins
83+
*
84+
* @since 0.11
85+
*/
86+
public KaitaiStream(long offset) { this.offset = offset; }
87+
7088
@Override
7189
abstract public void close() throws IOException;
7290

@@ -90,6 +108,16 @@ public abstract class KaitaiStream implements Closeable {
90108
*/
91109
abstract public void seek(long newPos);
92110

111+
/**
112+
* Get position of a stream pointer relative to the root stream in the stream hierarchy.
113+
* Root stream is a stream without parent stream.
114+
*
115+
* @return the pointer position, number of bytes from the beginning of the root stream
116+
*
117+
* @since 0.11
118+
*/
119+
public long offset() { return this.offset; }
120+
93121
/**
94122
* Get current position of a stream pointer.
95123
* @return pointer position, number of bytes from the beginning of the stream

0 commit comments

Comments
 (0)