misc/persolijn

osm-protobuf/src/main/java/osm/common/RandomAccessFileInputStream.java in master
Repositories | Summary | Log | Files

RandomAccessFileInputStream.java (1150B) download


 1package osm.common;
 2
 3import java.io.IOException;
 4import java.io.InputStream;
 5import java.io.RandomAccessFile;
 6
 7public class RandomAccessFileInputStream extends InputStream {
 8    private final RandomAccessFile file;
 9
10    public RandomAccessFileInputStream(RandomAccessFile file) {
11        this.file = file;
12    }
13
14    @Override
15    public int read() throws IOException {
16        return file.read();
17    }
18
19    @Override
20    public int available() throws IOException {
21        long av = file.length() - file.getFilePointer();
22        return av < Integer.MAX_VALUE
23                ? (int) av
24                : Integer.MAX_VALUE;
25    }
26
27    @Override
28    public int read(byte[] buffer) throws IOException {
29        return file.read(buffer);
30    }
31
32    @Override
33    public int read(byte[] buffer, int offset, int length) throws IOException {
34        return file.read(buffer, offset, length);
35    }
36
37    @Override
38    public long skip(long size) throws IOException {
39        long fp = file.getFilePointer();
40        file.seek(fp + size);
41        return size;
42    }
43
44    @Override
45    public void close() throws IOException {
46        file.close();
47    }
48}