Index: luni/src/main/java/java/io/FileInputStream.java =================================================================== --- luni/src/main/java/java/io/FileInputStream.java (revision 427081) +++ luni/src/main/java/java/io/FileInputStream.java (working copy) @@ -38,7 +38,7 @@ // The unique file channel associated with this FileInputStream (lazily // initialized). private FileChannel channel; - + boolean innerFD; private IFileSystem fileSystem = Platform.getFileSystem(); private Object repositioningLock = new Object(); @@ -65,6 +65,7 @@ fd = new FileDescriptor(); fd.descriptor = fileSystem.open(file.properPath(true), IFileSystem.O_RDONLY); + innerFD = true; channel = FileChannelFactory.getFileChannel(this, fd.descriptor, IFileSystem.O_RDONLY); } @@ -90,6 +91,7 @@ if (security != null) security.checkRead(fd); this.fd = fd; + innerFD = false; channel = FileChannelFactory.getFileChannel(this, fd.descriptor, IFileSystem.O_RDONLY); } @@ -150,31 +152,18 @@ // to close return; } - if (channel == null) { - /* - * if channel is null, then the channel doesn't need be taken care - * of but the underlying file has been opened - */ - synchronized (this) { - if (fd.descriptor >= 0) { - fileSystem.close(fd.descriptor); - } - fd.descriptor = -1; - } - } else { - /* - * if the FileInputStream is constructed sucessfully, then channel - * must be closed, which will close the underlying file - */ + if (channel != null) { 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; + if (channel.isOpen()) { + channel.close(); } } + } + synchronized (this) { + if (fd.descriptor >= 0 && innerFD) { + fileSystem.close(fd.descriptor); + fd.descriptor = -1; + } } } Index: luni/src/main/java/java/io/FileOutputStream.java =================================================================== --- luni/src/main/java/java/io/FileOutputStream.java (revision 427081) +++ luni/src/main/java/java/io/FileOutputStream.java (working copy) @@ -40,7 +40,7 @@ * The FileDescriptor representing this FileOutputStream. */ FileDescriptor fd; - + boolean innerFD; // The unique file channel associated with this FileInputStream (lazily // initialized). private FileChannel channel; @@ -90,6 +90,7 @@ security.checkWrite(file.getPath()); fd = new FileDescriptor(); fd.descriptor = fileSystem.open(file.properPath(true), append?IFileSystem.O_APPEND:IFileSystem.O_WRONLY); + innerFD = true; channel = FileChannelFactory.getFileChannel(this, fd.descriptor, append? IFileSystem.O_APPEND:IFileSystem.O_WRONLY); } @@ -114,6 +115,7 @@ if (security != null) security.checkWrite(fd); this.fd = fd; + innerFD = false; channel = FileChannelFactory.getFileChannel(this, fd.descriptor, IFileSystem.O_WRONLY); } @@ -168,32 +170,19 @@ // to close return; } - if (channel == null) { - /* - * if channel is null, then the channel doesn't need be taken care - * of but the underlying file has been opened - */ - synchronized (this) { - if (fd.descriptor >= 0) { - fileSystem.close(fd.descriptor); - } - fd.descriptor = -1; - } - } else { - /* - * if the FileOutputStream is constructed sucessfully, then channel - * must be closed, which will close the underlying file - */ + if (channel != null) { 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; + if (channel.isOpen() && fd.descriptor >= 0) { + channel.close(); } } } + synchronized (this) { + if (fd.descriptor >= 0 && innerFD) { + fileSystem.close(fd.descriptor); + fd.descriptor = -1; + } + } } /** Index: luni/src/main/java/java/io/RandomAccessFile.java =================================================================== --- luni/src/main/java/java/io/RandomAccessFile.java (revision 427081) +++ luni/src/main/java/java/io/RandomAccessFile.java (working copy) @@ -146,14 +146,17 @@ * If an error occurs attempting to close this RandomAccessFile. */ public void close() throws IOException { - synchronized (channel) { - synchronized (this) { - if(channel.isOpen() && fd.descriptor >= 0){ + synchronized (channel) { + if(channel.isOpen()){ channel.close(); } - fd.descriptor = -1; } - } + synchronized (this) { + if(fd != null && fd.descriptor >= 0){ + fileSystem.close(fd.descriptor); + fd.descriptor = -1; + } + } } /** Index: nio/src/main/java/org/apache/harmony/nio/internal/FileChannelImpl.java =================================================================== --- nio/src/main/java/org/apache/harmony/nio/internal/FileChannelImpl.java (revision 427076) +++ nio/src/main/java/org/apache/harmony/nio/internal/FileChannelImpl.java (working copy) @@ -97,7 +97,7 @@ if (stream instanceof Closeable) { ((Closeable) stream).close(); } - fileSystem.close(handle); +// fileSystem.close(handle); } protected FileLock basicLock(long position, long size, boolean shared,