| 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.IOException; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class ByteArrayReader extends RandomAccessReader |
| 38 |
{ |
| 39 |
@NotNull |
| 40 |
private final byte[] _buffer; |
| 41 |
private final int _baseOffset; |
| 42 |
|
| 43 |
@SuppressWarnings({ "ConstantConditions" }) |
| 44 |
@com.drew.lang.annotations.SuppressWarnings(value = "EI_EXPOSE_REP2", justification = "Design intent") |
| 45 |
public ByteArrayReader(@NotNull byte[] buffer) |
| 46 |
{ |
| 47 |
this(buffer, 0); |
| 48 |
} |
| 49 |
|
| 50 |
@SuppressWarnings({ "ConstantConditions" }) |
| 51 |
@com.drew.lang.annotations.SuppressWarnings(value = "EI_EXPOSE_REP2", justification = "Design intent") |
| 52 |
public ByteArrayReader(@NotNull byte[] buffer, int baseOffset) |
| 53 |
{ |
| 54 |
if (buffer == null) |
| 55 |
throw new NullPointerException(); |
| 56 |
if (baseOffset < 0) |
| 57 |
throw new IllegalArgumentException("Must be zero or greater"); |
| 58 |
|
| 59 |
_buffer = buffer; |
| 60 |
_baseOffset = baseOffset; |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public int toUnshiftedOffset(int localOffset) |
| 65 |
{ |
| 66 |
return localOffset + _baseOffset; |
| 67 |
} |
| 68 |
|
| 69 |
@Override |
| 70 |
public long getLength() |
| 71 |
{ |
| 72 |
return _buffer.length - _baseOffset; |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public byte getByte(int index) throws IOException |
| 77 |
{ |
| 78 |
validateIndex(index, 1); |
| 79 |
return _buffer[index + _baseOffset]; |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
protected void validateIndex(int index, int bytesRequested) throws IOException |
| 84 |
{ |
| 85 |
if (!isValidIndex(index, bytesRequested)) |
| 86 |
throw new BufferBoundsException(toUnshiftedOffset(index), bytesRequested, _buffer.length); |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
protected boolean isValidIndex(int index, int bytesRequested) throws IOException |
| 91 |
{ |
| 92 |
return bytesRequested >= 0 |
| 93 |
&& index >= 0 |
| 94 |
&& (long)index + (long)bytesRequested - 1L < getLength(); |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
@NotNull |
| 99 |
public byte[] getBytes(int index, int count) throws IOException |
| 100 |
{ |
| 101 |
validateIndex(index, count); |
| 102 |
|
| 103 |
byte[] bytes = new byte[count]; |
| 104 |
System.arraycopy(_buffer, index + _baseOffset, bytes, 0, count); |
| 105 |
return bytes; |
| 106 |
} |
| 107 |
} |