Skip to content

Commit

Permalink
Add ability to report absolute positions (relative to the root stream)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingun committed Aug 9, 2020
1 parent c342c6e commit 749be0f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>io.kaitai</groupId>
<artifactId>kaitai-struct-runtime</artifactId>
<packaging>jar</packaging>
<version>0.8</version>
<version>0.9-SNAPSHOT</version>

<name>Kaitai Struct runtime library for Java</name>
<description>
Expand Down
31 changes: 30 additions & 1 deletion src/main/java/io/kaitai/struct/ByteBufferKaitaiStream.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2015-2019 Kaitai Project: MIT license
* Copyright 2015-2020 Kaitai Project: MIT license
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
Expand Down Expand Up @@ -55,18 +55,47 @@ public ByteBufferKaitaiStream(String fileName) throws IOException {
/**
* Initializes a stream that will get data from given byte array when read.
* Internally, ByteBuffer wrapping given array will be used.
*
* @param arr byte array to read
*/
public ByteBufferKaitaiStream(byte[] arr) {
this(arr, 0);
}

/**
* Initializes a stream that will get data from given byte array when read.
* Internally, ByteBuffer wrapping given array will be used.
*
* @param arr byte array to read
* @param offset offset from the root stream where this stream begins
*
* @since 0.9
*/
public ByteBufferKaitaiStream(byte[] arr, long offset) {
super(offset);
fc = null;
bb = ByteBuffer.wrap(arr);
}

/**
* Initializes a stream that will get data from given ByteBuffer when read.
*
* @param buffer ByteBuffer to read
*/
public ByteBufferKaitaiStream(ByteBuffer buffer) {
this(buffer, 0);
}

/**
* Initializes a stream that will get data from given ByteBuffer when read.
*
* @param buffer ByteBuffer to read
* @param offset offset from the root stream where this stream begins
*
* @since 0.9
*/
public ByteBufferKaitaiStream(ByteBuffer buffer, long offset) {
super(offset);
fc = null;
bb = buffer;
}
Expand Down
30 changes: 29 additions & 1 deletion src/main/java/io/kaitai/struct/KaitaiStream.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2015-2019 Kaitai Project: MIT license
* Copyright 2015-2020 Kaitai Project: MIT license
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
Expand Down Expand Up @@ -56,6 +56,24 @@
public abstract class KaitaiStream implements Closeable {
protected int bitsLeft = 0;
protected long bits = 0;
/**
* Offset from the root stream where this stream begins.
*
* @since 0.9
*/
protected final long offset;

/** Initializes a stream with zero offset from the root stream. */
public KaitaiStream() { this(0); }

/**
* Initializes a stream with specified offset from the root stream.
*
* @param offset offset from the root stream where this stream begins
*
* @since 0.9
*/
public KaitaiStream(long offset) { this.offset = offset; }

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

/**
* Get position of a stream pointer relative to the root stream in stream hierarchy.
* Root stream is stream without parent stream.
*
* @return pointer position, number of bytes from the beginning of the root stream
*
* @since 0.9
*/
public long offset() { return this.offset; }

/**
* Get current position of a stream pointer.
* @return pointer position, number of bytes from the beginning of the stream
Expand Down

0 comments on commit 749be0f

Please sign in to comment.