| 7 | import java.io.OutputStream; |
| 8 | |
| 9 | public class FileDataSource implements DataSource { |
| 10 | private RandomAccessFile randomAccessFile; |
| 11 | private long start; |
| 12 | private long size; |
| 13 | private long pos; |
| 14 | |
| 15 | FileDataSource(RandomAccessFile randomAccessFile, long start, long size) { |
| 16 | this.randomAccessFile = randomAccessFile; |
| 17 | this.start = start; |
| 18 | this.size = size; |
| 19 | } |
| 20 | |
| 21 | |
| 22 | @Override |
| 23 | public long size() { |
| 24 | return size; |
| 25 | } |
| 26 | |
| 27 | @Override |
| 28 | public long pos() { |
| 29 | return pos; |
| 30 | } |
| 31 | |
| 32 | @Override |
| 33 | public void reset() { |
| 34 | pos = 0; |
| 35 | } |
| 36 | |
| 37 | @Override |
| 38 | public void copyTo(OutputStream os, long length) throws IOException { |
| 39 | if (length > remaining()) |
| 40 | throw new EOFException(); |
| 41 | byte[] buf = new byte[4096]; |
| 42 | int readLen; |
| 43 | randomAccessFile.seek(start + pos); |
| 44 | while (length > 0 && (readLen = randomAccessFile.read(buf, 0, (int) Math.min(length, buf.length))) != -1) { |
| 45 | os.write(buf, 0, readLen); |
| 46 | length -= readLen; |
| 47 | pos += readLen; |
| 48 | } |
| 49 | if (length != 0) |
| 50 | throw new IllegalStateException("Remaining length: " + length); |
| 51 | } |
| 52 | } |
nothing calls this directly
no outgoing calls
no test coverage detected