diff --git a/libjava/java/io/FileDescriptor.java b/libjava/java/io/FileDescriptor.java index 109d2468783..be86593e76d 100644 --- a/libjava/java/io/FileDescriptor.java +++ b/libjava/java/io/FileDescriptor.java @@ -39,7 +39,9 @@ exception statement from your version. */ package java.io; -import gnu.classpath.Configuration; +import java.nio.channels.ByteChannel; +import java.nio.channels.FileChannel; +import gnu.java.nio.channels.FileChannelImpl; /** * This class represents an opaque file handle as a Java class. It should @@ -57,82 +59,35 @@ public final class FileDescriptor * stream. This will usually be accessed through the * System.invariable. */ - public static final FileDescriptor in = null; + public static final FileDescriptor in + = new FileDescriptor (FileChannelImpl.in); /** * A FileDescriptor representing the system standard output * stream. This will usually be accessed through the * System.outvariable. */ - public static final FileDescriptor out = null; + public static final FileDescriptor out + = new FileDescriptor (FileChannelImpl.out); /** * A FileDescriptor representing the system standard error * stream. This will usually be accessed through the * System.errvariable. */ - public static final FileDescriptor err = null; + public static final FileDescriptor err + = new FileDescriptor (FileChannelImpl.err); - private static native void init(); - - static - { - if (Configuration.INIT_LOAD_LIBRARY) - { - System.loadLibrary("javaio"); - } - - init(); - } - - // These are WHENCE values for seek. - static final int SET = 0; - static final int CUR = 1; - - // These are mode values for open(). - static final int READ = 1; - static final int WRITE = 2; - static final int APPEND = 4; - - // EXCL is used only when making a temp file. - static final int EXCL = 8; - static final int SYNC = 16; - static final int DSYNC = 32; - - /** - * This is the actual native file descriptor value - */ - // System's notion of file descriptor. It might seem redundant to - // initialize this given that it is reassigned in the constructors. - // However, this is necessary because if open() throws an exception - // we want to make sure this has the value -1. This is the most - // efficient way to accomplish that. - private int fd = -1; - - private long position = 0; + final ByteChannel channel; /** * This method is used to initialize an invalid FileDescriptor object. */ - public FileDescriptor() + FileDescriptor(ByteChannel channel) { + this.channel = channel; } - // Open a file. MODE is a combination of the above mode flags. - FileDescriptor (String path, int mode) throws FileNotFoundException - { - fd = open (path, mode); - } - - // Attach to an already-opened file. This is not private because we - // need access to it from other packages, for instance java.net. - // Ordinarily that wouldn't work, either, but in our case we know - // the access comes from C++, where "package private" is translated - // into "public". Eww. - FileDescriptor (int desc) - { - fd = desc; - } /** * This method forces all data that has not yet been physically written to @@ -143,7 +98,23 @@ public final class FileDescriptor * support this functionality or if an error occurs, then an exception * will be thrown. */ - public native void sync() throws SyncFailedException; + public void sync () throws SyncFailedException + { + if (channel instanceof FileChannel) + { + try + { + ((FileChannel) channel).force(true); + } + catch (IOException ex) + { + if (ex instanceof SyncFailedException) + throw (SyncFailedException) ex; + else + throw new SyncFailedException(ex.getMessage()); + } + } + } /** * This methods tests whether or not this object represents a valid open @@ -152,70 +123,8 @@ public final class FileDescriptor * @return true if this object represents a valid * native file handle, false otherwise */ - public native boolean valid(); - - /** - * Opens the specified file in the specified mode. This can be done - * in one of the specified modes: - *