|
| 1 | +/** |
| 2 | + * Copyright 2015-2020 Kaitai Project: MIT license |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | + * a copy of this software and associated documentation files (the |
| 6 | + * "Software"), to deal in the Software without restriction, including |
| 7 | + * without limitation the rights to use, copy, modify, merge, publish, |
| 8 | + * distribute, sublicense, and/or sell copies of the Software, and to |
| 9 | + * permit persons to whom the Software is furnished to do so, subject to |
| 10 | + * the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be |
| 13 | + * included in all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 18 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 19 | + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 20 | + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 21 | + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | + */ |
| 23 | + |
| 24 | +package io.kaitai.struct; |
| 25 | + |
| 26 | +/** |
| 27 | + * Information about positions of parsed value in the streams. |
| 28 | + * |
| 29 | + * @since 0.10 |
| 30 | + */ |
| 31 | +public class Span { |
| 32 | + /** Offset from begin of the root stream, for which that span was created. */ |
| 33 | + public final long offset; |
| 34 | + /** |
| 35 | + * Offset from begin of the stream, from which value was parsed. This is relative |
| 36 | + * position, to get an absolute position (relative to the root stream) use |
| 37 | + * {@link #absoluteStart()}. That offset is always non-negative. |
| 38 | + */ |
| 39 | + public final long start; |
| 40 | + /** |
| 41 | + * Offset from begin of the stream, from which value was parsed. This is relative |
| 42 | + * position, to get an absolute position (relative to the root stream) use |
| 43 | + * {@link #absoluteEnd()}. |
| 44 | + * <p> |
| 45 | + * If that offset is negative, then value wasn't parsed yet or exception was |
| 46 | + * thrown while parsing value. |
| 47 | + */ |
| 48 | + public long end = -1; |
| 49 | + |
| 50 | + /** |
| 51 | + * Creates a span that starts at the current stream offset and ends at |
| 52 | + * the unknown position. |
| 53 | + * |
| 54 | + * @param io the stream to get the positional information |
| 55 | + */ |
| 56 | + public Span(KaitaiStream io) { |
| 57 | + this(io.offset(), io.pos()); |
| 58 | + } |
| 59 | + private Span(long offset, long start) { |
| 60 | + this.offset = offset; |
| 61 | + this.start = start; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Offset to the start of this span relative to the root stream. |
| 66 | + * |
| 67 | + * @return start offset from the root stream |
| 68 | + */ |
| 69 | + public long absoluteStart() { return offset + start; } |
| 70 | + /** |
| 71 | + * Offset to the end of this span relative to the root stream. |
| 72 | + * <p> |
| 73 | + * If that offset is negative, then value wasn't parsed yet or exception was |
| 74 | + * thrown while parsing value. |
| 75 | + * |
| 76 | + * @return start offset from the root stream or negative value if value not yet parsed |
| 77 | + */ |
| 78 | + public long absoluteEnd() { return end < 0 ? -1 : offset + end; } |
| 79 | + /** |
| 80 | + * Size of this span in bytes. |
| 81 | + * <p> |
| 82 | + * If size is negative, then value wasn't parsed yet or exception was |
| 83 | + * thrown while parsing value. |
| 84 | + * |
| 85 | + * @return size of the span in bytes |
| 86 | + */ |
| 87 | + public long size() { return end < 0 ? -1 : end - start; } |
| 88 | +} |
0 commit comments