File tree 2 files changed +55
-0
lines changed
src/main/java/io/kaitai/struct 2 files changed +55
-0
lines changed Original file line number Diff line number Diff line change @@ -63,6 +63,20 @@ public ByteBufferKaitaiStream(String fileName) throws IOException {
63
63
* @param arr byte array to read from or write to
64
64
*/
65
65
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 );
66
80
fc = null ;
67
81
bb = ByteBuffer .wrap (arr );
68
82
}
@@ -73,6 +87,19 @@ public ByteBufferKaitaiStream(byte[] arr) {
73
87
* @param buffer {@link ByteBuffer} to read from or write to
74
88
*/
75
89
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 );
76
103
fc = null ;
77
104
bb = buffer ;
78
105
}
Original file line number Diff line number Diff line change 60
60
public abstract class KaitaiStream implements Closeable {
61
61
protected int bitsLeft = 0 ;
62
62
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 ;
63
69
protected boolean bitsLe = false ;
64
70
protected boolean bitsWriteMode = false ;
65
71
66
72
protected WriteBackHandler writeBackHandler ;
67
73
68
74
protected List <KaitaiStream > childStreams = new ArrayList <>();
69
75
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
+
70
88
@ Override
71
89
abstract public void close () throws IOException ;
72
90
@@ -90,6 +108,16 @@ public abstract class KaitaiStream implements Closeable {
90
108
*/
91
109
abstract public void seek (long newPos );
92
110
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
+
93
121
/**
94
122
* Get current position of a stream pointer.
95
123
* @return pointer position, number of bytes from the beginning of the stream
You can’t perform that action at this time.
0 commit comments