View Javadoc

1   // RandomAccessFile.java, created Thu Jul  4  4:50:03 2002 by joewhaley
2   // Copyright (C) 2001-3 John Whaley <jwhaley@alum.mit.edu>
3   // Licensed under the terms of the GNU LGPL; see COPYING for details.
4   package joeq.ClassLib.Common.java.io;
5   
6   import joeq.Memory.HeapAddress;
7   import joeq.Runtime.SystemInterface;
8   import jwutil.util.Assert;
9   
10  /***
11   * RandomAccessFile
12   *
13   * @author  John Whaley <jwhaley@alum.mit.edu>
14   * @version $Id: RandomAccessFile.java 1941 2004-09-30 03:37:06Z joewhaley $
15   */
16  public abstract class RandomAccessFile {
17  
18      private FileDescriptor fd;
19      
20      // question: should this be private?
21      public void open(java.lang.String name, boolean writeable)
22      throws java.io.FileNotFoundException {
23          int flags = writeable?(SystemInterface._O_RDWR | SystemInterface._O_CREAT | SystemInterface._O_BINARY)
24                               :(SystemInterface._O_RDONLY | SystemInterface._O_BINARY);
25          int fdnum = SystemInterface.file_open(name, flags, 0);
26          if (fdnum == -1) throw new java.io.FileNotFoundException(name);
27          this.fd.fd = fdnum;
28      }
29      public int read() throws java.io.IOException {
30          byte[] b = new byte[1];
31          int v = this.readBytes(b, 0, 1);
32          if (v == -1) return -1;
33          else if (v != 1) throw new java.io.IOException();
34          return b[0]&0xFF;
35      }
36      private int readBytes(byte b[], int off, int len) throws java.io.IOException {
37          return readBytes(b, off, len, this.fd);
38      }
39      // IBM JDK has this extra fd argument here.
40      private int readBytes(byte b[], int off, int len, FileDescriptor fd) throws java.io.IOException {
41          int fdnum = fd.fd;
42          // check for index out of bounds/null pointer
43          if (len < 0) throw new IndexOutOfBoundsException();
44          // do bounds check
45          byte b2 = b[off+len-1];
46          // BUG in Sun's implementation, which we mimic here.  off=b.length and len=0 doesn't throw an error (?)
47          if (off < 0) throw new IndexOutOfBoundsException();
48          if (len == 0) return 0;
49          HeapAddress start = (HeapAddress) HeapAddress.addressOf(b).offset(off);
50          int result = SystemInterface.file_readbytes(fdnum, start, len);
51          if (result == 0)
52              return -1; // EOF
53          if (result == -1)
54              throw new java.io.IOException();
55          return result;
56      }
57      public void write(int b) throws java.io.IOException {
58          int fdnum = this.fd.fd;
59          int result = SystemInterface.file_writebyte(fdnum, b);
60          if (result != 1)
61              throw new java.io.IOException();
62      }
63      private void writeBytes(byte b[], int off, int len) throws java.io.IOException {
64          writeBytes(b, off, len, this.fd);
65      }
66      // IBM JDK has this extra fd argument here.
67      private void writeBytes(byte b[], int off, int len, FileDescriptor fd) throws java.io.IOException {
68          int fdnum = fd.fd;
69          // check for index out of bounds/null pointer
70          if (len < 0) throw new IndexOutOfBoundsException();
71          // do bounds check
72          byte b2 = b[off+len-1];
73          // BUG in Sun's implementation, which we mimic here.  off=b.length and len=0 doesn't throw an error (?)
74          if (off < 0) throw new IndexOutOfBoundsException();
75          if (len == 0) return;
76          HeapAddress start = (HeapAddress) HeapAddress.addressOf(b).offset(off);
77          int result = SystemInterface.file_writebytes(fdnum, start, len);
78          if (result != len)
79              throw new java.io.IOException();
80      }
81      public long getFilePointer() throws java.io.IOException {
82          int fdnum = this.fd.fd;
83          long curpos = SystemInterface.file_seek(fdnum, 0, SystemInterface.SEEK_CUR);
84          if (curpos == (long)-1)
85              throw new java.io.IOException();
86          return curpos;
87      }
88      public void seek(long pos) throws java.io.IOException {
89          if (pos < 0L)
90              throw new java.io.IOException(pos+" < 0");
91          int fdnum = this.fd.fd;
92          long result = SystemInterface.file_seek(fdnum, pos, SystemInterface.SEEK_SET);
93          if (result == (long)-1)
94              throw new java.io.IOException();
95      }
96      public long length() throws java.io.IOException {
97          int fdnum = this.fd.fd;
98          long curpos = SystemInterface.file_seek(fdnum, 0, SystemInterface.SEEK_CUR);
99          if (curpos == (long)-1)
100             throw new java.io.IOException();
101         long endpos = SystemInterface.file_seek(fdnum, 0, SystemInterface.SEEK_END);
102         if (endpos == (long)-1)
103             throw new java.io.IOException();
104         long result = SystemInterface.file_seek(fdnum, curpos, SystemInterface.SEEK_SET);
105         if (result == (long)-1)
106             throw new java.io.IOException();
107         return endpos;
108     }
109     public void setLength(long newLength) throws java.io.IOException {
110         Assert.TODO();
111     }
112     public void close() throws java.io.IOException {
113         int fdnum = this.fd.fd;
114         int result = SystemInterface.file_close(fdnum);
115         // Sun's "implementation" ignores errors on file close, allowing files to be closed multiple times.
116         if (false && result != 0)
117             throw new java.io.IOException();
118     }
119     private static void initIDs() { }
120 
121 }