Skip to content

Commit 980a7b7

Browse files
committed
Add ability to report absolute positions (relative to the root stream)
Fix #26
1 parent de129e4 commit 980a7b7

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2015-2019 Kaitai Project: MIT license
2+
* Copyright 2015-2020 Kaitai Project: MIT license
33
*
44
* Permission is hereby granted, free of charge, to any person obtaining
55
* a copy of this software and associated documentation files (the
@@ -55,18 +55,47 @@ public ByteBufferKaitaiStream(String fileName) throws IOException {
5555
/**
5656
* Initializes a stream that will get data from given byte array when read.
5757
* Internally, ByteBuffer wrapping given array will be used.
58+
*
5859
* @param arr byte array to read
5960
*/
6061
public ByteBufferKaitaiStream(byte[] arr) {
62+
this(arr, 0);
63+
}
64+
65+
/**
66+
* Initializes a stream that will get data from given byte array when read.
67+
* Internally, ByteBuffer wrapping given array will be used.
68+
*
69+
* @param arr byte array to read
70+
* @param offset offset from the root stream where this stream begins
71+
*
72+
* @since 0.9
73+
*/
74+
public ByteBufferKaitaiStream(byte[] arr, long offset) {
75+
super(offset);
6176
fc = null;
6277
bb = ByteBuffer.wrap(arr);
6378
}
6479

6580
/**
6681
* Initializes a stream that will get data from given ByteBuffer when read.
82+
*
6783
* @param buffer ByteBuffer to read
6884
*/
6985
public ByteBufferKaitaiStream(ByteBuffer buffer) {
86+
this(buffer, 0);
87+
}
88+
89+
/**
90+
* Initializes a stream that will get data from given ByteBuffer when read.
91+
*
92+
* @param buffer ByteBuffer to read
93+
* @param offset offset from the root stream where this stream begins
94+
*
95+
* @since 0.9
96+
*/
97+
public ByteBufferKaitaiStream(ByteBuffer buffer, long offset) {
98+
super(offset);
7099
fc = null;
71100
bb = buffer;
72101
}

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2015-2019 Kaitai Project: MIT license
2+
* Copyright 2015-2020 Kaitai Project: MIT license
33
*
44
* Permission is hereby granted, free of charge, to any person obtaining
55
* a copy of this software and associated documentation files (the
@@ -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.9
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.9
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 stream hierarchy.
103+
* Root stream is stream without parent stream.
104+
*
105+
* @return pointer position, number of bytes from the beginning of the root stream
106+
*
107+
* @since 0.9
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)