-
Notifications
You must be signed in to change notification settings - Fork 6
/
RandomAccessFileInputStream.java
59 lines (47 loc) · 1.54 KB
/
RandomAccessFileInputStream.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package su.litvak.justdlna.util;
import java.io.*;
public class RandomAccessFileInputStream extends InputStream {
private final RandomAccessFile randomAccessFile;
/**
* Absolute position in file to finish reading on (exclusive)
*/
private long limit = -1;
public RandomAccessFileInputStream(File file) throws FileNotFoundException {
this(new RandomAccessFile(file, "r"));
}
public RandomAccessFileInputStream(RandomAccessFile randomAccessFile) {
this.randomAccessFile = randomAccessFile;
}
@Override
public int read() throws IOException {
return randomAccessFile.read();
}
@Override
public int read(byte[] b) throws IOException {
return randomAccessFile.read(b);
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
return randomAccessFile.read(b, off, len);
}
@Override
public int available() throws IOException {
long a = (limit >= 0 ? Math.min(limit, randomAccessFile.length()) : randomAccessFile.length()) - randomAccessFile.getFilePointer();
return (int) Math.min(Integer.MAX_VALUE, Math.max(a, 0));
}
@Override
public synchronized void reset() throws IOException {
limit = -1;
randomAccessFile.seek(0);
}
@Override
public void close() throws IOException {
randomAccessFile.close();
}
public void seek(long pos) throws IOException {
randomAccessFile.seek(pos);
}
public void limit(long limit) {
this.limit = limit;
}
}