1
2
3
4 package joeq.ClassLib.Common.java.io;
5
6 import joeq.Memory.HeapAddress;
7 import joeq.Runtime.SystemInterface;
8
9 /***
10 * FileInputStream
11 *
12 * @author John Whaley <jwhaley@alum.mit.edu>
13 * @version $Id: FileInputStream.java 1456 2004-03-09 22:01:46Z jwhaley $
14 */
15 abstract class FileInputStream {
16
17 private FileDescriptor fd;
18
19 private void open(String name)
20 throws java.io.FileNotFoundException {
21 int fdnum = SystemInterface.file_open(name, SystemInterface._O_RDONLY | SystemInterface._O_BINARY, 0);
22 if (fdnum == -1) throw new java.io.FileNotFoundException(name);
23 this.fd.fd = fdnum;
24 }
25 public int read() throws java.io.IOException {
26 byte[] b = new byte[1];
27 int v = this.readBytes(b, 0, 1);
28 if (v == -1) return -1;
29 else if (v != 1) throw new java.io.IOException();
30 return b[0]&0xFF;
31 }
32 private int readBytes(byte b[], int off, int len) throws java.io.IOException {
33 return readBytes(b, off, len, this.fd);
34 }
35
36 private int readBytes(byte b[], int off, int len, FileDescriptor fd) throws java.io.IOException {
37 int fdnum = fd.fd;
38
39 if (len < 0) throw new IndexOutOfBoundsException();
40
41 byte b2 = b[off+len-1];
42
43 if (off < 0) throw new IndexOutOfBoundsException();
44 if (len == 0) return 0;
45 HeapAddress start = (HeapAddress) HeapAddress.addressOf(b).offset(off);
46 int result = SystemInterface.file_readbytes(fdnum, start, len);
47 if (result == 0)
48 return -1;
49 if (result == -1)
50 throw new java.io.IOException();
51 return result;
52 }
53 public long skip(long n) throws java.io.IOException {
54 int fdnum = this.fd.fd;
55 long curpos = SystemInterface.file_seek(fdnum, 0, SystemInterface.SEEK_CUR);
56 long result = SystemInterface.file_seek(fdnum, n, SystemInterface.SEEK_CUR);
57 if (result == (long)-1)
58 throw new java.io.IOException();
59 return result-curpos;
60 }
61 public int available() throws java.io.IOException {
62 int fdnum = this.fd.fd;
63 if (fdnum == 0) {
64 int result = SystemInterface.console_available();
65 if (result == -1) throw new java.io.IOException();
66 return result;
67 } else {
68 long curpos = SystemInterface.file_seek(fdnum, 0, SystemInterface.SEEK_CUR);
69 if (curpos == (long)-1)
70 throw new java.io.IOException();
71 long endpos = SystemInterface.file_seek(fdnum, 0, SystemInterface.SEEK_END);
72 if (endpos == (long)-1)
73 throw new java.io.IOException();
74 long result = SystemInterface.file_seek(fdnum, curpos, SystemInterface.SEEK_SET);
75 if (result == (long)-1)
76 throw new java.io.IOException();
77 if (endpos-curpos > Integer.MAX_VALUE) return Integer.MAX_VALUE;
78 else return (int)(endpos-curpos);
79 }
80 }
81 public void close() throws java.io.IOException {
82 int fdnum = this.fd.fd;
83 int result = SystemInterface.file_close(fdnum);
84
85 if (false && result != 0)
86 throw new java.io.IOException();
87 }
88 private static void initIDs() { }
89
90 }