1 public class FileInputStream extends InputStream {
4 public FileInputStream(String pathname) {
5 fd = nativeOpen(pathname.getBytes());
8 public FileInputStream(File path) {
9 fd = nativeOpen(path.getPath().getBytes());
16 private static native int nativeOpen(byte[] filename);
18 private static native int nativeRead(int fd, byte[] array, int numBytes);
20 private static native int nativePeek(int fd);
22 private static native void nativeClose(int fd);
24 private static native int nativeAvailable(int fd);
27 byte b[] = new byte[1];
29 if (retval == -1 || retval == 0)
32 // if carriage return comes back, dump it
37 // otherwise return result
42 return nativePeek(fd);
45 public int read(byte[] b, int offset, int len) {
46 if (offset < 0 || len < 0 || offset + len > b.length) {
49 byte readbuf[] = new byte[len];
50 int rtr = nativeRead(fd, readbuf, len);
51 for (int i = offset; i < len + offset; i++) {
52 b[i] = readbuf[i - offset];
57 public int read(byte[] b) {
58 return nativeRead(fd, b, b.length);
61 public String readLine() {
65 // if we're already at the end of the file
66 // or there is an error, don't even return
72 // ASCII 13 is carriage return, check for that also
73 while (c != '\n' && c != 13 && c > 0) {
78 // peek and consume characters that are carriage
79 // returns or line feeds so the whole line is read
80 // and returned, and none of the line-ending chars
82 while (c == '\n' || c == 13) {
94 public int available() {
95 return nativeAvailable(fd);