| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
package com.drew.lang; |
| 23 |
|
| 24 |
import com.drew.lang.annotations.NotNull; |
| 25 |
|
| 26 |
import java.io.EOFException; |
| 27 |
import java.io.IOException; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class SequentialByteArrayReader extends SequentialReader |
| 34 |
{ |
| 35 |
@NotNull |
| 36 |
private final byte[] _bytes; |
| 37 |
private int _index; |
| 38 |
|
| 39 |
@Override |
| 40 |
public long getPosition() |
| 41 |
{ |
| 42 |
return _index; |
| 43 |
} |
| 44 |
|
| 45 |
public SequentialByteArrayReader(@NotNull byte[] bytes) |
| 46 |
{ |
| 47 |
this(bytes, 0); |
| 48 |
} |
| 49 |
|
| 50 |
@SuppressWarnings("ConstantConditions") |
| 51 |
public SequentialByteArrayReader(@NotNull byte[] bytes, int baseIndex) |
| 52 |
{ |
| 53 |
if (bytes == null) |
| 54 |
throw new NullPointerException(); |
| 55 |
|
| 56 |
_bytes = bytes; |
| 57 |
_index = baseIndex; |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
public byte getByte() throws IOException |
| 62 |
{ |
| 63 |
if (_index >= _bytes.length) { |
| 64 |
throw new EOFException("End of data reached."); |
| 65 |
} |
| 66 |
return _bytes[_index++]; |
| 67 |
} |
| 68 |
|
| 69 |
@NotNull |
| 70 |
@Override |
| 71 |
public byte[] getBytes(int count) throws IOException |
| 72 |
{ |
| 73 |
if ((long)_index + count > _bytes.length) { |
| 74 |
throw new EOFException("End of data reached."); |
| 75 |
} |
| 76 |
|
| 77 |
byte[] bytes = new byte[count]; |
| 78 |
System.arraycopy(_bytes, _index, bytes, 0, count); |
| 79 |
_index += count; |
| 80 |
|
| 81 |
return bytes; |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
public void getBytes(@NotNull byte[] buffer, int offset, int count) throws IOException |
| 86 |
{ |
| 87 |
if ((long)_index + count > _bytes.length) { |
| 88 |
throw new EOFException("End of data reached."); |
| 89 |
} |
| 90 |
|
| 91 |
System.arraycopy(_bytes, _index, buffer, offset, count); |
| 92 |
_index += count; |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
public void skip(long n) throws IOException |
| 97 |
{ |
| 98 |
if (n < 0) { |
| 99 |
throw new IllegalArgumentException("n must be zero or greater."); |
| 100 |
} |
| 101 |
|
| 102 |
if (_index + n > _bytes.length) { |
| 103 |
throw new EOFException("End of data reached."); |
| 104 |
} |
| 105 |
|
| 106 |
_index += n; |
| 107 |
} |
| 108 |
|
| 109 |
@Override |
| 110 |
public boolean trySkip(long n) throws IOException |
| 111 |
{ |
| 112 |
if (n < 0) { |
| 113 |
throw new IllegalArgumentException("n must be zero or greater."); |
| 114 |
} |
| 115 |
|
| 116 |
if (_index + n > _bytes.length) { |
| 117 |
_index = _bytes.length; |
| 118 |
return false; |
| 119 |
} |
| 120 |
|
| 121 |
_index += n; |
| 122 |
|
| 123 |
return true; |
| 124 |
} |
| 125 |
|
| 126 |
@Override |
| 127 |
public int available() { |
| 128 |
return _bytes.length - _index; |
| 129 |
} |
| 130 |
} |