Source code: ClassLib/Common/java/io/FileOutputStream.java
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 ClassLib.Common.java.io;
5
6 import Memory.HeapAddress;
7 import Run_Time.SystemInterface;
8
9 /**
10 * FileOutputStream
11 *
12 * @author John Whaley <jwhaley@alum.mit.edu>
13 * @version $Id: FileOutputStream.java,v 1.8 2003/07/23 18:29:46 joewhaley Exp $
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 byte b2 = b[off+len-1];
44 // BUG in Sun's implementation, which we mimic here. off=b.length and len=0 doesn't throw an error (?)
45 if (off < 0) throw new IndexOutOfBoundsException();
46 if (len == 0) return;
47 HeapAddress start = (HeapAddress) HeapAddress.addressOf(b).offset(off);
48 int result = SystemInterface.file_writebytes(fdnum, start, len);
49 if (result != len)
50 throw new java.io.IOException();
51 }
52 public void close() throws java.io.IOException {
53 int fdnum = this.fd.fd;
54 int result = SystemInterface.file_close(fdnum);
55 // Sun's "implementation" ignores errors on file close, allowing files to be closed multiple times.
56 //if (result != 0)
57 // throw new java.io.IOException();
58 }
59 private static void initIDs() {}
60
61 }