extend library class
[IRC.git] / Robust / src / ClassLibrary / 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   public int getfd() {
12     return fd;
13   }
14
15   private static native int nativeOpen(byte[] filename);
16   private static native int nativeRead(int fd, byte[] array, int numBytes);
17   private static native void nativeClose(int fd);
18
19   public int read() {
20     byte b[]=new byte[1];
21     int retval=read(b);
22     if (retval==-1)
23       return -1;
24     return b[0];
25   }
26
27   public int read(byte[] b) {
28     return nativeRead(fd, b, b.length);
29   }
30
31   public String readLine() {
32     String line = "";
33     int c = read();
34     
35     // if we're already at the end of the file
36     // don't even return the empty string
37     if( c == -1 ) { 
38       return null;
39     }
40
41     while( c != '\n' && c != -1 ) {
42       line += (char)c;
43       c = read();      
44     } 
45
46     return line;
47   }
48
49   public void close() {
50     nativeClose(fd);
51   }
52 }