Skip to content

Commit b82d6b1

Browse files
committed
Add ability to report absolute positions (relative to the root stream)
Fix #26
1 parent 75ecd23 commit b82d6b1

File tree

4 files changed

+63
-4
lines changed

4 files changed

+63
-4
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>io.kaitai</groupId>
55
<artifactId>kaitai-struct-runtime</artifactId>
66
<packaging>jar</packaging>
7-
<version>0.8</version>
7+
<version>0.9-SNAPSHOT</version>
88

99
<name>Kaitai Struct runtime library for Java</name>
1010
<description>

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

Lines changed: 31 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
@@ -48,25 +48,55 @@ public class ByteBufferKaitaiStream extends KaitaiStream {
4848
* @throws IOException if file can't be read
4949
*/
5050
public ByteBufferKaitaiStream(String fileName) throws IOException {
51+
super(0);
5152
fc = FileChannel.open(Paths.get(fileName), StandardOpenOption.READ);
5253
bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
5354
}
5455

5556
/**
5657
* Initializes a stream that will get data from given byte array when read.
5758
* Internally, ByteBuffer wrapping given array will be used.
59+
*
5860
* @param arr byte array to read
5961
*/
6062
public ByteBufferKaitaiStream(byte[] arr) {
63+
this(arr, 0);
64+
}
65+
66+
/**
67+
* Initializes a stream that will get data from given byte array when read.
68+
* Internally, ByteBuffer wrapping given array will be used.
69+
*
70+
* @param arr byte array to read
71+
* @param offset offset from the root stream where this stream begins
72+
*
73+
* @since 0.9
74+
*/
75+
public ByteBufferKaitaiStream(byte[] arr, long offset) {
76+
super(offset);
6177
fc = null;
6278
bb = ByteBuffer.wrap(arr);
6379
}
6480

6581
/**
6682
* Initializes a stream that will get data from given ByteBuffer when read.
83+
*
6784
* @param buffer ByteBuffer to read
6885
*/
6986
public ByteBufferKaitaiStream(ByteBuffer buffer) {
87+
this(buffer, 0);
88+
}
89+
90+
/**
91+
* Initializes a stream that will get data from given ByteBuffer when read.
92+
*
93+
* @param buffer ByteBuffer to read
94+
* @param offset offset from the root stream where this stream begins
95+
*
96+
* @since 0.9
97+
*/
98+
public ByteBufferKaitaiStream(ByteBuffer buffer, long offset) {
99+
super(offset);
70100
fc = null;
71101
bb = buffer;
72102
}

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 {@link #p 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

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

Lines changed: 2 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
@@ -49,6 +49,7 @@ public class RandomAccessFileKaitaiStream extends KaitaiStream {
4949
protected RandomAccessFile raf;
5050

5151
public RandomAccessFileKaitaiStream(String fileName) throws IOException {
52+
super(0);
5253
raf = new RandomAccessFile(fileName, "r");
5354
}
5455

0 commit comments

Comments
 (0)