From ef3916ef8e8a15b800e293ed2555b1b60c9f96f3 Mon Sep 17 00:00:00 2001 From: Per Bothner Date: Sun, 29 Feb 2004 11:12:15 -0800 Subject: FileDescriptor.java: Implement on top of FileChannel. * java/io/FileDescriptor.java: Implement on top of FileChannel. Remove native methods. * java/io/natFileDescriptorEcos.cc: Remove file. * java/io/natFileDescriptorPosix.cc: Remove file. * java/io/natFileDescriptorWin32.cc: Remove file. * java/io/FileInputStream.java (ch): Change type to FileChannelImpl. ((File)): Allocate a FileChannelImpl, not a FileDescriptor. ((FileChannelImpl)): New package-private constructor. ((FileDescriptor)): Extract FileChannelImpl from arg. (available, close, read, skip): Implement using FileChannelImpl. (getFD): Allocate FileDescriptor if needed. (getChannel): Is now trivial. * java/io/FileOutputStream.java: Corresponding changes. * java/io/RandomAccessFile.java: Corresponding changes. From-SVN: r78661 --- libjava/java/io/FileDescriptor.java | 153 ++++++++---------------------------- 1 file changed, 31 insertions(+), 122 deletions(-) (limited to 'libjava/java/io/FileDescriptor.java') 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() - { - } - - // Open a file. MODE is a combination of the above mode flags. - FileDescriptor (String path, int mode) throws FileNotFoundException + FileDescriptor(ByteChannel channel) { - fd = open (path, mode); + this.channel = channel; } - // 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: - *
    - *
  • r - Read Only - *
  • rw - Read / Write - *
  • ra - Read / Write - append to end of file - *
  • rws - Read / Write - synchronous writes of data/metadata - *
  • rwd - Read / Write - synchronous writes of data. - * - * @param path Name of the file to open - * @param mode Mode to open - * - * @exception IOException If an error occurs. - */ - native int open(String path, int mode) throws FileNotFoundException; - - /** - * Close the file descriptor. - */ - native void close() throws IOException; - - /** - * Write oe byte of data. - */ - native void write(int b) throws IOException; - - /** - * Write data. - */ - native void write(byte[] b, int offset, int len) - throws IOException, NullPointerException, IndexOutOfBoundsException; - - /** - * Read one byte of data. - */ - native int read() throws IOException; - - /** - * Read data. - */ - native int read(byte[] bytes, int offset, int len) throws IOException; - native int available() throws IOException; - - // EOF_TRUNC is true if a request to seek past the end of file - // should actually stop at the end of file. If false, then a seek - // past the end is ok (and if a subsequent write occurs the file - // will grow). - native int seek(long pos, int whence, boolean eof_trunc) throws IOException; - - native long getFilePointer() throws IOException; - native long getLength() throws IOException; - native void setLength(long pos) throws IOException; - - native void lock(long pos, int len, boolean shared) throws IOException; - native boolean tryLock(long pos, int lent, boolean shared) throws IOException; - native void unlock(long pos, int len) throws IOException; - - // When collected, close. - protected void finalize() throws Throwable + public boolean valid () { - if (valid()) - close(); + return channel.isOpen(); } } -- cgit v1.2.3