1
2
3
4
5
6
7
8
9
10
11 package psiprobe.tools;
12
13 import java.io.File;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.io.RandomAccessFile;
17
18
19
20
21 public class BackwardsFileStream extends InputStream {
22
23
24 private final RandomAccessFile raf;
25
26
27 private long seekPos;
28
29
30
31
32
33
34
35
36 public BackwardsFileStream(File file) throws IOException {
37 raf = new RandomAccessFile(file, "r");
38 seekPos = raf.length();
39 }
40
41
42
43
44
45
46
47
48
49 public BackwardsFileStream(File file, long pos) throws IOException {
50 raf = new RandomAccessFile(file, "r");
51 seekPos = pos;
52 }
53
54 @Override
55 public int read() throws IOException {
56 if (seekPos > 0) {
57 raf.seek(--seekPos);
58 return raf.read();
59 }
60
61 return -1;
62 }
63
64 @Override
65 public void close() throws IOException {
66 if (raf != null) {
67 raf.close();
68 }
69 }
70
71 }