changes: now Inference engine works fine with the EyeTracking benchmark.
[IRC.git] / Robust / src / ClassLibrary / SSJava / FileInputStream.java
1 public class FileInputStream extends InputStream {
2   private int fd;
3
4   public FileInputStream(String pathname) {
5     fd = nativeOpen(pathname.getBytes());
6   }
7
8   public FileInputStream(File path) {
9     fd = nativeOpen(path.getPath().getBytes());
10   }
11
12   public int getfd() {
13     return fd;
14   }
15
16   private static native int nativeOpen(byte[] filename);
17
18   private static native int nativeRead(int fd, byte[] array, int numBytes);
19
20   private static native int nativePeek(int fd);
21
22   private static native void nativeClose(int fd);
23
24   private static native int nativeAvailable(int fd);
25
26   public int read() {
27     byte b[] = new byte[1];
28     int retval = read(b);
29     if (retval == -1 || retval == 0)
30       return -1;
31
32     // if carriage return comes back, dump it
33     if (b[0] == 13) {
34       return read();
35     }
36
37     // otherwise return result
38     return b[0];
39   }
40
41   public int peek() {
42     return nativePeek(fd);
43   }
44
45   public int read(byte[] b, int offset, int len) {
46     if (offset < 0 || len < 0 || offset + len > b.length) {
47       return -1;
48     }
49     byte readbuf[] = new byte[len];
50     int rtr = nativeRead(fd, readbuf, len);
51     System.arraycopy(readbuf, 0, b, offset, len);
52     return rtr;
53   }
54
55   public int read(byte[] b) {
56     return nativeRead(fd, b, b.length);
57   }
58
59   public String readLine() {
60     String line = "";
61     int c = read();
62
63     // if we're already at the end of the file
64     // or there is an error, don't even return
65     // the empty string
66     if (c <= 0) {
67       return null;
68     }
69
70     // ASCII 13 is carriage return, check for that also
71     while (c != '\n' && c != 13 && c > 0) {
72       line += (char) c;
73       c = read();
74     }
75
76     // peek and consume characters that are carriage
77     // returns or line feeds so the whole line is read
78     // and returned, and none of the line-ending chars
79     c = peek();
80     while (c == '\n' || c == 13) {
81       c = read();
82       c = peek();
83     }
84
85     return line;
86   }
87
88   public void close() {
89     nativeClose(fd);
90   }
91
92   public int available() {
93     return nativeAvailable(fd);
94   }
95 }