| 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 |
import java.io.RandomAccessFile; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class RandomAccessFileReader extends RandomAccessReader |
| 36 |
{ |
| 37 |
@NotNull |
| 38 |
private final RandomAccessFile _file; |
| 39 |
private final long _length; |
| 40 |
private int _currentIndex; |
| 41 |
|
| 42 |
private final int _baseOffset; |
| 43 |
|
| 44 |
@SuppressWarnings({ "ConstantConditions" }) |
| 45 |
@com.drew.lang.annotations.SuppressWarnings(value = "EI_EXPOSE_REP2", justification = "Design intent") |
| 46 |
public RandomAccessFileReader(@NotNull RandomAccessFile file) throws IOException |
| 47 |
{ |
| 48 |
this(file, 0); |
| 49 |
} |
| 50 |
|
| 51 |
@SuppressWarnings({ "ConstantConditions" }) |
| 52 |
@com.drew.lang.annotations.SuppressWarnings(value = "EI_EXPOSE_REP2", justification = "Design intent") |
| 53 |
public RandomAccessFileReader(@NotNull RandomAccessFile file, int baseOffset) throws IOException |
| 54 |
{ |
| 55 |
if (file == null) |
| 56 |
throw new NullPointerException(); |
| 57 |
|
| 58 |
_file = file; |
| 59 |
_baseOffset = baseOffset; |
| 60 |
_length = _file.length(); |
| 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 _length; |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public byte getByte(int index) throws IOException |
| 77 |
{ |
| 78 |
if (index != _currentIndex) |
| 79 |
seek(index); |
| 80 |
|
| 81 |
final int b = _file.read(); |
| 82 |
if (b < 0) |
| 83 |
throw new BufferBoundsException("Unexpected end of file encountered."); |
| 84 |
assert (b <= 0xff); |
| 85 |
_currentIndex++; |
| 86 |
return (byte)b; |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
@NotNull |
| 91 |
public byte[] getBytes(int index, int count) throws IOException |
| 92 |
{ |
| 93 |
validateIndex(index, count); |
| 94 |
|
| 95 |
if (index != _currentIndex) |
| 96 |
seek(index); |
| 97 |
|
| 98 |
byte[] bytes = new byte[count]; |
| 99 |
final int bytesRead = _file.read(bytes); |
| 100 |
_currentIndex += bytesRead; |
| 101 |
if (bytesRead != count) |
| 102 |
throw new BufferBoundsException("Unexpected end of file encountered."); |
| 103 |
return bytes; |
| 104 |
} |
| 105 |
|
| 106 |
private void seek(final int index) throws IOException |
| 107 |
{ |
| 108 |
if (index == _currentIndex) |
| 109 |
return; |
| 110 |
|
| 111 |
_file.seek(index); |
| 112 |
_currentIndex = index; |
| 113 |
} |
| 114 |
|
| 115 |
@Override |
| 116 |
protected boolean isValidIndex(int index, int bytesRequested) throws IOException |
| 117 |
{ |
| 118 |
return bytesRequested >= 0 |
| 119 |
&& index >= 0 |
| 120 |
&& (long)index + (long)bytesRequested - 1L < _length; |
| 121 |
} |
| 122 |
|
| 123 |
@Override |
| 124 |
protected void validateIndex(final int index, final int bytesRequested) throws IOException |
| 125 |
{ |
| 126 |
if (!isValidIndex(index, bytesRequested)) |
| 127 |
throw new BufferBoundsException(index, bytesRequested, _length); |
| 128 |
} |
| 129 |
} |