Index: D:/clear/Eclipse3.2/workspace/luni/src/main/java/java/io/FileOutputStream.java
===================================================================
--- D:/clear/Eclipse3.2/workspace/luni/src/main/java/java/io/FileOutputStream.java (revision 372135)
+++ D:/clear/Eclipse3.2/workspace/luni/src/main/java/java/io/FileOutputStream.java (working copy)
@@ -15,9 +15,11 @@
package java.io;
-
import java.nio.channels.FileChannel;
+import com.ibm.platform.IFileSystem;
+import com.ibm.platform.Platform;
+
/**
* FileOutputStream is a class whose underlying stream is represented by a file
* in the operating system. The bytes that are written to this stream are passed
@@ -41,13 +43,8 @@
// initialized).
private FileChannel channel;
- // Fill in the JNI id caches
- private static native void oneTimeInitialization();
+ private IFileSystem fileSystem = Platform.getFileSystem();
- static {
- oneTimeInitialization();
- }
-
/**
* Constructs a new FileOutputStream on the File file. If
* the file exists, it is written over. See the constructor which can append
@@ -90,9 +87,9 @@
if (security != null)
security.checkWrite(file.getPath());
fd = new FileDescriptor();
- if (openImpl(file.properPath(true), append) != 0)
- throw new FileNotFoundException(file.getPath());
-
+ fd.descriptor = fileSystem.open(file.properPath(true), append?IFileSystem.O_APPEND:IFileSystem.O_WRONLY);
+ channel = FileChannelFactory.getFileChannel(this, fd.descriptor,
+ append? IFileSystem.O_APPEND:IFileSystem.O_WRONLY);
}
/**
@@ -107,14 +104,16 @@
*/
public FileOutputStream(FileDescriptor fd) {
super();
- if (fd != null) {
- SecurityManager security = System.getSecurityManager();
- if (security != null)
- security.checkWrite(fd);
- this.fd = fd;
- } else
- throw new NullPointerException(com.ibm.oti.util.Msg
- .getString("K006c")); //$NON-NLS-1$
+ if (fd == null) {
+ throw new NullPointerException(com.ibm.oti.util.Msg
+ .getString("K006c")); //$NON-NLS-1$
+ }
+ SecurityManager security = System.getSecurityManager();
+ if (security != null)
+ security.checkWrite(fd);
+ this.fd = fd;
+ channel = FileChannelFactory.getFileChannel(this, fd.descriptor,
+ IFileSystem.O_WRONLY);
}
/**
@@ -151,15 +150,7 @@
*/
public FileOutputStream(String filename, boolean append)
throws FileNotFoundException {
- super();
- SecurityManager security = System.getSecurityManager();
- if (security != null)
- security.checkWrite(filename);
- fd = new FileDescriptor();
- File f = new File(filename);
- if (openImpl(f.properPath(true), append) != 0)
- throw new FileNotFoundException(filename);
-
+ this(new File(filename), append);
}
/**
@@ -170,11 +161,17 @@
* If an error occurs attempting to close this FileOutputStream.
*/
public void close() throws IOException {
- closeImpl();
+ synchronized (channel) {
+ synchronized (this) {
+ //FIXME: System.in, out, err may not want to be closed?
+ if(channel.isOpen() && fd.descriptor >= 0){
+ channel.close();
+ }
+ fd.descriptor = -1;
+ }
+ }
}
- private native void closeImpl() throws IOException;
-
/**
* Frees any resources allocated to represent this FileOutputStream before
* it is garbage collected. This method is called from the Java Virtual
@@ -185,7 +182,6 @@
* FileOutputStream.
*/
protected void finalize() throws IOException {
- if (fd != null)
close();
}
@@ -200,11 +196,7 @@
*
* @return the file channel representation for this FileOutputStream.
*/
- public synchronized FileChannel getChannel() {
- if (channel == null) {
- channel = FileChannelFactory.getFileChannel(fd.descriptor,
- FileChannelFactory.O_WRONLY);
- }
+ public FileChannel getChannel() {
return channel;
}
@@ -219,13 +211,9 @@
* FileDescriptor.
*/
public final FileDescriptor getFD() throws IOException {
- if (fd != null)
return fd;
- throw new IOException();
}
- private native int openImpl(byte[] fileName, boolean openAppend);
-
/**
* Writes the entire contents of the byte array buffer to
* this FileOutputStream.
@@ -262,14 +250,10 @@
* If buffer is null.
*/
public void write(byte[] buffer, int offset, int count) throws IOException {
- if (fd == null)
- throw new IOException();
- writeImpl(buffer, offset, count, getFD().descriptor);
+ openCheck();
+ fileSystem.write(fd.descriptor, buffer, offset, count);
}
- private native void writeImpl(byte[] buffer, int offset, int count,
- long descriptor) throws IOException;
-
/**
* Writes the specified byte oneByte to this
* FileOutputStream. Only the low order byte of oneByte is
@@ -282,14 +266,17 @@
* If an error occurs attempting to write to this
* FileOutputStream.
*/
- public void write(int oneByte) throws IOException {
- if (fd != null) {
- writeByteImpl(oneByte, getFD().descriptor);
- } else
- throw new IOException();
- }
+ public void write(int oneByte) throws IOException {
+ openCheck();
+ byte[] byteArray = new byte[1];
+ byteArray[0] = (byte)oneByte;
+ fileSystem.write(fd.descriptor, byteArray, 0, 1);
+ }
- private native void writeByteImpl(int oneByte, long descriptor)
- throws IOException;
+ private synchronized void openCheck() throws IOException {
+ if (fd.descriptor < 0) {
+ throw new IOException();
+ }
+ }
}