View Javadoc

1   // FileOutputStream.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   
9   /***
10   * FileOutputStream
11   *
12   * @author  John Whaley <jwhaley@alum.mit.edu>
13   * @version $Id: FileOutputStream.java 1456 2004-03-09 22:01:46Z jwhaley $
14   */
15  abstract class FileOutputStream {
16  
17      private FileDescriptor fd;
18      
19      private void open(String name) throws java.io.FileNotFoundException {
20          int fdnum = SystemInterface.file_open(name, SystemInterface._O_WRONLY | SystemInterface._O_BINARY | SystemInterface._O_CREAT | SystemInterface._O_TRUNC, SystemInterface._S_IREAD | SystemInterface._S_IWRITE);
21          if (fdnum == -1) throw new java.io.FileNotFoundException(name);
22          this.fd.fd = fdnum;
23      }
24      private void openAppend(String name) throws java.io.FileNotFoundException {
25          int fdnum = SystemInterface.file_open(name, SystemInterface._O_WRONLY | SystemInterface._O_BINARY | SystemInterface._O_CREAT | SystemInterface._O_APPEND, SystemInterface._S_IREAD | SystemInterface._S_IWRITE);
26          if (fdnum == -1) throw new java.io.FileNotFoundException(name);
27          this.fd.fd = fdnum;
28      }
29      public void write(int b) throws java.io.IOException {
30          int fdnum = this.fd.fd;
31          int result = SystemInterface.file_writebyte(fdnum, b);
32          if (result != 1)
33              throw new java.io.IOException();
34      }
35      private void writeBytes(byte b[], int off, int len) throws java.io.IOException {
36          writeBytes(b, off, len, this.fd);
37      }
38      // IBM JDK has this extra fd argument here.
39      private void writeBytes(byte b[], int off, int len, FileDescriptor fd) throws java.io.IOException {
40          int fdnum = fd.fd;
41          // check for index out of bounds/null pointer
42          if (len < 0) throw new IndexOutOfBoundsException();
43          // do bounds check
44          byte b2 = b[off+len-1];
45          // BUG in Sun's implementation, which we mimic here.  off=b.length and len=0 doesn't throw an error (?)
46          if (off < 0) throw new IndexOutOfBoundsException();
47          if (len == 0) return;
48          HeapAddress start = (HeapAddress) HeapAddress.addressOf(b).offset(off);
49          int result = SystemInterface.file_writebytes(fdnum, start, len);
50          if (result != len)
51              throw new java.io.IOException();
52      }
53      public void close() throws java.io.IOException {
54          int fdnum = this.fd.fd;
55          int result = SystemInterface.file_close(fdnum);
56          // Sun's "implementation" ignores errors on file close, allowing files to be closed multiple times.
57          if (false && result != 0)
58              throw new java.io.IOException();
59      }
60      private static void initIDs() {}
61  
62  }