This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
PATCH for java.nio FileChannel and MappedByteBuffer
- From: Per Bothner <per at bothner dot com>
- To: java-patches at gcc dot gnu dot org, gcc-patches at gcc dot gnu dot org
- Date: Thu, 19 Feb 2004 17:43:07 -0800
- Subject: PATCH for java.nio FileChannel and MappedByteBuffer
This is the last part of my java.nio patches.
The executive summary:
Working implementations of mapped I/o (at least
read mode) on Posix and Win32. [At least it
worked for earlier versions of the patch.]
FileDescriptor is now implemented in terms of
FileChannelImpl rather than the other way round.
Thus FileInputStream etc are implemented using
FileChannelImpl rather than FileDescriptor.
Hence FileChannelImpl had to be moved to the
gnu.java hierarchy, so it could be public.
The main reason for this change is that java.nio
is intended for more advanced and/or higher-performance
I/O needs, so it it seems wasteful to have them run
on top of the older FileDescriptor code.
You may also note that I don't use symlinks to
select between the natFileChannel{Posix,Win32,Ecos}.cc,
but instead use teh pre-processor.
--
--Per Bothner
per@bothner.com http://per.bothner.com/
? java/io/patched
Index: ChangeLog
===================================================================
RCS file: /cvs/gcc/gcc/libjava/ChangeLog,v
retrieving revision 1.2666
diff -u -p -r1.2666 ChangeLog
--- ChangeLog 16 Feb 2004 20:00:32 -0000 1.2666
+++ ChangeLog 20 Feb 2004 01:27:52 -0000
@@ -1,3 +1,52 @@
+2004-02-17 Per Bothner <per@bothner.com>
+
+ * gnu/java/nio/FileLockImpl.java (fd): Remove field, replacing it by:
+ (ch): New FileChannelImpl field. Update constructor to match.
+ (releaseImpl): Remove native method. Instead ...
+ (release): Call unlock on channel.
+ * gnu/java/nio/natFileLockImpl.cc: Removed file.
+
+ * java/nio/channels/FileChannelImpl.java: Moved to package
+ gnu/java/nio/channels, since we need to refer to it from java.io.
+ * java/nio/channels/natFileChannelImpl.cc: Removed file.
+ * gnu/java/nio/channels/FileChannelImpl.java: New class, renamaed
+ from java/nio/channels. Don't depend on FileDescriptor.
+ (in, out, err): New static fields.
+ (mode): New field.
+ (SET, CUR, READ, WRITE, APPEND, EXCL, SYNC, DSYNC): Moved constants
+ from FileDescriptor.
+ (by): Removed MappedByteBuffer field.
+ (map): New working implementation.
+ * gnu/java/nio/channels/natFileChannelImpl.cc: New file.
+ * gnu/java/nio/channels/natFileChannelPosix.cc: New file, though
+ some code "ported" from natFileDescriptoPosix.cc.
+ * gnu/java/nio/channels/natFileChannelEcos.cc: Likewise.
+ * gnu/java/nio/channels/natFileChannelWin32.cc Likewise.
+ * 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.
+ (<init>(File)): Allocate a FileChannelImpl, not a FileDescriptor.
+ (<init>(FileChannelImpl)): New package-private constructor.
+ (<init>(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.
+ * java/lang/natPosixProcess.cc (startProcess): Implement standard
+ streams using FileChannelImpl, not FileDescriptor.
+ * java/lang/natWin32Process.cc (startProcess): Likewise.
+ * java/nio/MappedByteBuffer.java: (forceImpl, isLoadedImpl, loadImpl,
+ unmapImpl): New dummy methods, to be overridden by subclass.
+ (finalize, isLoaded, load, force): New methods.
+ * java/nio/MappedByteBufferImpl.java: More-or-less rewrite.
+ Now works, at least for read mapping.
+ * Makefile.am: Update accordingly.
+ * configure.in: Don't link java/io/natFileDescriptor.cc.
+
2004-02-16 Per Bothner <per@bothner.com>
* java/nio/CharBufferImpl.java: Inline super constructor.
Index: Makefile.am
===================================================================
RCS file: /cvs/gcc/gcc/libjava/Makefile.am,v
retrieving revision 1.361
diff -u -p -r1.361 Makefile.am
--- Makefile.am 12 Feb 2004 07:10:06 -0000 1.361
+++ Makefile.am 20 Feb 2004 01:27:53 -0000
@@ -2334,6 +2334,7 @@ gnu/java/nio/ServerSocketChannelImpl.jav
gnu/java/nio/ServerSocketChannelSelectionKey.java \
gnu/java/nio/SocketChannelImpl.java \
gnu/java/nio/SocketChannelSelectionKey.java \
+gnu/java/nio/channels/FileChannelImpl.java \
gnu/java/nio/charset/ISO_8859_1.java \
gnu/java/nio/charset/Provider.java \
gnu/java/nio/charset/US_ASCII.java \
@@ -2486,7 +2487,6 @@ java/nio/channels/ClosedSelectorExceptio
java/nio/channels/ConnectionPendingException.java \
java/nio/channels/DatagramChannel.java \
java/nio/channels/FileChannel.java \
-java/nio/channels/FileChannelImpl.java \
java/nio/channels/FileLock.java \
java/nio/channels/FileLockInterruptionException.java \
java/nio/channels/GatheringByteChannel.java \
@@ -2798,12 +2798,11 @@ gnu/java/awt/natEmbeddedWindow.cc \
gnu/java/net/natPlainDatagramSocketImpl.cc \
gnu/java/net/natPlainSocketImpl.cc \
gnu/java/net/protocol/core/natCoreInputStream.cc \
-gnu/java/nio/natFileLockImpl.cc \
gnu/java/nio/natPipeImpl.cc \
gnu/java/nio/natSelectorImpl.cc \
gnu/java/nio/natNIOServerSocket.cc \
+gnu/java/nio/channels/natFileChannelImpl.cc \
java/io/natFile.cc \
-java/io/natFileDescriptor.cc \
java/io/natObjectInputStream.cc \
java/io/natVMObjectStreamClass.cc \
java/lang/natCharacter.cc \
@@ -2830,7 +2829,6 @@ java/lang/reflect/natProxy.cc \
java/net/natNetworkInterface.cc \
java/net/natInetAddress.cc \
java/nio/natDirectByteBufferImpl.cc \
-java/nio/channels/natFileChannelImpl.cc \
java/text/natCollator.cc \
java/util/natResourceBundle.cc \
java/util/natTimeZone.cc \
Index: Makefile.in
===================================================================
RCS file: /cvs/gcc/gcc/libjava/Makefile.in,v
retrieving revision 1.385
diff -u -p -r1.385 Makefile.in
--- Makefile.in 12 Feb 2004 07:10:06 -0000 1.385
+++ Makefile.in 20 Feb 2004 01:27:55 -0000
@@ -2032,6 +2032,7 @@ gnu/java/nio/ServerSocketChannelImpl.jav
gnu/java/nio/ServerSocketChannelSelectionKey.java \
gnu/java/nio/SocketChannelImpl.java \
gnu/java/nio/SocketChannelSelectionKey.java \
+gnu/java/nio/channels/FileChannelImpl.java \
gnu/java/nio/charset/ISO_8859_1.java \
gnu/java/nio/charset/Provider.java \
gnu/java/nio/charset/US_ASCII.java \
@@ -2184,7 +2185,6 @@ java/nio/channels/ClosedSelectorExceptio
java/nio/channels/ConnectionPendingException.java \
java/nio/channels/DatagramChannel.java \
java/nio/channels/FileChannel.java \
-java/nio/channels/FileChannelImpl.java \
java/nio/channels/FileLock.java \
java/nio/channels/FileLockInterruptionException.java \
java/nio/channels/GatheringByteChannel.java \
@@ -2495,12 +2495,11 @@ gnu/java/awt/natEmbeddedWindow.cc \
gnu/java/net/natPlainDatagramSocketImpl.cc \
gnu/java/net/natPlainSocketImpl.cc \
gnu/java/net/protocol/core/natCoreInputStream.cc \
-gnu/java/nio/natFileLockImpl.cc \
gnu/java/nio/natPipeImpl.cc \
gnu/java/nio/natSelectorImpl.cc \
gnu/java/nio/natNIOServerSocket.cc \
+gnu/java/nio/channels/natFileChannelImpl.cc \
java/io/natFile.cc \
-java/io/natFileDescriptor.cc \
java/io/natObjectInputStream.cc \
java/io/natVMObjectStreamClass.cc \
java/lang/natCharacter.cc \
@@ -2527,7 +2526,6 @@ java/lang/reflect/natProxy.cc \
java/net/natNetworkInterface.cc \
java/net/natInetAddress.cc \
java/nio/natDirectByteBufferImpl.cc \
-java/nio/channels/natFileChannelImpl.cc \
java/text/natCollator.cc \
java/util/natResourceBundle.cc \
java/util/natTimeZone.cc \
@@ -2677,9 +2675,9 @@ gnu/java/awt/natEmbeddedWindow.lo \
gnu/java/net/natPlainDatagramSocketImpl.lo \
gnu/java/net/natPlainSocketImpl.lo \
gnu/java/net/protocol/core/natCoreInputStream.lo \
-gnu/java/nio/natFileLockImpl.lo gnu/java/nio/natPipeImpl.lo \
+gnu/java/nio/natPipeImpl.lo \
gnu/java/nio/natSelectorImpl.lo gnu/java/nio/natNIOServerSocket.lo \
-java/io/natFile.lo java/io/natFileDescriptor.lo \
+java/io/natFile.lo \
java/io/natObjectInputStream.lo java/io/natVMObjectStreamClass.lo \
java/lang/natCharacter.lo java/lang/natClass.lo \
java/lang/natClassLoader.lo java/lang/natConcreteProcess.lo \
@@ -2692,7 +2690,7 @@ java/lang/reflect/natArray.lo java/lang/
java/lang/reflect/natField.lo java/lang/reflect/natMethod.lo \
java/lang/reflect/natProxy.lo java/net/natNetworkInterface.lo \
java/net/natInetAddress.lo java/nio/natDirectByteBufferImpl.lo \
-java/nio/channels/natFileChannelImpl.lo java/text/natCollator.lo \
+gnu/java/nio/channels/natFileChannelImpl.lo java/text/natCollator.lo \
java/util/natResourceBundle.lo java/util/natTimeZone.lo \
java/util/zip/natDeflater.lo java/util/zip/natInflater.lo
lib_org_xml_sax_la_DEPENDENCIES = libgcj.la
@@ -3273,7 +3271,7 @@ DEP_FILES = .deps/$(srcdir)/$(CONVERT_D
.deps/gnu/java/nio/charset/UTF_16Decoder.P \
.deps/gnu/java/nio/charset/UTF_16Encoder.P \
.deps/gnu/java/nio/charset/UTF_16LE.P \
-.deps/gnu/java/nio/charset/UTF_8.P .deps/gnu/java/nio/natFileLockImpl.P \
+.deps/gnu/java/nio/charset/UTF_8.P \
.deps/gnu/java/nio/natNIOServerSocket.P \
.deps/gnu/java/nio/natPipeImpl.P .deps/gnu/java/nio/natSelectorImpl.P \
.deps/gnu/java/rmi/RMIMarshalledObjectInputStream.P \
@@ -3615,7 +3613,7 @@ DEP_FILES = .deps/$(srcdir)/$(CONVERT_D
.deps/java/io/DataInput.P .deps/java/io/DataInputStream.P \
.deps/java/io/DataOutput.P .deps/java/io/DataOutputStream.P \
.deps/java/io/EOFException.P .deps/java/io/Externalizable.P \
-.deps/java/io/File.P .deps/java/io/FileDescriptor.P \
+.deps/java/io/File.P \
.deps/java/io/FileFilter.P .deps/java/io/FileInputStream.P \
.deps/java/io/FileNotFoundException.P .deps/java/io/FileOutputStream.P \
.deps/java/io/FilePermission.P .deps/java/io/FileReader.P \
@@ -3650,7 +3648,7 @@ DEP_FILES = .deps/$(srcdir)/$(CONVERT_D
.deps/java/io/UnsupportedEncodingException.P \
.deps/java/io/VMObjectStreamClass.P \
.deps/java/io/WriteAbortedException.P .deps/java/io/Writer.P \
-.deps/java/io/natFile.P .deps/java/io/natFileDescriptor.P \
+.deps/java/io/natFile.P \
.deps/java/io/natObjectInputStream.P \
.deps/java/io/natVMObjectStreamClass.P \
.deps/java/lang/AbstractMethodError.P \
@@ -3806,7 +3804,7 @@ DEP_FILES = .deps/$(srcdir)/$(CONVERT_D
.deps/java/nio/channels/ConnectionPendingException.P \
.deps/java/nio/channels/DatagramChannel.P \
.deps/java/nio/channels/FileChannel.P \
-.deps/java/nio/channels/FileChannelImpl.P \
+.deps/gnu/java/nio/channels/FileChannelImpl.P \
.deps/java/nio/channels/FileLock.P \
.deps/java/nio/channels/FileLockInterruptionException.P \
.deps/java/nio/channels/GatheringByteChannel.P \
@@ -3830,7 +3828,7 @@ DEP_FILES = .deps/$(srcdir)/$(CONVERT_D
.deps/java/nio/channels/UnresolvedAddressException.P \
.deps/java/nio/channels/UnsupportedAddressTypeException.P \
.deps/java/nio/channels/WritableByteChannel.P \
-.deps/java/nio/channels/natFileChannelImpl.P \
+.deps/gnu/java/nio/channels/natFileChannelImpl.P \
.deps/java/nio/channels/spi/AbstractInterruptibleChannel.P \
.deps/java/nio/channels/spi/AbstractSelectableChannel.P \
.deps/java/nio/channels/spi/AbstractSelectionKey.P \
Index: configure
===================================================================
RCS file: /cvs/gcc/gcc/libjava/configure,v
retrieving revision 1.203
diff -u -p -r1.203 configure
--- configure 2 Feb 2004 02:18:32 -0000 1.203
+++ configure 20 Feb 2004 01:27:57 -0000
@@ -9037,8 +9037,8 @@ fi; done
EOF
cat >> $CONFIG_STATUS <<EOF
-ac_sources="include/$PLATFORMH java/io/natFile${FILE-${PLATFORM}}.cc java/io/natFileDescriptor${FILE-${PLATFORM}}.cc java/lang/${PLATFORM}Process.java java/lang/nat${PLATFORM}Process.cc java/net/natInetAddress${PLATFORMNET}.cc java/net/natNetworkInterface${PLATFORMNET}.cc gnu/java/net/natPlainSocketImpl${PLATFORMNET}.cc gnu/java/net/natPlainDatagramSocketImpl${PLATFORMNET}.cc gnu/java/nio/natPipeImpl${PLATFORM}.cc gnu/java/nio/natSelectorImpl${PLATFORM}.cc include/$GCHDR include/$THREADH sysdep/$sysdeps_dir/locks.h $SIGNAL_HANDLER $SIGNAL_HANDLER_AUX"
-ac_dests="include/platform.h java/io/natFile.cc java/io/natFileDescriptor.cc java/lang/ConcreteProcess.java java/lang/natConcreteProcess.cc java/net/natInetAddress.cc java/net/natNetworkInterface.cc gnu/java/net/natPlainSocketImpl.cc gnu/java/net/natPlainDatagramSocketImpl.cc gnu/java/nio/natPipeImpl.cc gnu/java/nio/natSelectorImpl.cc include/java-gc.h include/java-threads.h sysdep/locks.h include/java-signal.h include/java-signal-aux.h"
+ac_sources="include/$PLATFORMH java/io/natFile${FILE-${PLATFORM}}.cc java/lang/${PLATFORM}Process.java java/lang/nat${PLATFORM}Process.cc java/net/natInetAddress${PLATFORMNET}.cc java/net/natNetworkInterface${PLATFORMNET}.cc gnu/java/net/natPlainSocketImpl${PLATFORMNET}.cc gnu/java/net/natPlainDatagramSocketImpl${PLATFORMNET}.cc gnu/java/nio/natPipeImpl${PLATFORM}.cc gnu/java/nio/natSelectorImpl${PLATFORM}.cc include/$GCHDR include/$THREADH sysdep/$sysdeps_dir/locks.h $SIGNAL_HANDLER $SIGNAL_HANDLER_AUX"
+ac_dests="include/platform.h java/io/natFile.cc java/lang/ConcreteProcess.java java/lang/natConcreteProcess.cc java/net/natInetAddress.cc java/net/natNetworkInterface.cc gnu/java/net/natPlainSocketImpl.cc gnu/java/net/natPlainDatagramSocketImpl.cc gnu/java/nio/natPipeImpl.cc gnu/java/nio/natSelectorImpl.cc include/java-gc.h include/java-threads.h sysdep/locks.h include/java-signal.h include/java-signal-aux.h"
EOF
cat >> $CONFIG_STATUS <<\EOF
Index: configure.in
===================================================================
RCS file: /cvs/gcc/gcc/libjava/configure.in,v
retrieving revision 1.181
diff -u -p -r1.181 configure.in
--- configure.in 2 Feb 2004 02:18:33 -0000 1.181
+++ configure.in 20 Feb 2004 01:27:57 -0000
@@ -302,13 +302,12 @@ AC_EGREP_HEADER(mktime, time.h, AC_DEFIN
AC_EGREP_HEADER(localtime, time.h, AC_DEFINE(HAVE_LOCALTIME, 1,
[Define is you have 'localtime' in <time.h>]))
-dnl Create the subdirectory for natFileDescriptor.cc, or the attempt
+dnl Create the subdirectory for natFile.cc, or the attempt
dnl to create the link will fail.
test -d java || mkdir java
test -d java/io || mkdir java/io
test -d gnu || mkdir gnu
AC_LINK_FILES(java/io/natFile${FILE-${PLATFORM}}.cc, java/io/natFile.cc)
-AC_LINK_FILES(java/io/natFileDescriptor${FILE-${PLATFORM}}.cc, java/io/natFileDescriptor.cc)
dnl Likewise for ConcreteProcess.java and natConcreteProcess.cc.
test -d java/lang || mkdir java/lang
Index: gnu/java/nio/FileLockImpl.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/gnu/java/nio/FileLockImpl.java,v
retrieving revision 1.7
diff -u -p -r1.7 FileLockImpl.java
--- gnu/java/nio/FileLockImpl.java 23 Jan 2004 15:53:51 -0000 1.7
+++ gnu/java/nio/FileLockImpl.java 20 Feb 2004 01:27:57 -0000
@@ -40,9 +40,9 @@ package gnu.java.nio;
import java.io.FileDescriptor;
import java.io.IOException;
-import java.nio.channels.FileChannel;
-import java.nio.channels.FileLock;
+import java.nio.channels.*;
import gnu.classpath.Configuration;
+import gnu.java.nio.channels.FileChannelImpl;
/**
* @author Michael Koch
@@ -59,13 +59,13 @@ public class FileLockImpl extends FileLo
}
}
- private FileDescriptor fd;
+ private FileChannelImpl ch;
- public FileLockImpl (FileDescriptor fd, FileChannel channel, long position,
+ public FileLockImpl (FileChannelImpl channel, long position,
long size, boolean shared)
{
super (channel, position, size, shared);
- this.fd = fd;
+ ch = channel;
}
public void finalize()
@@ -85,10 +85,8 @@ public class FileLockImpl extends FileLo
return !channel().isOpen();
}
- private native void releaseImpl () throws IOException;
-
public synchronized void release () throws IOException
{
- releaseImpl ();
+ ch.unlock(position(), size());
}
}
Index: gnu/java/nio/natFileLockImpl.cc
===================================================================
RCS file: gnu/java/nio/natFileLockImpl.cc
diff -N gnu/java/nio/natFileLockImpl.cc
--- gnu/java/nio/natFileLockImpl.cc 23 Jan 2004 14:37:08 -0000 1.2
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,24 +0,0 @@
-// natFileLockImpl.cc
-
-/* Copyright (C) 2003 Free Software Foundation
-
- This file is part of libgcj.
-
-This software is copyrighted work licensed under the terms of the
-Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
-details. */
-
-#include <config.h>
-
-#include <jvm.h>
-#include <errno.h>
-
-#include <gnu/java/nio/FileLockImpl.h>
-#include <java/io/FileDescriptor.h>
-#include <java/io/IOException.h>
-
-void
-gnu::java::nio::FileLockImpl::releaseImpl ()
-{
- fd->unlock(position(), size());
-}
Index: gnu/java/nio/channels/FileChannelImpl.java
===================================================================
RCS file: gnu/java/nio/channels/FileChannelImpl.java
diff -N gnu/java/nio/channels/FileChannelImpl.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ gnu/java/nio/channels/FileChannelImpl.java 20 Feb 2004 01:27:57 -0000
@@ -0,0 +1,420 @@
+/* FileChannelImpl.java --
+ Copyright (C) 2002, 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+02111-1307 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package gnu.java.nio.channels;
+
+import gnu.classpath.Configuration;
+import gnu.gcj.RawData;
+import gnu.java.nio.FileLockImpl;
+import java.io.*;
+import java.nio.ByteBuffer;
+import java.nio.MappedByteBuffer;
+import java.nio.channels.*;
+
+/**
+ * This file is not user visible !
+ * But alas, Java does not have a concept of friendly packages
+ * so this class is public.
+ * Instances of this class are created by invoking getChannel
+ * Upon a Input/Output/RandomAccessFile object.
+ */
+
+public class FileChannelImpl extends FileChannel
+{
+ int mode;
+ // 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;
+
+ int length;
+ private long pos;
+
+ public FileChannelImpl ()
+ {
+ }
+
+ /* Open a file. MODE is a combination of the above mode flags. */
+ public FileChannelImpl (String path, int mode) throws FileNotFoundException
+ {
+ fd = open (path, mode);
+ this.mode = mode;
+ }
+
+ private static native void init();
+ static { init (); }
+
+ public static FileChannelImpl in;
+ public static FileChannelImpl out;
+ public static FileChannelImpl err;
+
+ private native int open (String path, int mode) throws FileNotFoundException;
+
+ /** Attach to an already-opened file. */
+ public FileChannelImpl (int desc, int mode)
+ {
+ fd = desc;
+ this.mode = mode;
+ }
+
+ native int available () throws IOException;
+ private native long implPosition ();
+ private native void seek (long newPosition);
+ private native void implTruncate (long size);
+
+ public native void unlock (long pos, long len);
+
+ public native long size () throws IOException;
+
+ protected native void implCloseChannel() throws IOException;
+
+ public int read (ByteBuffer dst) throws IOException
+ {
+ return implRead (dst);
+ }
+
+ public int read (ByteBuffer dst, long position)
+ throws IOException
+ {
+ if (position < 0)
+ throw new IllegalArgumentException ();
+ long oldPosition = implPosition ();
+ position (position);
+ int result = implRead (dst);
+ position (oldPosition);
+
+ return result;
+ }
+
+ private int implRead (ByteBuffer dst) throws IOException
+ {
+ int result;
+ byte[] buffer = new byte [dst.remaining ()];
+
+ result = read (buffer, 0, buffer.length);
+
+ if (result > 0)
+ dst.put (buffer, 0, result);
+
+ return result;
+ }
+
+ public native int read ()
+ throws IOException;
+
+ public native int read (byte[] buffer, int offset, int length)
+ throws IOException;
+
+ public long read (ByteBuffer[] dsts, int offset, int length)
+ throws IOException
+ {
+ long result = 0;
+
+ for (int i = offset; i < offset + length; i++)
+ {
+ result += read (dsts [i]);
+ }
+
+ return result;
+ }
+
+ public int write (ByteBuffer src) throws IOException
+ {
+ return implWrite (src);
+ }
+
+ public int write (ByteBuffer src, long position)
+ throws IOException
+ {
+ if (position < 0)
+ throw new IllegalArgumentException ();
+
+ if (!isOpen ())
+ throw new ClosedChannelException ();
+
+ if ((mode & WRITE) == 0)
+ throw new NonWritableChannelException ();
+
+ int result;
+ long oldPosition;
+
+ oldPosition = implPosition ();
+ seek (position);
+ result = implWrite (src);
+ seek (oldPosition);
+
+ return result;
+ }
+
+ private int implWrite (ByteBuffer src) throws IOException
+ {
+ int len = src.remaining ();
+ if (src.hasArray())
+ {
+ byte[] buffer = src.array();
+ write(buffer, src.arrayOffset() + src.position(), len);
+ }
+ else
+ {
+ // Use a more efficient native method! FIXME!
+ byte[] buffer = new byte [len];
+ src.get (buffer, 0, len);
+ write (buffer, 0, len);
+ }
+ return len;
+ }
+
+ public native void write (byte[] buffer, int offset, int length)
+ throws IOException;
+
+ public native void write (int b) throws IOException;
+
+ public long write(ByteBuffer[] srcs, int offset, int length)
+ throws IOException
+ {
+ long result = 0;
+
+ for (int i = offset;i < offset + length;i++)
+ {
+ result += write (srcs[i]);
+ }
+
+ return result;
+ }
+
+ public native MappedByteBuffer mapImpl (char mode, long position, int size)
+ throws IOException;
+
+ public MappedByteBuffer map (FileChannel.MapMode mode,
+ long position, long size)
+ throws IOException
+ {
+ char nmode = 0;
+ if (mode == MapMode.READ_ONLY)
+ {
+ nmode = 'r';
+ if ((this.mode & READ) == 0)
+ throw new NonReadableChannelException();
+ }
+ else if (mode == MapMode.READ_WRITE || mode == MapMode.PRIVATE)
+ {
+ nmode = mode == MapMode.READ_WRITE ? '+' : 'c';
+ if ((this.mode & (READ|WRITE)) != (READ|WRITE))
+ throw new NonWritableChannelException();
+ }
+ else
+ throw new IllegalArgumentException ();
+
+ if (position < 0 || size < 0 || size > Integer.MAX_VALUE)
+ throw new IllegalArgumentException ();
+ return mapImpl(nmode, position, (int) size);
+ }
+
+ /**
+ * msync with the disk
+ */
+ public void force (boolean metaData) throws IOException
+ {
+ if (!isOpen ())
+ throw new ClosedChannelException ();
+ }
+
+ public long transferTo (long position, long count, WritableByteChannel target)
+ throws IOException
+ {
+ if (position < 0
+ || count < 0)
+ throw new IllegalArgumentException ();
+
+ if (!isOpen ())
+ throw new ClosedChannelException ();
+
+ if ((mode & READ) == 0)
+ throw new NonReadableChannelException ();
+
+ // XXX: count needs to be casted from long to int. Dataloss ?
+ ByteBuffer buffer = ByteBuffer.allocate ((int) count);
+ read (buffer, position);
+ buffer.flip();
+ return target.write (buffer);
+ }
+
+ public long transferFrom (ReadableByteChannel src, long position, long count)
+ throws IOException
+ {
+ if (position < 0
+ || count < 0)
+ throw new IllegalArgumentException ();
+
+ if (!isOpen ())
+ throw new ClosedChannelException ();
+
+ if ((mode & WRITE) == 0)
+ throw new NonWritableChannelException ();
+
+ // XXX: count needs to be casted from long to int. Dataloss ?
+ ByteBuffer buffer = ByteBuffer.allocate ((int) count);
+ src.read (buffer);
+ buffer.flip();
+ return write (buffer, position);
+ }
+
+ public FileLock tryLock (long position, long size, boolean shared)
+ throws IOException
+ {
+ if (position < 0
+ || size < 0)
+ throw new IllegalArgumentException ();
+
+ if (!isOpen ())
+ throw new ClosedChannelException ();
+
+ if (shared && (mode & READ) == 0)
+ throw new NonReadableChannelException ();
+
+ if (!shared && (mode & WRITE) == 0)
+ throw new NonWritableChannelException ();
+
+ boolean completed = false;
+
+ try
+ {
+ begin();
+ lock(position, size, shared, true);
+ completed = true;
+ return new FileLockImpl(this, position, size, shared);
+ }
+ finally
+ {
+ end(completed);
+ }
+ }
+
+ /** Try to acquire a lock at the given position and size.
+ * On success return true.
+ * If wait as specified, block until we can get it.
+ * Otherwise return false.
+ */
+ private native boolean lock(long position, long size,
+ boolean shared, boolean wait);
+
+ public FileLock lock (long position, long size, boolean shared)
+ throws IOException
+ {
+ if (position < 0
+ || size < 0)
+ throw new IllegalArgumentException ();
+
+ if (!isOpen ())
+ throw new ClosedChannelException ();
+
+ boolean completed = false;
+
+ try
+ {
+ boolean lockable = lock(position, size, shared, false);
+ completed = true;
+ return (lockable
+ ? new FileLockImpl(this, position, size, shared)
+ : null);
+ }
+ finally
+ {
+ end(completed);
+ }
+ }
+
+ public long position ()
+ throws IOException
+ {
+ if (!isOpen ())
+ throw new ClosedChannelException ();
+
+ return implPosition ();
+ }
+
+ public FileChannel position (long newPosition)
+ throws IOException
+ {
+ if (newPosition < 0)
+ throw new IllegalArgumentException ();
+
+ if (!isOpen ())
+ throw new ClosedChannelException ();
+
+ // FIXME note semantics if seeking beyond eof.
+ // We should seek lazily - only on a write.
+ seek (newPosition);
+ return this;
+ }
+
+ public FileChannel truncate (long size)
+ throws IOException
+ {
+ if (size < 0)
+ throw new IllegalArgumentException ();
+
+ if (!isOpen ())
+ throw new ClosedChannelException ();
+
+ if ((mode & WRITE) == 0)
+ throw new NonWritableChannelException ();
+
+ implTruncate (size);
+ return this;
+ }
+}
Index: gnu/java/nio/channels/natFileChannelEcos.cc
===================================================================
RCS file: gnu/java/nio/channels/natFileChannelEcos.cc
diff -N gnu/java/nio/channels/natFileChannelEcos.cc
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ gnu/java/nio/channels/natFileChannelEcos.cc 20 Feb 2004 01:27:57 -0000
@@ -0,0 +1,159 @@
+// natFileDescriptor.cc - Native part of FileDescriptor class.
+
+/* Copyright (C) 1998, 1999, 2001, 2002 Free Software Foundation
+
+ This file is part of libgcj.
+
+This software is copyrighted work licensed under the terms of the
+Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
+details. */
+
+#include <config.h>
+
+#include <errno.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/param.h>
+
+#include <gcj/cni.h>
+#include <jvm.h>
+#include <java/io/FileDescriptor.h>
+#include <java/io/SyncFailedException.h>
+#include <java/io/IOException.h>
+#include <java/io/EOFException.h>
+#include <java/lang/ArrayIndexOutOfBoundsException.h>
+#include <java/lang/NullPointerException.h>
+#include <java/lang/String.h>
+#include <java/io/FileNotFoundException.h>
+
+extern "C" void diag_write_char (char c);
+
+static void
+diag_write (char *data, int len)
+{
+ while (len > 0)
+ {
+ diag_write_char (*data++);
+ len--;
+ }
+}
+
+#define NO_FSYNC_MESSAGE "sync unsupported"
+
+void
+java::io::FileDescriptor::init(void)
+{
+ in = new java::io::FileDescriptor(0);
+ out = new java::io::FileDescriptor(1);
+ err = new java::io::FileDescriptor(2);
+}
+
+jboolean
+java::io::FileDescriptor::valid (void)
+{
+ return true;
+}
+
+void
+java::io::FileDescriptor::sync (void)
+{
+ // Some files don't support fsync. We don't bother reporting these
+ // as errors.
+#ifdef HAVE_FSYNC
+#else
+ throw new SyncFailedException (JvNewStringLatin1 (NO_FSYNC_MESSAGE));
+#endif
+}
+
+jint
+java::io::FileDescriptor::open (jstring path, jint jflags)
+{
+ return fd;
+}
+
+void
+java::io::FileDescriptor::write (jint b)
+{
+ char d = (char) b;
+ ::diag_write (&d, 1);
+}
+
+void
+java::io::FileDescriptor::write (jbyteArray b, jint offset, jint len)
+{
+ if (! b)
+ throw new java::lang::NullPointerException;
+ if (offset < 0 || len < 0 || offset + len > JvGetArrayLength (b))
+ throw new java::lang::ArrayIndexOutOfBoundsException;
+ char *bytes = (char *)elements (b) + offset;
+ ::diag_write (bytes, len);
+}
+
+void
+java::io::FileDescriptor::close (void)
+{
+}
+
+void
+java::io::FileDescriptor::setLength (long)
+{
+}
+
+jint
+java::io::FileDescriptor::seek (jlong pos, jint whence, jboolean)
+{
+ JvAssert (whence == SET || whence == CUR);
+ return 0;
+}
+
+jlong
+java::io::FileDescriptor::getLength (void)
+{
+ return 0;
+}
+
+jlong
+java::io::FileDescriptor::getFilePointer (void)
+{
+ return 0;
+}
+
+jint
+java::io::FileDescriptor::read (void)
+{
+ return 0;
+}
+
+jint
+java::io::FileDescriptor::read (jbyteArray buffer, jint offset, jint count)
+{
+ return 0;
+}
+
+jint
+java::io::FileDescriptor::available (void)
+{
+ return 0;
+}
+
+void
+java::io::FileDescriptor::lock (jlong pos, jint len, jboolean shared)
+{
+ throw new IOException (JvNewStringLatin1
+ ("java.io.FileDescriptor.lock() not implemented"));
+}
+
+jboolean
+java::io::FileDescriptor::tryLock (jlong pos, jint len, jboolean shared)
+{
+ throw new IOException (JvNewStringLatin1
+ ("java.io.FileDescriptor.tryLock() not implemented"));
+}
+
+void
+java::io::FileDescriptor::unlock (jlong pos, jint len)
+{
+ throw new IOException (JvNewStringLatin1
+ ("java.io.FileDescriptor.unlock() not implemented"));
+}
Index: gnu/java/nio/channels/natFileChannelImpl.cc
===================================================================
RCS file: gnu/java/nio/channels/natFileChannelImpl.cc
diff -N gnu/java/nio/channels/natFileChannelImpl.cc
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ gnu/java/nio/channels/natFileChannelImpl.cc 20 Feb 2004 01:27:57 -0000
@@ -0,0 +1,177 @@
+// natFileChannelImpl.cc
+
+/* Copyright (C) 2003, 2004 Free Software Foundation
+
+ This file is part of libgcj.
+
+This software is copyrighted work licensed under the terms of the
+Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
+details. */
+
+#include <config.h>
+#include <platform.h>
+
+#include <gcj/cni.h>
+#include <gcj/javaprims.h>
+#include <jvm.h>
+
+#include <gnu/gcj/RawData.h>
+#include <gnu/java/nio/FileLockImpl.h>
+#include <gnu/java/nio/channels/FileChannelImpl.h>
+#include <java/io/FileNotFoundException.h>
+#include <java/io/IOException.h>
+#include <java/io/SyncFailedException.h>
+#include <java/io/InterruptedIOException.h>
+#include <java/io/EOFException.h>
+#include <java/lang/ArrayIndexOutOfBoundsException.h>
+#include <java/lang/NullPointerException.h>
+#include <java/lang/System.h>
+#include <java/lang/String.h>
+#include <java/lang/Thread.h>
+#include <java/nio/ByteBuffer.h>
+#include <java/nio/MappedByteBufferImpl.h>
+#include <java/nio/channels/FileChannel.h>
+#include <java/nio/channels/FileLock.h>
+
+using gnu::gcj::RawData;
+using gnu::java::nio::channels::FileChannelImpl;
+using java::io::IOException;
+using java::io::InterruptedIOException;
+using java::io::FileNotFoundException;
+using java::lang::ArrayIndexOutOfBoundsException;
+using java::nio::MappedByteBufferImpl;
+
+#include <errno.h>
+#include <string.h>
+#include <sys/types.h>
+
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#ifdef HAVE_FCNTL_H
+#include <fcntl.h>
+#endif
+
+#if defined(__JV_POSIX_H__)
+#ifdef HAVE_MMAP
+#include <sys/mman.h>
+#endif
+#include "natFileChannelPosix.cc"
+#elif defined(__JV_WIN32_H__)
+#include "natFileChannelWin32.cc"
+#else
+#include "natFileChannelEcos.cc"
+#endif
+
+java::nio::MappedByteBuffer *
+FileChannelImpl::mapImpl (jchar mmode, jlong position, jint size)
+{
+#if defined(__JV_POSIX_H__) && defined(HAVE_MMAP)
+ int prot, flags;
+ if (mmode == 'r')
+ {
+ prot = PROT_READ;
+ flags = MAP_PRIVATE;
+ }
+ else
+ {
+ prot = PROT_READ|PROT_WRITE;
+ flags = mmode == '+' ? MAP_SHARED : MAP_PRIVATE;
+ }
+ jint page_size = ::getpagesize();
+ jint offset = position & ~(page_size-1);
+ jint align = position - offset;
+ void* ptr = ::mmap(NULL, size + align, prot, flags, fd, offset);
+ MappedByteBufferImpl *buf
+ = new MappedByteBufferImpl ((RawData *) ((char *) ptr + align),
+ size, mmode == 'r');
+ if (ptr == MAP_FAILED)
+ throw new IOException (JvNewStringLatin1 (strerror (errno)));
+ buf->implPtr = reinterpret_cast<RawData*> (ptr);
+ buf->implLen = size+align;
+ return buf;
+#elif defined(__JV_WIN32_H__)
+ SYSTEM_INFO siSysInfo;
+ GetSystemInfo(&siSysInfo);
+ DWORD page_size = siSysInfo.dwPageSize;
+ jlong offset = position & ~(page_size-1);
+ jint align = position - offset;
+ jlong high = position + size;
+ jlong max_size;
+ if (mmode == '+')
+ max_size = high - offset;
+ else
+ max_size = 0;
+ DWORD access, protect;
+ if (mmode == 'r')
+ {
+ access = FILE_MAP_READ;
+ protect = PAGE_READONLY;
+ }
+ else if (mmode == '+')
+ {
+ access = FILE_MAP_WRITE;
+ protect = PAGE_READWRITE;
+ }
+ else
+ {
+ access = FILE_MAP_COPY;
+ protect = PAGE_WRITECOPY;
+ }
+ HANDLE hFileMapping = CreateFileMapping((HANDLE) fd,
+ (LPSECURITY_ATTRIBUTES) NULL,
+ protect,
+ (DWORD) (max_size >> 32),
+ (DWORD) max_size,
+ (LPCTSTR) NULL);
+ if (hFileMapping == NULL)
+ throw new IOException();
+ void *ptr = MapViewOfFile(hFileMapping, access,
+ (DWORD) (offset >> 32), (DWORD) offset,
+ (SIZE_T) (high - offset));
+ if (ptr == NULL)
+ throw new IOException();
+ MappedByteBufferImpl *buf
+ = new MappedByteBufferImpl((RawData *) ((char *) ptr + align),
+ size, mode == 'r');
+ buf->implPtr = reinterpret_cast<RawData*> (ptr);
+ buf->implLen = (jlong) hFileMapping;
+ return buf;
+#else
+ throw new IOException (JvNewStringUTF ("mmap not implemented"));
+#endif
+}
+
+void
+MappedByteBufferImpl::unmapImpl ()
+{
+#if defined(__JV_POSIX_H__) && defined(HAVE_MMAP)
+ munmap((void*) implPtr, implLen);
+#elif defined(__JV_WIN32_H__)
+ UnmapViewOfFile((void*)implPtr);
+ CloseHandle((HANDLE) implLen);
+#else
+#endif
+}
+
+void
+MappedByteBufferImpl::loadImpl ()
+{
+}
+
+jboolean
+MappedByteBufferImpl::isLoadedImpl ()
+{
+ return true;
+}
+
+void
+MappedByteBufferImpl::forceImpl ()
+{
+#if defined(__JV_POSIX_H__) && defined(HAVE_MMAP)
+ ::msync((void*) implPtr, implLen, MS_SYNC);
+#elif defined(__JV_WIN32_H__)
+#endif
+}
+
Index: gnu/java/nio/channels/natFileChannelPosix.cc
===================================================================
RCS file: gnu/java/nio/channels/natFileChannelPosix.cc
diff -N gnu/java/nio/channels/natFileChannelPosix.cc
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ gnu/java/nio/channels/natFileChannelPosix.cc 20 Feb 2004 01:27:57 -0000
@@ -0,0 +1,424 @@
+// natFileChannelImplPosix.cc - Native part of FileChannelImpl class.
+
+/* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation
+
+ This file is part of libgcj.
+
+This software is copyrighted work licensed under the terms of the
+Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
+details. */
+
+#include "posix.h"
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/param.h>
+
+#ifdef HAVE_SYS_IOCTL_H
+#define BSD_COMP /* Get FIONREAD on Solaris2. */
+#include <sys/ioctl.h>
+#endif
+
+// Pick up FIONREAD on Solaris 2.5.
+#ifdef HAVE_SYS_FILIO_H
+#include <sys/filio.h>
+#endif
+
+#define NO_FSYNC_MESSAGE "sync unsupported"
+
+void
+FileChannelImpl::init(void)
+{
+ in = new FileChannelImpl((jint) 0, FileChannelImpl::READ);
+ out = new FileChannelImpl((jint) 1, FileChannelImpl::WRITE);
+ err = new FileChannelImpl((jint) 2, FileChannelImpl::WRITE);
+}
+
+#if 0
+jboolean
+FileChannelImpl::valid (void)
+{
+ struct stat sb;
+ return fd >= 0 && ::fstat (fd, &sb) == 0;
+}
+
+void
+FileChannelImpl::sync (void)
+{
+ // Some files don't support fsync. We don't bother reporting these
+ // as errors.
+#ifdef HAVE_FSYNC
+ if (::fsync (fd) && errno != EROFS && errno != EINVAL)
+ throw new SyncFailedException (JvNewStringLatin1 (strerror (errno)));
+#else
+ throw new SyncFailedException (JvNewStringLatin1 (NO_FSYNC_MESSAGE));
+#endif
+}
+#endif
+
+jint
+FileChannelImpl::open (jstring path, jint jflags)
+{
+ fd = -1;
+ char *buf = (char *) _Jv_AllocBytes (_Jv_GetStringUTFLength (path) + 1);
+ jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
+ buf[total] = '\0';
+ int flags = 0;
+#ifdef O_BINARY
+ flags |= O_BINARY;
+#endif
+
+ JvAssert ((jflags & READ) || (jflags & WRITE));
+ int mode = 0666;
+ if ((jflags & READ) && (jflags & WRITE))
+ flags |= O_RDWR | O_CREAT;
+ else if ((jflags & READ))
+ flags |= O_RDONLY;
+ else
+ {
+ flags |= O_WRONLY | O_CREAT;
+ if ((jflags & APPEND))
+ flags |= O_APPEND;
+ else
+ flags |= O_TRUNC;
+
+ if ((jflags & EXCL))
+ {
+ flags |= O_EXCL;
+ // In this case we are making a temp file.
+ mode = 0600;
+ }
+ }
+
+ if ((jflags & SYNC))
+ flags |= O_SYNC;
+
+ if ((jflags & DSYNC))
+ flags |= O_DSYNC;
+
+ int fd = ::open (buf, flags, mode);
+ if (fd == -1 && errno == EMFILE)
+ {
+ // Because finalize () calls close () we might be able to continue.
+ ::java::lang::System::gc ();
+ ::java::lang::System::runFinalization ();
+ fd = ::open (buf, flags, mode);
+ }
+ if (fd == -1)
+ {
+ char msg[MAXPATHLEN + 200];
+ // We choose the formatting here for JDK compatibility, believe
+ // it or not.
+ sprintf (msg, "%.*s (%.*s)",
+ MAXPATHLEN + 150, buf,
+ 40, strerror (errno));
+ throw new ::java::io::FileNotFoundException (JvNewStringLatin1 (msg));
+ }
+
+ _Jv_platform_close_on_exec (fd);
+
+ return fd;
+}
+
+void
+FileChannelImpl::write (jint b)
+{
+ jbyte d = (jbyte) b;
+ int r = 0;
+ while (r != 1)
+ {
+ r = ::write (fd, &d, 1);
+ if (r == -1)
+ {
+ if (::java::lang::Thread::interrupted())
+ {
+ ::java::io::InterruptedIOException *iioe
+ = new ::java::io::InterruptedIOException (JvNewStringLatin1 (strerror (errno)));
+ iioe->bytesTransferred = r == -1 ? 0 : r;
+ throw iioe;
+ }
+ if (errno != EINTR)
+ throw new IOException (JvNewStringLatin1 (strerror (errno)));
+ }
+ }
+ pos++;
+}
+
+void
+FileChannelImpl::write (jbyteArray b, jint offset, jint len)
+{
+ if (! b)
+ throw new ::java::lang::NullPointerException;
+ if (offset < 0 || len < 0 || offset + len > JvGetArrayLength (b))
+ throw new ArrayIndexOutOfBoundsException;
+ jbyte *bytes = elements (b) + offset;
+
+ int written = 0;
+ while (len > 0)
+ {
+ int r = ::write (fd, bytes, len);
+ if (r == -1)
+ {
+ if (::java::lang::Thread::interrupted())
+ {
+ InterruptedIOException *iioe
+ = new InterruptedIOException (JvNewStringLatin1 (strerror (errno)));
+ iioe->bytesTransferred = written;
+ throw iioe;
+ }
+ if (errno != EINTR)
+ throw new IOException (JvNewStringLatin1 (strerror (errno)));
+ }
+
+ written += r;
+ len -= r;
+ bytes += r;
+ pos += r;
+ }
+}
+
+void
+FileChannelImpl::implCloseChannel (void)
+{
+ jint save = fd;
+ fd = -1;
+ if (::close (save))
+ throw new IOException (JvNewStringLatin1 (strerror (errno)));
+}
+
+void
+FileChannelImpl::implTruncate (jlong size)
+{
+ struct stat sb;
+
+#ifdef HAVE_FTRUNCATE
+ if (::fstat (fd, &sb))
+ throw new IOException (JvNewStringLatin1 (strerror (errno)));
+
+ if ((jlong) sb.st_size == size)
+ return;
+
+ // If the file is too short, we extend it. We can't rely on
+ // ftruncate() extending the file. So we lseek() to 1 byte less
+ // than we want, and then we write a single byte at the end.
+ if ((jlong) sb.st_size < size)
+ {
+ if (::lseek (fd, (off_t) (size - 1), SEEK_SET) == -1)
+ throw new IOException (JvNewStringLatin1 (strerror (errno)));
+ char out = '\0';
+ int r = ::write (fd, &out, 1);
+ if (r <= 0 || ::lseek (fd, pos, SEEK_SET) == -1)
+ throw new IOException (JvNewStringLatin1 (strerror (errno)));
+ }
+ else
+ {
+ if (::ftruncate (fd, (off_t) pos))
+ throw new IOException (JvNewStringLatin1 (strerror (errno)));
+ pos = size;
+ }
+#else /* HAVE_FTRUNCATE */
+ throw new IOException (JvNewStringLatin1 ("FileDescriptor.setLength not implemented"));
+#endif /* HAVE_FTRUNCATE */
+}
+
+void
+FileChannelImpl::seek (jlong newPos)
+{
+ off_t r = ::lseek (fd, (off_t) newPos, SEEK_SET);
+ if (r == -1)
+ throw new IOException (JvNewStringLatin1 (strerror (errno)));
+ pos = r;
+}
+
+jlong
+FileChannelImpl::size (void)
+{
+ struct stat sb;
+ if (::fstat (fd, &sb))
+ throw new IOException (JvNewStringLatin1 (strerror (errno)));
+ return sb.st_size;
+}
+
+jlong
+FileChannelImpl::implPosition (void)
+{
+ return pos;
+}
+
+jint
+FileChannelImpl::read (void)
+{
+ jbyte b;
+ int r;
+ do
+ {
+ r = ::read (fd, &b, 1);
+ if (r == 0)
+ return -1;
+ if (r == -1)
+ {
+ if (::java::lang::Thread::interrupted())
+ {
+ InterruptedIOException *iioe
+ = new InterruptedIOException (JvNewStringLatin1 (strerror (errno)));
+ iioe->bytesTransferred = r == -1 ? 0 : r;
+ throw iioe;
+ }
+ if (errno != EINTR)
+ throw new IOException (JvNewStringLatin1 (strerror (errno)));
+ }
+ }
+ while (r != 1);
+ pos++;
+ return b & 0xFF;
+}
+
+jint
+FileChannelImpl::read (jbyteArray buffer, jint offset, jint count)
+{
+ if (! buffer)
+ throw new ::java::lang::NullPointerException;
+ jsize bsize = JvGetArrayLength (buffer);
+ if (offset < 0 || count < 0 || offset + count > bsize)
+ throw new ::java::lang::ArrayIndexOutOfBoundsException;
+
+ // Must return 0 if an attempt is made to read 0 bytes.
+ if (count == 0)
+ return 0;
+
+ jbyte *bytes = elements (buffer) + offset;
+ int r;
+ do
+ {
+ r = ::read (fd, bytes, count);
+ if (r == 0)
+ return -1;
+ if (r == -1)
+ {
+ if (::java::lang::Thread::interrupted())
+ {
+ InterruptedIOException *iioe
+ = new InterruptedIOException (JvNewStringLatin1 (strerror (errno)));
+ iioe->bytesTransferred = r == -1 ? 0 : r;
+ throw iioe;
+ }
+ if (errno != EINTR)
+ throw new IOException (JvNewStringLatin1 (strerror (errno)));
+ }
+ }
+ while (r <= 0);
+ pos += r;
+ return r;
+}
+
+jint
+FileChannelImpl::available (void)
+{
+#if defined (FIONREAD) || defined (HAVE_SELECT) || defined (HAVE_FSTAT)
+ long num = 0;
+ int r = 0;
+ bool num_set = false;
+
+#if defined (FIONREAD)
+ r = ::ioctl (fd, FIONREAD, &num);
+ if (r == -1 && errno == ENOTTY)
+ {
+ // If the ioctl doesn't work, we don't care.
+ r = 0;
+ num = 0;
+ }
+ else
+ num_set = true;
+#elif defined (HAVE_SELECT)
+ if (fd < 0)
+ {
+ errno = EBADF;
+ r = -1;
+ }
+#endif
+
+ if (r == -1)
+ {
+ posix_error:
+ throw new IOException (JvNewStringLatin1 (strerror (errno)));
+ }
+
+ // If we didn't get anything, and we have fstat, then see if see if
+ // we're reading a regular file. On many systems, FIONREAD does not
+ // work on regular files; select() likewise returns a useless
+ // result. This is run incorrectly when FIONREAD does work on
+ // regular files and we are at the end of the file. However, this
+ // case probably isn't very important.
+#if defined (HAVE_FSTAT)
+ if (! num_set)
+ {
+ struct stat sb;
+ off_t where = 0;
+ if (fstat (fd, &sb) != -1
+ && S_ISREG (sb.st_mode)
+ && (where = lseek (fd, 0, SEEK_CUR)) != (off_t) -1)
+ {
+ num = (long) (sb.st_size - where);
+ num_set = true;
+ }
+ }
+#endif /* HAVE_FSTAT */
+
+#if defined (HAVE_SELECT)
+ if (! num_set)
+ {
+ fd_set rd;
+ FD_ZERO (&rd);
+ FD_SET (fd, &rd);
+ struct timeval tv;
+ tv.tv_sec = 0;
+ tv.tv_usec = 0;
+ r = _Jv_select (fd + 1, &rd, NULL, NULL, &tv);
+ if (r == -1)
+ goto posix_error;
+ num = r == 0 ? 0 : 1;
+ }
+#endif /* HAVE_SELECT */
+
+ return (jint) num;
+#else
+ return 0;
+#endif
+}
+
+jboolean
+FileChannelImpl::lock
+(jlong pos, jlong len, jboolean shared, jboolean wait)
+{
+ struct flock lockdata;
+
+ lockdata.l_type = shared ? F_WRLCK : F_RDLCK;
+ lockdata.l_whence = SEEK_SET;
+ lockdata.l_start = pos;
+ lockdata.l_len = len;
+
+ if (::fcntl (fd, wait ? F_SETLKW : F_SETLK, &lockdata) == -1)
+ {
+ if (! wait && (errno == EACCES || errno == EAGAIN))
+ return false;
+ throw new IOException (JvNewStringLatin1 (strerror (errno)));
+ }
+ return true;
+}
+
+void
+FileChannelImpl::unlock (jlong pos, jlong len)
+{
+ struct flock lockdata;
+
+ lockdata.l_type = F_UNLCK;
+ lockdata.l_whence = SEEK_SET;
+ lockdata.l_start = pos;
+ lockdata.l_len = len;
+
+ if (::fcntl (fd, F_SETLK, &lockdata) == -1)
+ throw new IOException (JvNewStringLatin1 (strerror (errno)));
+}
Index: gnu/java/nio/channels/natFileChannelWin32.cc
===================================================================
RCS file: gnu/java/nio/channels/natFileChannelWin32.cc
diff -N gnu/java/nio/channels/natFileChannelWin32.cc
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ gnu/java/nio/channels/natFileChannelWin32.cc 20 Feb 2004 01:27:58 -0000
@@ -0,0 +1,324 @@
+// natFileChannelImplWin32.cc - Native part of FileChannelImpl class.
+
+/* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software
+ Foundation, Inc.
+
+ This file is part of libgcj.
+
+This software is copyrighted work licensed under the terms of the
+Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
+details. */
+
+// FIXME: In order to support interrupting of IO operations, we
+// need to change to use the windows asynchronous IO functions
+
+#include <stdio.h>
+
+#undef STRICT
+
+static bool testCanUseGetHandleInfo()
+{
+ /* Test to see whether GetHandleInformation can be used
+ for console input or screen buffers. This is better
+ a kludgy OS version check. */
+ DWORD dwFlags;
+ return GetHandleInformation (GetStdHandle (STD_INPUT_HANDLE),
+ &dwFlags) != 0;
+}
+
+// FIXME: casting a FILE (pointer) to a jint will not work on Win64 --
+// we should be using gnu.gcj.RawData's.
+
+void
+FileChannelImpl::init(void)
+{
+ in = new FileChannelImpl((jint)(GetStdHandle (STD_INPUT_HANDLE)),
+ FileChannelImpl::READ);
+ out = new FileChannelImpl((jint)(GetStdHandle (STD_OUTPUT_HANDLE)),
+ FileChannelImpl::WRITE);
+ err = new FileChannelImpl((jint)(GetStdHandle (STD_ERROR_HANDLE)),
+ FileChannelImpl::WRITE);
+}
+
+#if 0
+FileChannelImpl::sync (void) {
+ if (! FlushFileBuffers ((HANDLE)fd))
+ {
+ DWORD dwErrorCode = GetLastError ();
+ throw new SyncFailedException (_Jv_WinStrError (dwErrorCode));
+ }
+}
+#endif
+
+jint
+FileChannelImpl::open (jstring path, jint jflags) {
+
+ HANDLE handle = NULL;
+ DWORD access = 0;
+ DWORD create = OPEN_EXISTING;
+
+ JV_TEMP_STRING_WIN32(cpath, path)
+
+ JvAssert((jflags & READ) || (jflags & WRITE));
+
+ if ((jflags & READ) && (jflags & WRITE))
+ {
+ access = GENERIC_READ | GENERIC_WRITE;
+ if (jflags & EXCL)
+ create = CREATE_NEW; // this will raise error if file exists.
+ else
+ create = OPEN_ALWAYS; // equivalent to O_CREAT
+ }
+ else if (jflags & READ)
+ {
+ access = GENERIC_READ;
+ create = OPEN_EXISTING; // ignore EXCL
+ }
+ else
+ {
+ access = GENERIC_WRITE;
+ if (jflags & EXCL)
+ create = CREATE_NEW;
+ else if (jflags & APPEND)
+ create = OPEN_ALWAYS;
+ else
+ create = CREATE_ALWAYS;
+ }
+
+ handle = CreateFile(cpath, access, FILE_SHARE_READ | FILE_SHARE_WRITE,
+ NULL, create, 0, NULL);
+
+ if (handle == INVALID_HANDLE_VALUE)
+ {
+ DWORD dwErrorCode = GetLastError ();
+ throw new FileNotFoundException (_Jv_WinStrError (cpath, dwErrorCode));
+ }
+
+ // For APPEND mode, move the file pointer to the end of the file.
+ if (jflags & APPEND)
+ {
+ DWORD low = SetFilePointer (handle, 0, NULL, FILE_END);
+ if ((low == (DWORD) 0xffffffff) && (GetLastError () != NO_ERROR))
+ {
+ DWORD dwErrorCode = GetLastError ();
+ throw new FileNotFoundException (_Jv_WinStrError (cpath, dwErrorCode));
+ }
+ }
+
+ // Make this handle non-inheritable so that child
+ // processes don't inadvertently prevent us from
+ // closing this file.
+ _Jv_platform_close_on_exec (handle);
+
+ return (jint) handle;
+}
+
+void
+FileChannelImpl::write (jint b)
+{
+ DWORD bytesWritten;
+ jbyte buf = (jbyte)b;
+
+ if (WriteFile ((HANDLE)fd, &buf, 1, &bytesWritten, NULL))
+ {
+ if (::java::lang::Thread::interrupted())
+ {
+ InterruptedIOException *iioe = new InterruptedIOException (JvNewStringLatin1 ("write interrupted"));
+ iioe->bytesTransferred = bytesWritten;
+ throw iioe;
+ }
+ if (bytesWritten != 1)
+ _Jv_ThrowIOException ();
+ }
+ else
+ _Jv_ThrowIOException ();
+ // FIXME: loop until bytesWritten == 1
+}
+
+void
+FileChannelImpl::write(jbyteArray b, jint offset, jint len)
+{
+ if (! b)
+ throw new ::java::lang::NullPointerException;
+ if(offset < 0 || len < 0 || offset + len > JvGetArrayLength (b))
+ throw new ArrayIndexOutOfBoundsException;
+
+ jbyte *buf = elements (b) + offset;
+ DWORD bytesWritten;
+
+ if (WriteFile ((HANDLE)fd, buf, len, &bytesWritten, NULL))
+ {
+ if (::java::lang::Thread::interrupted())
+ {
+ InterruptedIOException *iioe = new InterruptedIOException (JvNewStringLatin1 ("write interrupted"));
+ iioe->bytesTransferred = bytesWritten;
+ throw iioe;
+ }
+ }
+ else
+ _Jv_ThrowIOException ();
+ // FIXME: loop until bytesWritten == len
+}
+
+void
+FileChannelImpl::implCloseChannel (void)
+{
+ HANDLE save = (HANDLE)fd;
+ fd = (jint)INVALID_HANDLE_VALUE;
+ if (! CloseHandle (save))
+ _Jv_ThrowIOException ();
+}
+
+void
+FileChannelImpl::implTruncate (jlong size)
+{
+ LONG liOrigFilePointer;
+ LONG liNewFilePointer;
+ LONG liEndFilePointer;
+
+ // Get the original file pointer.
+ if (SetFilePointer((HANDLE) fd, (LONG) 0, &liOrigFilePointer,
+ FILE_CURRENT) != (BOOL) 0
+ && (GetLastError() != NO_ERROR))
+ _Jv_ThrowIOException ();
+
+ // Get the length of the file.
+ if (SetFilePointer((HANDLE) fd, (LONG) 0, &liEndFilePointer,
+ FILE_END) != (BOOL) 0
+ && (GetLastError() != NO_ERROR))
+ _Jv_ThrowIOException ();
+
+ if ((jlong)liEndFilePointer == size)
+ {
+ // Restore the file pointer.
+ if (liOrigFilePointer != liEndFilePointer)
+ {
+ if (SetFilePointer((HANDLE) fd, liOrigFilePointer, &liNewFilePointer,
+ FILE_BEGIN) != (BOOL) 0
+ && (GetLastError() != NO_ERROR))
+ _Jv_ThrowIOException ();
+ }
+ return;
+ }
+
+ // Seek to the new end of file.
+ if (SetFilePointer((HANDLE) fd, (LONG) size, &liNewFilePointer,
+ FILE_BEGIN) != (BOOL) 0
+ && (GetLastError() != NO_ERROR))
+ _Jv_ThrowIOException ();
+
+ // Truncate the file at this point.
+ if (SetEndOfFile((HANDLE) fd) != (BOOL) 0 && (GetLastError() != NO_ERROR))
+ _Jv_ThrowIOException ();
+
+ if (liOrigFilePointer < liNewFilePointer)
+ {
+ // Restore the file pointer.
+ if (SetFilePointer((HANDLE) fd, liOrigFilePointer, &liNewFilePointer,
+ FILE_BEGIN) != (BOOL) 0
+ && (GetLastError() != NO_ERROR))
+ _Jv_ThrowIOException ();
+ }
+}
+
+void
+FileChannelImpl::seek (jlong newPos)
+{
+ LONG high = pos >> 32;
+ DWORD low = SetFilePointer ((HANDLE)fd, (DWORD)(0xffffffff & newPos), &high, FILE_BEGIN);
+ if ((low == 0xffffffff) && (GetLastError () != NO_ERROR))
+ _Jv_ThrowIOException ();
+}
+
+jlong
+FileChannelImpl::implPosition (void)
+{
+ LONG high = 0;
+ DWORD low = SetFilePointer ((HANDLE)fd, 0, &high, FILE_CURRENT);
+ if ((low == 0xffffffff) && (GetLastError() != NO_ERROR))
+ _Jv_ThrowIOException ();
+ return (((jlong)high) << 32L) | (jlong)low;
+}
+
+jlong
+FileChannelImpl::size (void)
+{
+ DWORD high;
+ DWORD low;
+
+ low = GetFileSize ((HANDLE)fd, &high);
+ // FIXME: Error checking
+ return (((jlong)high) << 32L) | (jlong)low;
+}
+
+jint
+FileChannelImpl::read (void)
+{
+ CHAR buf;
+ DWORD read;
+
+ if (! ReadFile ((HANDLE)fd, &buf, 1, &read, NULL))
+ {
+ if (GetLastError () == ERROR_BROKEN_PIPE)
+ return -1;
+ else
+ _Jv_ThrowIOException ();
+ }
+
+ if (! read)
+ return -1;
+ else
+ return (jint)(buf & 0xff);
+}
+
+jint
+FileChannelImpl::read (jbyteArray buffer, jint offset, jint count)
+{
+ if (! buffer)
+ throw new ::java::lang::NullPointerException;
+
+ jsize bsize = JvGetArrayLength (buffer);
+ if (offset < 0 || count < 0 || offset + count > bsize)
+ throw new ArrayIndexOutOfBoundsException;
+
+ // Must return 0 if an attempt is made to read 0 bytes.
+ if (count == 0)
+ return 0;
+
+ jbyte *bytes = elements (buffer) + offset;
+
+ DWORD read;
+ if (! ReadFile((HANDLE)fd, bytes, count, &read, NULL))
+ {
+ if (GetLastError () == ERROR_BROKEN_PIPE)
+ return -1;
+ else
+ _Jv_ThrowIOException ();
+ }
+
+ if (read == 0) return -1;
+
+ return (jint)read;
+}
+
+jint
+FileChannelImpl::available (void)
+{
+ // FIXME:
+ return size() - position();
+}
+
+jboolean
+FileChannelImpl::lock
+(jlong /*pos*/, jlong /*len*/, jboolean /*shared*/, jboolean /*wait*/)
+{
+ throw new IOException (JvNewStringLatin1
+ ("FileChannel.lock() not implemented"));
+}
+
+void
+FileChannelImpl::unlock (jlong /*pos*/, jlong /*len*/)
+{
+ throw new IOException (JvNewStringLatin1
+ ("FileChannel.unlock() not implemented"));
+}
Index: java/io/FileDescriptor.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/FileDescriptor.java,v
retrieving revision 1.17
diff -u -p -r1.17 FileDescriptor.java
--- java/io/FileDescriptor.java 23 Jan 2004 14:07:28 -0000 1.17
+++ java/io/FileDescriptor.java 20 Feb 2004 01:27:58 -0000
@@ -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
* <code>System.in</code>variable.
*/
- public static final FileDescriptor in = null;
+ public static final FileDescriptor in
+ = new FileDescriptor (FileChannelImpl.in);
/**
* A <code>FileDescriptor</code> representing the system standard output
* stream. This will usually be accessed through the
* <code>System.out</code>variable.
*/
- public static final FileDescriptor out = null;
+ public static final FileDescriptor out
+ = new FileDescriptor (FileChannelImpl.out);
/**
* A <code>FileDescriptor</code> representing the system standard error
* stream. This will usually be accessed through the
* <code>System.err</code>variable.
*/
- 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 <code>true</code> if this object represents a valid
* native file handle, <code>false</code> otherwise
*/
- public native boolean valid();
-
- /**
- * Opens the specified file in the specified mode. This can be done
- * in one of the specified modes:
- * <ul>
- * <li>r - Read Only
- * <li>rw - Read / Write
- * <li>ra - Read / Write - append to end of file
- * <li>rws - Read / Write - synchronous writes of data/metadata
- * <li>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();
}
}
Index: java/io/FileInputStream.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/FileInputStream.java,v
retrieving revision 1.19
diff -u -p -r1.19 FileInputStream.java
--- java/io/FileInputStream.java 26 Dec 2003 21:11:03 -0000 1.19
+++ java/io/FileInputStream.java 20 Feb 2004 01:27:58 -0000
@@ -1,5 +1,5 @@
/* FileInputStream.java -- An input stream that reads from disk files.
- Copyright (C) 1998, 2002, 2003 Free Software Foundation, Inc.
+ Copyright (C) 1998, 2002, 2003, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -39,7 +39,7 @@ exception statement from your version. *
package java.io;
import java.nio.channels.FileChannel;
-import java.nio.channels.FileChannelImpl;
+import gnu.java.nio.channels.FileChannelImpl;
/* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
* "The Java Language Specification", ISBN 0-201-63451-1
@@ -60,7 +60,7 @@ public class FileInputStream extends Inp
*/
private FileDescriptor fd;
- private FileChannel ch; /* cached associated file-channel */
+ private FileChannelImpl ch;
/**
* This method initializes a <code>FileInputStream</code> to read from the
@@ -107,7 +107,7 @@ public class FileInputStream extends Inp
if (file.isDirectory())
throw new FileNotFoundException(file.getPath() + " is a directory");
- fd = new FileDescriptor(file.getPath(), FileDescriptor.READ);
+ ch = new FileChannelImpl (file.getPath(), FileChannelImpl.READ);
}
/**
@@ -133,6 +133,12 @@ public class FileInputStream extends Inp
s.checkRead(fdObj);
fd = fdObj;
+ ch = (FileChannelImpl) fdObj.channel;
+ }
+
+ FileInputStream(FileChannelImpl ch)
+ {
+ this.ch = ch;
}
/**
@@ -156,7 +162,7 @@ public class FileInputStream extends Inp
*/
public int available() throws IOException
{
- return fd.available();
+ return ch.available();
}
/**
@@ -168,8 +174,7 @@ public class FileInputStream extends Inp
*/
public void close() throws IOException
{
- if (fd.valid())
- fd.close();
+ ch.close();
}
protected void finalize() throws IOException
@@ -189,9 +194,12 @@ public class FileInputStream extends Inp
*/
public final FileDescriptor getFD() throws IOException
{
- if (!fd.valid())
- throw new IOException();
- return fd;
+ synchronized (this)
+ {
+ if (fd == null)
+ fd = new FileDescriptor (ch);
+ return fd;
+ }
}
/**
@@ -207,7 +215,7 @@ public class FileInputStream extends Inp
*/
public int read() throws IOException
{
- return fd.read();
+ return ch.read();
}
/**
@@ -258,7 +266,7 @@ public class FileInputStream extends Inp
|| offset + len > buf.length)
throw new ArrayIndexOutOfBoundsException();
- return fd.read(buf, offset, len);
+ return ch.read(buf, offset, len);
}
/**
@@ -281,9 +289,9 @@ public class FileInputStream extends Inp
if (numBytes == 0)
return 0;
- long curPos = fd.getFilePointer ();
- long newPos = fd.seek (numBytes, FileDescriptor.CUR, true);
- return newPos - curPos;
+ long oldPos = ch.position ();
+ ch.position(oldPos + numBytes);
+ return ch.position() - oldPos;
}
/**
@@ -294,9 +302,6 @@ public class FileInputStream extends Inp
*/
public synchronized FileChannel getChannel ()
{
- if (ch == null)
- ch = new FileChannelImpl (fd, false, this);
-
return ch;
}
Index: java/io/FileOutputStream.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/FileOutputStream.java,v
retrieving revision 1.15
diff -u -p -r1.15 FileOutputStream.java
--- java/io/FileOutputStream.java 13 Jul 2003 16:53:05 -0000 1.15
+++ java/io/FileOutputStream.java 20 Feb 2004 01:27:58 -0000
@@ -1,5 +1,5 @@
/* FileOutputStream.java -- Writes to a file on disk.
- Copyright (C) 1998, 2001, 2003 Free Software Foundation, Inc.
+ Copyright (C) 1998, 2001, 2003, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -39,7 +39,7 @@ exception statement from your version. *
package java.io;
import java.nio.channels.FileChannel;
-import java.nio.channels.FileChannelImpl;
+import gnu.java.nio.channels.FileChannelImpl;
/* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
* "The Java Language Specification", ISBN 0-201-63451-1
@@ -57,7 +57,7 @@ public class FileOutputStream extends Ou
{
private FileDescriptor fd;
- private FileChannel ch; /* cached associated file-channel */
+ private FileChannelImpl ch;
/**
* This method initializes a <code>FileOutputStream</code> object to write
@@ -84,10 +84,10 @@ public class FileOutputStream extends Ou
SecurityManager s = System.getSecurityManager();
if (s != null)
s.checkWrite(path);
- fd = new FileDescriptor (path, (append
- ? FileDescriptor.WRITE
- | FileDescriptor.APPEND
- : FileDescriptor.WRITE));
+ ch = new FileChannelImpl (path, (append
+ ? FileChannelImpl.WRITE
+ | FileChannelImpl.APPEND
+ : FileChannelImpl.WRITE));
}
/**
@@ -188,6 +188,12 @@ public class FileOutputStream extends Ou
s.checkWrite(fdObj);
fd = fdObj;
+ ch = (FileChannelImpl) fdObj.channel;
+ }
+
+ FileOutputStream(FileChannelImpl ch)
+ {
+ this.ch = ch;
}
protected void finalize () throws IOException
@@ -206,9 +212,12 @@ public class FileOutputStream extends Ou
*/
public final FileDescriptor getFD () throws IOException
{
- if (! fd.valid())
- throw new IOException ();
- return fd;
+ synchronized (this)
+ {
+ if (fd == null)
+ fd = new FileDescriptor (ch);
+ return fd;
+ }
}
/**
@@ -220,7 +229,7 @@ public class FileOutputStream extends Ou
*/
public void write (int b) throws IOException
{
- fd.write (b);
+ ch.write (b);
}
/**
@@ -255,7 +264,7 @@ public class FileOutputStream extends Ou
|| offset + len > buf.length)
throw new ArrayIndexOutOfBoundsException ();
- fd.write (buf, offset, len);
+ ch.write (buf, offset, len);
}
/**
@@ -267,8 +276,7 @@ public class FileOutputStream extends Ou
*/
public void close () throws IOException
{
- if (fd.valid())
- fd.close();
+ ch.close();
}
/**
@@ -279,9 +287,6 @@ public class FileOutputStream extends Ou
*/
public synchronized FileChannel getChannel()
{
- if (ch == null)
- ch = new FileChannelImpl (fd, true, this);
-
return ch;
}
Index: java/io/RandomAccessFile.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/RandomAccessFile.java,v
retrieving revision 1.17
diff -u -p -r1.17 RandomAccessFile.java
--- java/io/RandomAccessFile.java 13 Jul 2003 16:53:05 -0000 1.17
+++ java/io/RandomAccessFile.java 20 Feb 2004 01:27:58 -0000
@@ -1,5 +1,5 @@
/* RandomAccessFile.java -- Class supporting random file I/O
- Copyright (C) 1998, 1999, 2001, 2002, 2003 Free Software Foundation, Inc.
+ Copyright (C) 1998, 1999, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -39,7 +39,7 @@ exception statement from your version. *
package java.io;
import java.nio.channels.FileChannel;
-import java.nio.channels.FileChannelImpl;
+import gnu.java.nio.channels.FileChannelImpl;
/* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
* "The Java Language Specification", ISBN 0-201-63451-1
@@ -61,12 +61,12 @@ public class RandomAccessFile implements
{
// The underlying file.
+ private FileChannelImpl ch;
private FileDescriptor fd;
// The corresponding input and output streams.
private DataOutputStream out;
private DataInputStream in;
- private FileChannel ch; /* cached associated file-channel */
/**
* This method initializes a new instance of <code>RandomAccessFile</code>
@@ -119,18 +119,18 @@ public class RandomAccessFile implements
{
int fdmode;
if (mode.equals("r"))
- fdmode = FileDescriptor.READ;
+ fdmode = FileChannelImpl.READ;
else if (mode.equals("rw"))
- fdmode = FileDescriptor.READ | FileDescriptor.WRITE;
+ fdmode = FileChannelImpl.READ | FileChannelImpl.WRITE;
else if (mode.equals("rws"))
{
- fdmode = (FileDescriptor.READ | FileDescriptor.WRITE
- | FileDescriptor.SYNC);
+ fdmode = (FileChannelImpl.READ | FileChannelImpl.WRITE
+ | FileChannelImpl.SYNC);
}
else if (mode.equals("rwd"))
{
- fdmode = (FileDescriptor.READ | FileDescriptor.WRITE
- | FileDescriptor.DSYNC);
+ fdmode = (FileChannelImpl.READ | FileChannelImpl.WRITE
+ | FileChannelImpl.DSYNC);
}
else
throw new IllegalArgumentException ("invalid mode: " + mode);
@@ -141,11 +141,12 @@ public class RandomAccessFile implements
{
s.checkRead(fileName);
- if ((fdmode & FileDescriptor.WRITE) != 0)
+ if ((fdmode & FileChannelImpl.WRITE) != 0)
s.checkWrite(fileName);
}
- fd = new FileDescriptor (fileName, fdmode);
+ ch = new FileChannelImpl (fileName, fdmode);
+ fd = new FileDescriptor(ch);
out = new DataOutputStream (new FileOutputStream (fd));
in = new DataInputStream (new FileInputStream (fd));
}
@@ -158,8 +159,7 @@ public class RandomAccessFile implements
*/
public void close () throws IOException
{
- if (fd.valid())
- fd.close();
+ ch.close();
}
/**
@@ -172,10 +172,12 @@ public class RandomAccessFile implements
*/
public final FileDescriptor getFD () throws IOException
{
- if (! fd.valid())
- throw new IOException ();
-
- return fd;
+ synchronized (this)
+ {
+ if (fd == null)
+ fd = new FileDescriptor (ch);
+ return fd;
+ }
}
/**
@@ -188,7 +190,7 @@ public class RandomAccessFile implements
*/
public long getFilePointer () throws IOException
{
- return fd.getFilePointer();
+ return ch.position();
}
/**
@@ -206,7 +208,7 @@ public class RandomAccessFile implements
*/
public void setLength (long newLen) throws IOException
{
- fd.setLength (newLen);
+ ch.truncate (newLen);
}
/**
@@ -218,7 +220,7 @@ public class RandomAccessFile implements
*/
public long length () throws IOException
{
- return fd.getLength ();
+ return ch.size();
}
/**
@@ -702,7 +704,7 @@ public class RandomAccessFile implements
*/
public void seek (long pos) throws IOException
{
- fd.seek (pos, FileDescriptor.SET, false);
+ ch.position(pos);
}
/**
@@ -726,10 +728,13 @@ public class RandomAccessFile implements
if (numBytes == 0)
return 0;
- long curPos = fd.getFilePointer ();
- long newPos = fd.seek (numBytes, FileDescriptor.CUR, true);
-
- return (int) (newPos - curPos);
+ long oldPos = ch.position();
+ long newPos = oldPos + numBytes;
+ long size = ch.size();
+ if (newPos > size)
+ newPos = size;
+ ch.position(newPos);
+ return (int) (ch.position() - oldPos);
}
/**
@@ -962,9 +967,6 @@ public class RandomAccessFile implements
*/
public final synchronized FileChannel getChannel ()
{
- if (ch == null)
- ch = new FileChannelImpl (fd, true, this);
-
return ch;
}
Index: java/io/natFileDescriptorEcos.cc
===================================================================
RCS file: java/io/natFileDescriptorEcos.cc
diff -N java/io/natFileDescriptorEcos.cc
--- java/io/natFileDescriptorEcos.cc 23 Jan 2004 14:07:28 -0000 1.11
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,159 +0,0 @@
-// natFileDescriptor.cc - Native part of FileDescriptor class.
-
-/* Copyright (C) 1998, 1999, 2001, 2002 Free Software Foundation
-
- This file is part of libgcj.
-
-This software is copyrighted work licensed under the terms of the
-Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
-details. */
-
-#include <config.h>
-
-#include <errno.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/param.h>
-
-#include <gcj/cni.h>
-#include <jvm.h>
-#include <java/io/FileDescriptor.h>
-#include <java/io/SyncFailedException.h>
-#include <java/io/IOException.h>
-#include <java/io/EOFException.h>
-#include <java/lang/ArrayIndexOutOfBoundsException.h>
-#include <java/lang/NullPointerException.h>
-#include <java/lang/String.h>
-#include <java/io/FileNotFoundException.h>
-
-extern "C" void diag_write_char (char c);
-
-static void
-diag_write (char *data, int len)
-{
- while (len > 0)
- {
- diag_write_char (*data++);
- len--;
- }
-}
-
-#define NO_FSYNC_MESSAGE "sync unsupported"
-
-void
-java::io::FileDescriptor::init(void)
-{
- in = new java::io::FileDescriptor(0);
- out = new java::io::FileDescriptor(1);
- err = new java::io::FileDescriptor(2);
-}
-
-jboolean
-java::io::FileDescriptor::valid (void)
-{
- return true;
-}
-
-void
-java::io::FileDescriptor::sync (void)
-{
- // Some files don't support fsync. We don't bother reporting these
- // as errors.
-#ifdef HAVE_FSYNC
-#else
- throw new SyncFailedException (JvNewStringLatin1 (NO_FSYNC_MESSAGE));
-#endif
-}
-
-jint
-java::io::FileDescriptor::open (jstring path, jint jflags)
-{
- return fd;
-}
-
-void
-java::io::FileDescriptor::write (jint b)
-{
- char d = (char) b;
- ::diag_write (&d, 1);
-}
-
-void
-java::io::FileDescriptor::write (jbyteArray b, jint offset, jint len)
-{
- if (! b)
- throw new java::lang::NullPointerException;
- if (offset < 0 || len < 0 || offset + len > JvGetArrayLength (b))
- throw new java::lang::ArrayIndexOutOfBoundsException;
- char *bytes = (char *)elements (b) + offset;
- ::diag_write (bytes, len);
-}
-
-void
-java::io::FileDescriptor::close (void)
-{
-}
-
-void
-java::io::FileDescriptor::setLength (long)
-{
-}
-
-jint
-java::io::FileDescriptor::seek (jlong pos, jint whence, jboolean)
-{
- JvAssert (whence == SET || whence == CUR);
- return 0;
-}
-
-jlong
-java::io::FileDescriptor::getLength (void)
-{
- return 0;
-}
-
-jlong
-java::io::FileDescriptor::getFilePointer (void)
-{
- return 0;
-}
-
-jint
-java::io::FileDescriptor::read (void)
-{
- return 0;
-}
-
-jint
-java::io::FileDescriptor::read (jbyteArray buffer, jint offset, jint count)
-{
- return 0;
-}
-
-jint
-java::io::FileDescriptor::available (void)
-{
- return 0;
-}
-
-void
-java::io::FileDescriptor::lock (jlong pos, jint len, jboolean shared)
-{
- throw new IOException (JvNewStringLatin1
- ("java.io.FileDescriptor.lock() not implemented"));
-}
-
-jboolean
-java::io::FileDescriptor::tryLock (jlong pos, jint len, jboolean shared)
-{
- throw new IOException (JvNewStringLatin1
- ("java.io.FileDescriptor.tryLock() not implemented"));
-}
-
-void
-java::io::FileDescriptor::unlock (jlong pos, jint len)
-{
- throw new IOException (JvNewStringLatin1
- ("java.io.FileDescriptor.unlock() not implemented"));
-}
Index: java/io/natFileDescriptorPosix.cc
===================================================================
RCS file: java/io/natFileDescriptorPosix.cc
diff -N java/io/natFileDescriptorPosix.cc
--- java/io/natFileDescriptorPosix.cc 23 Jan 2004 14:07:28 -0000 1.30
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,467 +0,0 @@
-// natFileDescriptor.cc - Native part of FileDescriptor class.
-
-/* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation
-
- This file is part of libgcj.
-
-This software is copyrighted work licensed under the terms of the
-Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
-details. */
-
-#include <config.h>
-
-#include "posix.h"
-
-#include <errno.h>
-#include <fcntl.h>
-#include <stdio.h>
-#include <string.h>
-#include <sys/stat.h>
-#include <sys/param.h>
-
-#ifdef HAVE_SYS_IOCTL_H
-#define BSD_COMP /* Get FIONREAD on Solaris2. */
-#include <sys/ioctl.h>
-#endif
-
-// Pick up FIONREAD on Solaris 2.5.
-#ifdef HAVE_SYS_FILIO_H
-#include <sys/filio.h>
-#endif
-
-#include <gcj/cni.h>
-#include <jvm.h>
-#include <java/io/FileDescriptor.h>
-#include <java/io/SyncFailedException.h>
-#include <java/io/IOException.h>
-#include <java/io/InterruptedIOException.h>
-#include <java/io/EOFException.h>
-#include <java/lang/ArrayIndexOutOfBoundsException.h>
-#include <java/lang/NullPointerException.h>
-#include <java/lang/System.h>
-#include <java/lang/String.h>
-#include <java/lang/Thread.h>
-#include <java/io/FileNotFoundException.h>
-
-#define NO_FSYNC_MESSAGE "sync unsupported"
-
-void
-java::io::FileDescriptor::init (void)
-{
- in = new java::io::FileDescriptor(0);
- out = new java::io::FileDescriptor(1);
- err = new java::io::FileDescriptor(2);
-}
-
-jboolean
-java::io::FileDescriptor::valid (void)
-{
- struct stat sb;
- return fd >= 0 && ::fstat (fd, &sb) == 0;
-}
-
-void
-java::io::FileDescriptor::sync (void)
-{
- // Some files don't support fsync. We don't bother reporting these
- // as errors.
-#ifdef HAVE_FSYNC
- if (::fsync (fd) && errno != EROFS && errno != EINVAL)
- throw new SyncFailedException (JvNewStringLatin1 (strerror (errno)));
-#else
- throw new SyncFailedException (JvNewStringLatin1 (NO_FSYNC_MESSAGE));
-#endif
-}
-
-jint
-java::io::FileDescriptor::open (jstring path, jint jflags)
-{
- char *buf = (char *) _Jv_AllocBytes (_Jv_GetStringUTFLength (path) + 1);
- jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
- buf[total] = '\0';
- int flags = 0;
-#ifdef O_BINARY
- flags |= O_BINARY;
-#endif
-
- JvAssert ((jflags & READ) || (jflags & WRITE));
- int mode = 0666;
- if ((jflags & READ) && (jflags & WRITE))
- flags |= O_RDWR | O_CREAT;
- else if ((jflags & READ))
- flags |= O_RDONLY;
- else
- {
- flags |= O_WRONLY | O_CREAT;
- if ((jflags & APPEND))
- flags |= O_APPEND;
- else
- flags |= O_TRUNC;
-
- if ((jflags & EXCL))
- {
- flags |= O_EXCL;
- // In this case we are making a temp file.
- mode = 0600;
- }
- }
-
- if ((jflags & SYNC))
- flags |= O_SYNC;
-
- if ((jflags & DSYNC))
- flags |= O_DSYNC;
-
- int fd = ::open (buf, flags, mode);
- if (fd == -1 && errno == EMFILE)
- {
- // Because finalize () calls close () we might be able to continue.
- java::lang::System::gc ();
- java::lang::System::runFinalization ();
- fd = ::open (buf, flags, mode);
- }
- if (fd == -1)
- {
- char msg[MAXPATHLEN + 200];
- // We choose the formatting here for JDK compatibility, believe
- // it or not.
- sprintf (msg, "%s (%s)", buf, strerror (errno));
- throw new FileNotFoundException (JvNewStringLatin1 (msg));
- }
-
- _Jv_platform_close_on_exec (fd);
-
- return fd;
-}
-
-void
-java::io::FileDescriptor::write (jint b)
-{
- jbyte d = (jbyte) b;
- int r = 0;
- while (r != 1)
- {
- r = ::write (fd, &d, 1);
- if (r == -1)
- {
- if (java::lang::Thread::interrupted())
- {
- InterruptedIOException *iioe
- = new InterruptedIOException (JvNewStringLatin1 (strerror (errno)));
- iioe->bytesTransferred = r == -1 ? 0 : r;
- throw iioe;
- }
- if (errno != EINTR)
- throw new IOException (JvNewStringLatin1 (strerror (errno)));
- }
- }
- position++;
-}
-
-void
-java::io::FileDescriptor::write (jbyteArray b, jint offset, jint len)
-{
- if (! b)
- throw new java::lang::NullPointerException;
- if (offset < 0 || len < 0 || offset + len > JvGetArrayLength (b))
- throw new java::lang::ArrayIndexOutOfBoundsException;
- jbyte *bytes = elements (b) + offset;
-
- int written = 0;
- while (len > 0)
- {
- int r = ::write (fd, bytes, len);
- if (r == -1)
- {
- if (java::lang::Thread::interrupted())
- {
- InterruptedIOException *iioe
- = new InterruptedIOException (JvNewStringLatin1 (strerror (errno)));
- iioe->bytesTransferred = written;
- throw iioe;
- }
- if (errno != EINTR)
- throw new IOException (JvNewStringLatin1 (strerror (errno)));
- }
-
- written += r;
- len -= r;
- bytes += r;
- position += r;
- }
-}
-
-void
-java::io::FileDescriptor::close (void)
-{
- jint save = fd;
- fd = -1;
- if (::close (save))
- throw new IOException (JvNewStringLatin1 (strerror (errno)));
-}
-
-void
-java::io::FileDescriptor::setLength (jlong pos)
-{
- struct stat sb;
-
-#ifdef HAVE_FTRUNCATE
- if (::fstat (fd, &sb))
- throw new IOException (JvNewStringLatin1 (strerror (errno)));
-
- if ((jlong) sb.st_size == pos)
- return;
-
- // If the file is too short, we extend it. We can't rely on
- // ftruncate() extending the file. So we lseek() to 1 byte less
- // than we want, and then we write a single byte at the end.
- if ((jlong) sb.st_size < pos)
- {
- if (::lseek (fd, (off_t) (pos - 1), SEEK_SET) == -1)
- throw new IOException (JvNewStringLatin1 (strerror (errno)));
- char out = '\0';
- int r = ::write (fd, &out, 1);
- if (r <= 0 || ::lseek (fd, position, SEEK_SET) == -1)
- throw new IOException (JvNewStringLatin1 (strerror (errno)));
- }
- else
- {
- if (::ftruncate (fd, (off_t) pos))
- throw new IOException (JvNewStringLatin1 (strerror (errno)));
- position = pos;
- }
-#else /* HAVE_FTRUNCATE */
- throw new IOException (JvNewStringLatin1 ("FileDescriptor.setLength not implemented"));
-#endif /* HAVE_FTRUNCATE */
-}
-
-jint
-java::io::FileDescriptor::seek (jlong pos, jint whence, jboolean eof_trunc)
-{
- JvAssert (whence == SET || whence == CUR);
-
- if (eof_trunc)
- {
- jlong len = getLength ();
- if (whence == SET)
- {
- if (pos > len)
- pos = len;
- }
- else
- {
- jlong here = getFilePointer ();
- if (here + pos > len)
- {
- pos = len;
- whence = SET;
- }
- }
- }
-
- off_t r = ::lseek (fd, (off_t) pos, whence == SET ? SEEK_SET : SEEK_CUR);
- if (r == -1)
- throw new IOException (JvNewStringLatin1 (strerror (errno)));
- position = r;
- return r;
-}
-
-jlong
-java::io::FileDescriptor::getLength (void)
-{
- struct stat sb;
- if (::fstat (fd, &sb))
- throw new IOException (JvNewStringLatin1 (strerror (errno)));
- return sb.st_size;
-}
-
-jlong
-java::io::FileDescriptor::getFilePointer (void)
-{
- return position;
-}
-
-jint
-java::io::FileDescriptor::read (void)
-{
- jbyte b;
- int r;
- do
- {
- r = ::read (fd, &b, 1);
- if (r == 0)
- return -1;
- if (r == -1)
- {
- if (java::lang::Thread::interrupted())
- {
- InterruptedIOException *iioe
- = new InterruptedIOException (JvNewStringLatin1 (strerror (errno)));
- iioe->bytesTransferred = r == -1 ? 0 : r;
- throw iioe;
- }
- if (errno != EINTR)
- throw new IOException (JvNewStringLatin1 (strerror (errno)));
- }
- }
- while (r != 1);
- position++;
- return b & 0xFF;
-}
-
-jint
-java::io::FileDescriptor::read (jbyteArray buffer, jint offset, jint count)
-{
- if (! buffer)
- throw new java::lang::NullPointerException;
- jsize bsize = JvGetArrayLength (buffer);
- if (offset < 0 || count < 0 || offset + count > bsize)
- throw new java::lang::ArrayIndexOutOfBoundsException;
-
- // Must return 0 if an attempt is made to read 0 bytes.
- if (count == 0)
- return 0;
-
- jbyte *bytes = elements (buffer) + offset;
- int r;
- do
- {
- r = ::read (fd, bytes, count);
- if (r == 0)
- return -1;
- if (r == -1)
- {
- if (java::lang::Thread::interrupted())
- {
- InterruptedIOException *iioe
- = new InterruptedIOException (JvNewStringLatin1 (strerror (errno)));
- iioe->bytesTransferred = r == -1 ? 0 : r;
- throw iioe;
- }
- if (errno != EINTR)
- throw new IOException (JvNewStringLatin1 (strerror (errno)));
- }
- }
- while (r <= 0);
- position += r;
- return r;
-}
-
-jint
-java::io::FileDescriptor::available (void)
-{
-#if defined (FIONREAD) || defined (HAVE_SELECT) || defined (HAVE_FSTAT)
- long num = 0;
- int r = 0;
- bool num_set = false;
-
-#if defined (FIONREAD)
- r = ::ioctl (fd, FIONREAD, &num);
- if (r == -1 && errno == ENOTTY)
- {
- // If the ioctl doesn't work, we don't care.
- r = 0;
- num = 0;
- }
- else
- num_set = true;
-#elif defined (HAVE_SELECT)
- if (fd < 0)
- {
- errno = EBADF;
- r = -1;
- }
-#endif
-
- if (r == -1)
- {
- posix_error:
- throw new IOException (JvNewStringLatin1 (strerror (errno)));
- }
-
- // If we didn't get anything, and we have fstat, then see if see if
- // we're reading a regular file. On many systems, FIONREAD does not
- // work on regular files; select() likewise returns a useless
- // result. This is run incorrectly when FIONREAD does work on
- // regular files and we are at the end of the file. However, this
- // case probably isn't very important.
-#if defined (HAVE_FSTAT)
- if (! num_set)
- {
- struct stat sb;
- off_t where = 0;
- if (fstat (fd, &sb) != -1
- && S_ISREG (sb.st_mode)
- && (where = lseek (fd, 0, SEEK_CUR)) != (off_t) -1)
- {
- num = (long) (sb.st_size - where);
- num_set = true;
- }
- }
-#endif /* HAVE_FSTAT */
-
-#if defined (HAVE_SELECT)
- if (! num_set)
- {
- fd_set rd;
- FD_ZERO (&rd);
- FD_SET (fd, &rd);
- struct timeval tv;
- tv.tv_sec = 0;
- tv.tv_usec = 0;
- r = _Jv_select (fd + 1, &rd, NULL, NULL, &tv);
- if (r == -1)
- goto posix_error;
- num = r == 0 ? 0 : 1;
- }
-#endif /* HAVE_SELECT */
-
- return (jint) num;
-#else
- return 0;
-#endif
-}
-
-void
-java::io::FileDescriptor::lock (jlong pos, jint len, jboolean shared)
-{
- struct flock lockdata;
-
- lockdata.l_type = shared ? F_WRLCK : F_RDLCK;
- lockdata.l_whence = SEEK_SET;
- lockdata.l_start = pos;
- lockdata.l_len = len;
-
- if (::fcntl (fd, F_SETLK, &lockdata) == -1)
- throw new IOException (JvNewStringLatin1 (strerror (errno)));
-}
-
-jboolean
-java::io::FileDescriptor::tryLock (jlong pos, jint len, jboolean shared)
-{
- struct flock lockdata;
-
- lockdata.l_type = shared ? F_WRLCK : F_RDLCK;
- lockdata.l_whence = SEEK_SET;
- lockdata.l_start = pos;
- lockdata.l_len = len;
-
- if (::fcntl (fd, F_GETLK, &lockdata) == -1)
- throw new IOException (JvNewStringLatin1 (strerror (errno)));
-
- return lockdata.l_type == F_UNLCK;
-}
-
-void
-java::io::FileDescriptor::unlock (jlong pos, jint len)
-{
- struct flock lockdata;
-
- lockdata.l_type = F_UNLCK;
- lockdata.l_whence = SEEK_SET;
- lockdata.l_start = pos;
- lockdata.l_len = len;
-
- if (::fcntl (fd, F_SETLK, &lockdata) == -1)
- throw new IOException (JvNewStringLatin1 (strerror (errno)));
-}
Index: java/io/natFileDescriptorWin32.cc
===================================================================
RCS file: java/io/natFileDescriptorWin32.cc
diff -N java/io/natFileDescriptorWin32.cc
--- java/io/natFileDescriptorWin32.cc 23 Jan 2004 14:07:28 -0000 1.18
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,373 +0,0 @@
-// natFileDescriptorWin32.cc - Native part of FileDescriptor class.
-
-/* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software
- Foundation, Inc.
-
- This file is part of libgcj.
-
-This software is copyrighted work licensed under the terms of the
-Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
-details. */
-
-// FIXME: In order to support interrupting of IO operations, we
-// need to change to use the windows asynchronous IO functions
-
-#include <config.h>
-#include <platform.h>
-
-#include <stdio.h>
-#include <string.h>
-
-#undef STRICT
-
-#include <java/io/FileDescriptor.h>
-#include <java/io/SyncFailedException.h>
-#include <java/io/IOException.h>
-#include <java/io/InterruptedIOException.h>
-#include <java/io/EOFException.h>
-#include <java/lang/ArrayIndexOutOfBoundsException.h>
-#include <java/lang/NullPointerException.h>
-#include <java/lang/String.h>
-#include <java/lang/Thread.h>
-#include <java/io/FileNotFoundException.h>
-
-static bool testCanUseGetHandleInfo()
-{
- /* Test to see whether GetHandleInformation can be used
- for console input or screen buffers. This is better
- a kludgy OS version check. */
- DWORD dwFlags;
- return GetHandleInformation (GetStdHandle (STD_INPUT_HANDLE),
- &dwFlags) != 0;
-}
-
-// FIXME: casting a FILE (pointer) to a jint will not work on Win64 --
-// we should be using gnu.gcj.RawData's.
-
-void
-java::io::FileDescriptor::init(void)
-{
- in = new java::io::FileDescriptor((jint)(GetStdHandle (STD_INPUT_HANDLE)));
- out = new java::io::FileDescriptor((jint)(GetStdHandle (STD_OUTPUT_HANDLE)));
- err = new java::io::FileDescriptor((jint)(GetStdHandle (STD_ERROR_HANDLE)));
-}
-
-jboolean
-java::io::FileDescriptor::valid (void) {
- static bool bCanUseGetHandleInfo = testCanUseGetHandleInfo();
- if (bCanUseGetHandleInfo)
- {
- /* As with UNIX, a "file" descriptor can be one of
- a gazillion possible underlying things like a pipe
- or socket, so we can't get too fancy here. */
- DWORD dwFlags;
- HANDLE h = (HANDLE) fd;
- return GetHandleInformation (h, &dwFlags) != 0;
- }
- else
- {
- /* Can't use GetHandleInformation() for console handles on < WinNT 5. */
- return true;
- }
-}
-
-void
-java::io::FileDescriptor::sync (void) {
- if (! FlushFileBuffers ((HANDLE)fd))
- {
- DWORD dwErrorCode = GetLastError ();
- throw new SyncFailedException (_Jv_WinStrError (dwErrorCode));
- }
-}
-
-jint
-java::io::FileDescriptor::open (jstring path, jint jflags) {
-
- HANDLE handle = NULL;
- DWORD access = 0;
- DWORD create = OPEN_EXISTING;
-
- JV_TEMP_STRING_WIN32(cpath, path)
-
- JvAssert((jflags & READ) || (jflags & WRITE));
-
- if ((jflags & READ) && (jflags & WRITE))
- {
- access = GENERIC_READ | GENERIC_WRITE;
- if (jflags & EXCL)
- create = CREATE_NEW; // this will raise error if file exists.
- else
- create = OPEN_ALWAYS; // equivalent to O_CREAT
- }
- else if (jflags & READ)
- {
- access = GENERIC_READ;
- create = OPEN_EXISTING; // ignore EXCL
- }
- else
- {
- access = GENERIC_WRITE;
- if (jflags & EXCL)
- create = CREATE_NEW;
- else if (jflags & APPEND)
- create = OPEN_ALWAYS;
- else
- create = CREATE_ALWAYS;
- }
-
- handle = CreateFile(cpath, access, FILE_SHARE_READ | FILE_SHARE_WRITE,
- NULL, create, 0, NULL);
-
- if (handle == INVALID_HANDLE_VALUE)
- {
- DWORD dwErrorCode = GetLastError ();
- throw new FileNotFoundException (_Jv_WinStrError (cpath, dwErrorCode));
- }
-
- // For APPEND mode, move the file pointer to the end of the file.
- if (jflags & APPEND)
- {
- DWORD low = SetFilePointer (handle, 0, NULL, FILE_END);
- if ((low == 0xffffffff) && (GetLastError () != NO_ERROR))
- {
- DWORD dwErrorCode = GetLastError ();
- throw new FileNotFoundException (_Jv_WinStrError (cpath, dwErrorCode));
- }
- }
-
- // Make this handle non-inheritable so that child
- // processes don't inadvertently prevent us from
- // closing this file.
- _Jv_platform_close_on_exec (handle);
-
- return (jint) handle;
-}
-
-void
-java::io::FileDescriptor::write (jint b)
-{
- DWORD bytesWritten;
- jbyte buf = (jbyte)b;
-
- if (WriteFile ((HANDLE)fd, &buf, 1, &bytesWritten, NULL))
- {
- if (java::lang::Thread::interrupted())
- {
- InterruptedIOException *iioe = new InterruptedIOException (JvNewStringLatin1 ("write interrupted"));
- iioe->bytesTransferred = bytesWritten;
- throw iioe;
- }
- if (bytesWritten != 1)
- _Jv_ThrowIOException ();
- }
- else
- _Jv_ThrowIOException ();
- // FIXME: loop until bytesWritten == 1
-}
-
-void
-java::io::FileDescriptor::write(jbyteArray b, jint offset, jint len)
-{
- if (! b)
- throw new java::lang::NullPointerException;
- if(offset < 0 || len < 0 || offset + len > JvGetArrayLength (b))
- throw new java::lang::ArrayIndexOutOfBoundsException;
-
- jbyte *buf = elements (b) + offset;
- DWORD bytesWritten;
-
- if (WriteFile ((HANDLE)fd, buf, len, &bytesWritten, NULL))
- {
- if (java::lang::Thread::interrupted())
- {
- InterruptedIOException *iioe = new InterruptedIOException (JvNewStringLatin1 ("write interrupted"));
- iioe->bytesTransferred = bytesWritten;
- throw iioe;
- }
- }
- else
- _Jv_ThrowIOException ();
- // FIXME: loop until bytesWritten == len
-}
-
-void
-java::io::FileDescriptor::close (void)
-{
- HANDLE save = (HANDLE)fd;
- fd = (jint)INVALID_HANDLE_VALUE;
- if (! CloseHandle (save))
- _Jv_ThrowIOException ();
-}
-
-void
-java::io::FileDescriptor::setLength(jlong pos)
-{
- LONG liOrigFilePointer;
- LONG liNewFilePointer;
- LONG liEndFilePointer;
-
- // Get the original file pointer.
- if (SetFilePointer((HANDLE) fd, (LONG) 0, &liOrigFilePointer,
- FILE_CURRENT) != (BOOL) 0
- && (GetLastError() != NO_ERROR))
- _Jv_ThrowIOException ();
-
- // Get the length of the file.
- if (SetFilePointer((HANDLE) fd, (LONG) 0, &liEndFilePointer,
- FILE_END) != (BOOL) 0
- && (GetLastError() != NO_ERROR))
- _Jv_ThrowIOException ();
-
- if ((jlong)liEndFilePointer == pos)
- {
- // Restore the file pointer.
- if (liOrigFilePointer != liEndFilePointer)
- {
- if (SetFilePointer((HANDLE) fd, liOrigFilePointer, &liNewFilePointer,
- FILE_BEGIN) != (BOOL) 0
- && (GetLastError() != NO_ERROR))
- _Jv_ThrowIOException ();
- }
- return;
- }
-
- // Seek to the new end of file.
- if (SetFilePointer((HANDLE) fd, (LONG) pos, &liNewFilePointer,
- FILE_BEGIN) != (BOOL) 0
- && (GetLastError() != NO_ERROR))
- _Jv_ThrowIOException ();
-
- // Truncate the file at this point.
- if (SetEndOfFile((HANDLE) fd) != (BOOL) 0 && (GetLastError() != NO_ERROR))
- _Jv_ThrowIOException ();
-
- if (liOrigFilePointer < liNewFilePointer)
- {
- // Restore the file pointer.
- if (SetFilePointer((HANDLE) fd, liOrigFilePointer, &liNewFilePointer,
- FILE_BEGIN) != (BOOL) 0
- && (GetLastError() != NO_ERROR))
- _Jv_ThrowIOException ();
- }
-}
-
-jint
-java::io::FileDescriptor::seek (jlong pos, jint whence, jboolean eof_trunc)
-{
- JvAssert (whence == SET || whence == CUR);
-
- jlong len = getLength();
- jlong here = getFilePointer();
-
- if (eof_trunc
- && ((whence == SET && pos > len) || (whence == CUR && here + pos > len)))
- {
- whence = SET;
- pos = len;
- }
-
- LONG high = pos >> 32;
- DWORD low = SetFilePointer ((HANDLE)fd, (DWORD)(0xffffffff & pos), &high, whence == SET ? FILE_BEGIN : FILE_CURRENT);
- if ((low == 0xffffffff) && (GetLastError () != NO_ERROR))
- _Jv_ThrowIOException ();
- return low;
-}
-
-jlong
-java::io::FileDescriptor::getFilePointer(void)
-{
- LONG high = 0;
- DWORD low = SetFilePointer ((HANDLE)fd, 0, &high, FILE_CURRENT);
- if ((low == 0xffffffff) && (GetLastError() != NO_ERROR))
- _Jv_ThrowIOException ();
- return (((jlong)high) << 32L) | (jlong)low;
-}
-
-jlong
-java::io::FileDescriptor::getLength(void)
-{
- DWORD high;
- DWORD low;
-
- low = GetFileSize ((HANDLE)fd, &high);
- // FIXME: Error checking
- return (((jlong)high) << 32L) | (jlong)low;
-}
-
-jint
-java::io::FileDescriptor::read(void)
-{
- CHAR buf;
- DWORD read;
-
- if (! ReadFile ((HANDLE)fd, &buf, 1, &read, NULL))
- {
- if (GetLastError () == ERROR_BROKEN_PIPE)
- return -1;
- else
- _Jv_ThrowIOException ();
- }
-
- if (! read)
- return -1;
- else
- return (jint)(buf & 0xff);
-}
-
-jint
-java::io::FileDescriptor::read(jbyteArray buffer, jint offset, jint count)
-{
- if (! buffer)
- throw new java::lang::NullPointerException;
-
- jsize bsize = JvGetArrayLength (buffer);
- if (offset < 0 || count < 0 || offset + count > bsize)
- throw new java::lang::ArrayIndexOutOfBoundsException;
-
- // Must return 0 if an attempt is made to read 0 bytes.
- if (count == 0)
- return 0;
-
- jbyte *bytes = elements (buffer) + offset;
-
- DWORD read;
- if (! ReadFile((HANDLE)fd, bytes, count, &read, NULL))
- {
- if (GetLastError () == ERROR_BROKEN_PIPE)
- return -1;
- else
- _Jv_ThrowIOException ();
- }
-
- if (read == 0) return -1;
-
- return (jint)read;
-}
-
-jint
-java::io::FileDescriptor::available(void)
-{
- // FIXME:
- return getLength() - getFilePointer();
-}
-
-void
-java::io::FileDescriptor::lock (jlong pos, jint len, jboolean shared)
-{
- throw new IOException (JvNewStringLatin1
- ("java.io.FileDescriptor.lock() not implemented"));
-}
-
-jboolean
-java::io::FileDescriptor::tryLock (jlong pos, jint len, jboolean shared)
-{
- throw new IOException (JvNewStringLatin1
- ("java.io.FileDescriptor.tryLock() not implemented"));
-}
-
-void
-java::io::FileDescriptor::unlock (jlong pos, jint len)
-{
- throw new IOException (JvNewStringLatin1
- ("java.io.FileDescriptor.unlock() not implemented"));
-}
Index: java/lang/natPosixProcess.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/lang/natPosixProcess.cc,v
retrieving revision 1.16
diff -u -p -r1.16 natPosixProcess.cc
--- java/lang/natPosixProcess.cc 4 Feb 2004 00:07:19 -0000 1.16
+++ java/lang/natPosixProcess.cc 20 Feb 2004 01:27:58 -0000
@@ -32,11 +32,14 @@ details. */
#include <java/lang/Thread.h>
#include <java/io/File.h>
#include <java/io/FileDescriptor.h>
+#include <gnu/java/nio/channels/FileChannelImpl.h>
#include <java/io/FileInputStream.h>
#include <java/io/FileOutputStream.h>
#include <java/io/IOException.h>
#include <java/lang/OutOfMemoryError.h>
+using gnu::java::nio::channels::FileChannelImpl;
+
extern char **environ;
void
@@ -187,9 +190,9 @@ java::lang::ConcreteProcess::startProces
// We create the streams before forking. Otherwise if we had an
// error while creating the streams we would have run the child
// with no way to communicate with it.
- errorStream = new FileInputStream (new FileDescriptor (errp[0]));
- inputStream = new FileInputStream (new FileDescriptor (inp[0]));
- outputStream = new FileOutputStream (new FileDescriptor (outp[1]));
+ errorStream = new FileInputStream (new FileChannelImpl(errp[0], FileChannelImpl::READ));
+ inputStream = new FileInputStream (new FileChannelImpl(inp[0], FileChannelImpl::READ));
+ outputStream = new FileOutputStream (new FileChannelImpl(outp[0], FileChannelImpl::WRITE));
// We don't use vfork() because that would cause the local
// environment to be set by the child.
Index: java/lang/natWin32Process.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/lang/natWin32Process.cc,v
retrieving revision 1.9
diff -u -p -r1.9 natWin32Process.cc
--- java/lang/natWin32Process.cc 2 Dec 2003 22:26:49 -0000 1.9
+++ java/lang/natWin32Process.cc 20 Feb 2004 01:27:58 -0000
@@ -25,6 +25,9 @@ details. */
#include <java/io/FileOutputStream.h>
#include <java/io/IOException.h>
#include <java/lang/OutOfMemoryError.h>
+#include <gnu/java/nio/channels/FileChannelImpl.h>
+
+using gnu::java::nio::channels::FileChannelImpl;
void
java::lang::ConcreteProcess::cleanup (void)
@@ -282,12 +285,15 @@ java::lang::ConcreteProcess::startProces
ChildProcessPipe aChildStdOut(ChildProcessPipe::OUTPUT);
ChildProcessPipe aChildStdErr(ChildProcessPipe::OUTPUT);
- outputStream = new FileOutputStream (new FileDescriptor (
- (jint) aChildStdIn.getParentHandle ()));
- inputStream = new FileInputStream (new FileDescriptor (
- (jint) aChildStdOut.getParentHandle ()));
- errorStream = new FileInputStream (new FileDescriptor (
- (jint) aChildStdErr.getParentHandle ()));
+ outputStream = new FileOutputStream (new FileChannelImpl (
+ (jint) aChildStdIn.getParentHandle (),
+ FileChannelImpl::WRITE));
+ inputStream = new FileInputStream (new FileChannelImpl (
+ (jint) aChildStdOut.getParentHandle (),
+ FileChannelImpl::READ));
+ errorStream = new FileInputStream (new FileChannelImpl (
+ (jint) aChildStdErr.getParentHandle (),
+ FileChannelImpl::READ));
// Now create the child process.
PROCESS_INFORMATION pi;
Index: java/nio/MappedByteBuffer.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/nio/MappedByteBuffer.java,v
retrieving revision 1.4
diff -u -p -r1.4 MappedByteBuffer.java
--- java/nio/MappedByteBuffer.java 13 Oct 2003 04:45:03 -0000 1.4
+++ java/nio/MappedByteBuffer.java 20 Feb 2004 01:27:59 -0000
@@ -44,28 +44,46 @@ package java.nio;
*/
public abstract class MappedByteBuffer extends ByteBuffer
{
- private boolean loaded = false;
-
MappedByteBuffer (int capacity, int limit, int position, int mark)
{
super (capacity, limit, position, mark);
}
+ void forceImpl()
+ {
+ }
+
public final MappedByteBuffer force ()
{
- // FIXME: Flush to disk here.
+ forceImpl();
return this;
}
+ boolean isLoadedImpl()
+ {
+ load();
+ return true;
+ }
+
public final boolean isLoaded ()
{
- return loaded;
+ return isLoadedImpl();
}
+ void loadImpl()
+ {
+ }
+
public final MappedByteBuffer load ()
{
- // FIXME: Try to load all pages into memory.
- loaded = true;
+ loadImpl();
return this;
}
+
+ void unmapImpl ()
+ {
+ forceImpl();
+ }
+
+ public void finalize () { unmapImpl(); }
}
Index: java/nio/MappedByteBufferImpl.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/nio/MappedByteBufferImpl.java,v
retrieving revision 1.6
diff -u -p -r1.6 MappedByteBufferImpl.java
--- java/nio/MappedByteBufferImpl.java 16 Feb 2004 20:00:33 -0000 1.6
+++ java/nio/MappedByteBufferImpl.java 20 Feb 2004 01:27:59 -0000
@@ -39,73 +39,80 @@ exception statement from your version. *
package java.nio;
import java.io.IOException;
-import java.nio.channels.FileChannelImpl;
+import gnu.java.nio.channels.FileChannelImpl;
import gnu.gcj.RawData;
-public class MappedByteBufferImpl extends MappedByteBuffer
+class MappedByteBufferImpl extends MappedByteBuffer
{
boolean readOnly;
- RawData map_address;
- public FileChannelImpl ch;
-
- public MappedByteBufferImpl (FileChannelImpl ch) throws IOException
- {
- super ((int) ch.size (), (int) ch.size (), 0, -1);
-
- this.ch = ch;
- map_address = ch.map_address;
- long si = ch.size () / 1;
- limit ((int) si);
- }
+ RawData address;
- public MappedByteBufferImpl (FileChannelImpl ch, int offset, int capacity, int limit, int position, int mark, boolean readOnly)
+ /** Posix uses this for the pointer returned by mmap;
+ * Win32 uses it for the pointer returned by MapViewOfFile. */
+ public RawData implPtr;
+ /** Posix uses this for the actual length passed to mmap;
+ * Win32 uses it for the pointer returned by CreateFileMapping. */
+ public long implLen;
+
+ public MappedByteBufferImpl (RawData address, int size, boolean readOnly)
+ throws IOException
{
- super (capacity, limit, position, mark);
-
- this.ch = ch;
- this.array_offset = offset;
+ super(size, size, 0, -1);
+ this.address = address;
this.readOnly = readOnly;
}
-
+
public boolean isReadOnly ()
{
return readOnly;
}
- public static byte getImpl (FileChannelImpl ch, int index,
- int limit, RawData map_address)
- {
- throw new Error ("Not implemented");
- }
-
- public static void putImpl (FileChannelImpl ch, int index,
- int limit, byte value, RawData map_address)
- {
- throw new Error ("Not implemented");
- }
-
public byte get ()
{
- byte result = get (position());
- position (position() + 1);
+ int pos = position();
+ if (pos >= limit())
+ throw new BufferUnderflowException();
+ byte result = DirectByteBufferImpl.getImpl(address, pos);
+ position (pos + 1);
return result;
}
public ByteBuffer put (byte value)
{
- put (position(), value);
- position (position() + 1);
+ int pos = position();
+ if (pos >= limit())
+ throw new BufferUnderflowException();
+ DirectByteBufferImpl.putImpl(address, pos, value);
+ position(pos + 1);
return this;
}
public byte get (int index)
{
- return getImpl (ch, index, limit (), map_address);
+ if (index >= limit())
+ throw new BufferUnderflowException();
+ return DirectByteBufferImpl.getImpl(address, index);
+ }
+
+ public ByteBuffer get (byte[] dst, int offset, int length)
+ {
+ if (offset < 0 || length < 0 || offset + length > dst.length)
+ throw new IndexOutOfBoundsException ();
+ if (length > remaining())
+ throw new BufferUnderflowException();
+
+ int index = position();
+ DirectByteBufferImpl.getImpl(address, index, dst, offset, length);
+ position(index+length);
+
+ return this;
}
public ByteBuffer put (int index, byte value)
{
- putImpl (ch, index, limit (), value, map_address);
+ if (index >= limit())
+ throw new BufferUnderflowException();
+ DirectByteBufferImpl.putImpl(address, index, value);
return this;
}
@@ -129,17 +136,39 @@ public class MappedByteBufferImpl extend
public ByteBuffer slice ()
{
- throw new Error ("Not implemented");
+ int rem = remaining();
+ return new DirectByteBufferImpl (this,
+ DirectByteBufferImpl
+ .adjustAddress(address, position()),
+ rem, rem, 0, isReadOnly ());
+ }
+
+ private ByteBuffer duplicate (boolean readOnly)
+ {
+ int pos = position();
+ reset();
+ int mark = position();
+ position(pos);
+ DirectByteBufferImpl result
+ = new DirectByteBufferImpl (this, address, capacity (), limit (),
+ pos, readOnly);
+ if (mark != pos)
+ {
+ result.position(mark);
+ result.mark();
+ result.position(pos);
+ }
+ return result;
}
public ByteBuffer duplicate ()
{
- throw new Error ("Not implemented");
+ return duplicate(isReadOnly());
}
public ByteBuffer asReadOnlyBuffer ()
{
- throw new Error ("Not implemented");
+ return duplicate(true);
}
public CharBuffer asCharBuffer ()
@@ -303,4 +332,11 @@ public class MappedByteBufferImpl extend
ByteBufferHelper.putDouble (this, index, value, order());
return this;
}
+
+ native void unmapImpl ();
+ native boolean isLoadedImpl ();
+ // FIXME: Try to load all pages into memory.
+ native void loadImpl();
+
+ native void forceImpl();
}
Index: java/nio/channels/FileChannelImpl.java
===================================================================
RCS file: java/nio/channels/FileChannelImpl.java
diff -N java/nio/channels/FileChannelImpl.java
--- java/nio/channels/FileChannelImpl.java 23 Jan 2004 14:37:09 -0000 1.5
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,441 +0,0 @@
-/* FileChannelImpl.java --
- Copyright (C) 2002 Free Software Foundation, Inc.
-
-This file is part of GNU Classpath.
-
-GNU Classpath is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2, or (at your option)
-any later version.
-
-GNU Classpath is distributed in the hope that it will be useful, but
-WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with GNU Classpath; see the file COPYING. If not, write to the
-Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
-02111-1307 USA.
-
-Linking this library statically or dynamically with other modules is
-making a combined work based on this library. Thus, the terms and
-conditions of the GNU General Public License cover the whole
-combination.
-
-As a special exception, the copyright holders of this library give you
-permission to link this library with independent modules to produce an
-executable, regardless of the license terms of these independent
-modules, and to copy and distribute the resulting executable under
-terms of your choice, provided that you also meet, for each linked
-independent module, the terms and conditions of the license of that
-module. An independent module is a module which is not derived from
-or based on this library. If you modify this library, you may extend
-this exception to your version of the library, but you are not
-obligated to do so. If you do not wish to do so, delete this
-exception statement from your version. */
-
-
-package java.nio.channels;
-
-import gnu.classpath.Configuration;
-import gnu.gcj.RawData;
-import gnu.java.nio.FileLockImpl;
-import java.io.EOFException;
-import java.io.FileDescriptor;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.RandomAccessFile;
-import java.nio.ByteBuffer;
-import java.nio.MappedByteBuffer;
-import java.nio.MappedByteBufferImpl;
-
-/**
- * This file is not user visible !
- * But alas, Java does not have a concept of friendly packages
- * so this class is public.
- * Instances of this class are created by invoking getChannel
- * Upon a Input/Output/RandomAccessFile object.
- */
-
-public class FileChannelImpl extends FileChannel
-{
- static
- {
- // load the shared library needed for native methods.
- if (Configuration.INIT_LOAD_LIBRARY)
- {
- System.loadLibrary ("javanio");
- }
- }
-
- public RawData map_address;
-
- int length;
- FileDescriptor fd;
- MappedByteBuffer buf;
- Object file_obj; // just to keep it live...
-
- public FileChannelImpl (FileDescriptor fd, boolean write, Object obj)
- {
- if (!(obj instanceof RandomAccessFile)
- && !(obj instanceof FileInputStream)
- && !(obj instanceof FileOutputStream))
- throw new InternalError ();
-
- this.fd = fd;
- this.file_obj = obj;
- }
-
- public FileChannelImpl ()
- {
- this (new FileDescriptor (), true, null);
- }
-
- private native long implPosition ();
- private native FileChannel implPosition (long newPosition);
- private native FileChannel implTruncate (long size);
-
- private native RawData nio_mmap_file (long pos, long size, int mode);
- private native void nio_unmmap_file (RawData map_address, int size);
- private native void nio_msync (RawData map_address, int length);
-
- public native long size () throws IOException;
-
- protected void implCloseChannel() throws IOException
- {
- if (map_address != null)
- {
- nio_unmmap_file (map_address, (int) length);
- map_address = null;
- }
-
- if (file_obj instanceof RandomAccessFile)
- {
- RandomAccessFile o = (RandomAccessFile) file_obj;
- o.close();
- }
- else if (file_obj instanceof FileInputStream)
- {
- FileInputStream o = (FileInputStream) file_obj;
- o.close();
- }
- else if (file_obj instanceof FileOutputStream)
- {
- FileOutputStream o = (FileOutputStream) file_obj;
- o.close();
- }
- }
-
- public int read (ByteBuffer dst) throws IOException
- {
- // Check if file is mapped into memory.
- if (buf != null)
- {
- // FIXME: implement this
- throw new Error ("Accessing mapped buffers not implemented.");
- }
-
- // File not mapped, access it directly.
- return implRead (dst);
- }
-
- public int read (ByteBuffer dst, long position)
- throws IOException
- {
- if (position < 0)
- throw new IllegalArgumentException ();
-
- if (!isOpen ())
- throw new ClosedChannelException ();
-
- if (file_obj instanceof FileOutputStream)
- throw new NonReadableChannelException ();
-
- int result;
- long oldPosition;
-
- oldPosition = implPosition ();
- position (position);
- result = implRead (dst);
- implPosition (oldPosition);
-
- return result;
- }
-
- private int implRead (ByteBuffer dst) throws IOException
- {
- int result;
- byte[] buffer = new byte [dst.remaining ()];
-
- result = implRead (buffer, 0, buffer.length);
-
- if (result > 0)
- dst.put (buffer, 0, result);
-
- return result;
- }
-
- private native int implRead (byte[] buffer, int offset, int length)
- throws IOException;
-
- public long read (ByteBuffer[] dsts, int offset, int length)
- throws IOException
- {
- long result = 0;
-
- for (int i = offset; i < offset + length; i++)
- {
- result += read (dsts [i]);
- }
-
- return result;
- }
-
- public int write (ByteBuffer src) throws IOException
- {
- // Check if file is mapped into memory.
- if (buf != null)
- {
- // FIXME: implement this
- throw new Error ("Accessing mapped buffers not implemented.");
- }
-
- // File not mapped, access it directly.
- return implWrite (src);
- }
-
- public int write (ByteBuffer src, long position)
- throws IOException
- {
- if (position < 0)
- throw new IllegalArgumentException ();
-
- if (!isOpen ())
- throw new ClosedChannelException ();
-
- if (file_obj instanceof FileInputStream)
- throw new NonWritableChannelException ();
-
- int result;
- long oldPosition;
-
- oldPosition = implPosition ();
- position (position);
- result = implWrite (src);
- implPosition (oldPosition);
-
- return result;
- }
-
- private int implWrite (ByteBuffer src) throws IOException
- {
- byte[] buffer = new byte [src.remaining ()];
-
- src.get (buffer, 0, buffer.length);
- return implWrite (buffer, 0, buffer.length);
- }
-
- private native int implWrite (byte[] buffer, int offset, int length)
- throws IOException;
-
- public long write(ByteBuffer[] srcs, int offset, int length)
- throws IOException
- {
- long result = 0;
-
- for (int i = offset;i < offset + length;i++)
- {
- result += write (srcs[i]);
- }
-
- return result;
- }
-
- public MappedByteBuffer map (FileChannel.MapMode mode, long position,
- long size)
- throws IOException
- {
- if ((mode != MapMode.READ_ONLY
- && mode != MapMode.READ_WRITE
- && mode != MapMode.PRIVATE)
- || position < 0
- || size < 0
- || size > Integer.MAX_VALUE)
- throw new IllegalArgumentException ();
-
- // FIXME: Make this working.
- int cmode = mode.m;
- map_address = nio_mmap_file (position, size, cmode);
- length = (int) size;
- buf = new MappedByteBufferImpl (this);
- return buf;
- }
-
- static MappedByteBuffer create_direct_mapped_buffer (RawData map_address,
- long length)
- throws IOException
- {
- FileChannelImpl ch = new FileChannelImpl ();
- ch.map_address = map_address;
- ch.length = (int) length;
- ch.buf = new MappedByteBufferImpl (ch);
- return ch.buf;
- }
-
- /**
- * msync with the disk
- */
- public void force (boolean metaData) throws IOException
- {
- if (!isOpen ())
- throw new ClosedChannelException ();
-
- // FIXME: What to do with metaData ?
-
- nio_msync (map_address, length);
- }
-
- public long transferTo (long position, long count, WritableByteChannel target)
- throws IOException
- {
- if (position < 0
- || count < 0)
- throw new IllegalArgumentException ();
-
- if (!isOpen ())
- throw new ClosedChannelException ();
-
- if (file_obj instanceof FileOutputStream)
- throw new NonReadableChannelException ();
-
- // XXX: count needs to be casted from long to int. Dataloss ?
- ByteBuffer buffer = ByteBuffer.allocate ((int) count);
- read (buffer, position);
- buffer.flip();
- return target.write (buffer);
- }
-
- public long transferFrom (ReadableByteChannel src, long position, long count)
- throws IOException
- {
- if (position < 0
- || count < 0)
- throw new IllegalArgumentException ();
-
- if (!isOpen ())
- throw new ClosedChannelException ();
-
- if (file_obj instanceof FileInputStream)
- throw new NonWritableChannelException ();
-
- // XXX: count needs to be casted from long to int. Dataloss ?
- ByteBuffer buffer = ByteBuffer.allocate ((int) count);
- src.read (buffer);
- buffer.flip();
- return write (buffer, position);
- }
-
- public FileLock lock (long position, long size, boolean shared)
- throws IOException
- {
- if (position < 0
- || size < 0)
- throw new IllegalArgumentException ();
-
- if (!isOpen ())
- throw new ClosedChannelException ();
-
- if (shared &&
- file_obj instanceof FileOutputStream)
- throw new NonReadableChannelException ();
-
- if (!shared &&
- file_obj instanceof FileInputStream)
- throw new NonWritableChannelException ();
-
- boolean completed = false;
-
- try
- {
- begin();
- lockImpl(position, size, shared);
- completed = true;
- return new FileLockImpl(fd, this, position, size, shared);
- }
- finally
- {
- end(completed);
- }
- }
-
- private native void lockImpl(long position, long size, boolean shared);
-
- public FileLock tryLock (long position, long size, boolean shared)
- throws IOException
- {
- if (position < 0
- || size < 0)
- throw new IllegalArgumentException ();
-
- if (!isOpen ())
- throw new ClosedChannelException ();
-
- if (! tryLockImpl(position, size, shared))
- return null;
-
- boolean completed = false;
-
- try
- {
- boolean lockable = tryLockImpl(position, size, shared);
- completed = true;
- return (lockable
- ? new FileLockImpl(fd, this, position, size, shared)
- : null);
- }
- finally
- {
- end(completed);
- }
- }
-
- private native boolean tryLockImpl(long position, long size, boolean shared);
-
- public long position ()
- throws IOException
- {
- if (!isOpen ())
- throw new ClosedChannelException ();
-
- return implPosition ();
- }
-
- public FileChannel position (long newPosition)
- throws IOException
- {
- if (newPosition < 0)
- throw new IllegalArgumentException ();
-
- if (!isOpen ())
- throw new ClosedChannelException ();
-
- return implPosition (newPosition);
- }
-
- public FileChannel truncate (long size)
- throws IOException
- {
- if (size < 0)
- throw new IllegalArgumentException ();
-
- if (!isOpen ())
- throw new ClosedChannelException ();
-
- if (file_obj instanceof FileInputStream)
- throw new NonWritableChannelException ();
-
- return implTruncate (size);
- }
-}
Index: java/nio/channels/natFileChannelImpl.cc
===================================================================
RCS file: java/nio/channels/natFileChannelImpl.cc
diff -N java/nio/channels/natFileChannelImpl.cc
--- java/nio/channels/natFileChannelImpl.cc 23 Jan 2004 14:37:09 -0000 1.2
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,110 +0,0 @@
-// natFileChannelImpl.cc
-
-/* Copyright (C) 2003 Free Software Foundation
-
- This file is part of libgcj.
-
-This software is copyrighted work licensed under the terms of the
-Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
-details. */
-
-#include <config.h>
-
-#include <jvm.h>
-
-#include <errno.h>
-#include <string.h>
-#include <sys/types.h>
-
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-
-#ifdef HAVE_FCNTL_H
-#include <fcntl.h>
-#endif
-
-#include <gnu/gcj/RawData.h>
-#include <gnu/java/nio/FileLockImpl.h>
-#include <java/io/FileDescriptor.h>
-#include <java/io/IOException.h>
-#include <java/nio/ByteBuffer.h>
-#include <java/nio/channels/FileChannel.h>
-#include <java/nio/channels/FileChannelImpl.h>
-#include <java/nio/channels/FileLock.h>
-
-jlong
-java::nio::channels::FileChannelImpl::size ()
-{
- return fd->getLength ();
-}
-
-jlong
-java::nio::channels::FileChannelImpl::implPosition ()
-{
- return fd->getFilePointer ();
-}
-
-java::nio::channels::FileChannel*
-java::nio::channels::FileChannelImpl::implPosition (jlong newPosition)
-{
- fd->seek (newPosition, ::java::io::FileDescriptor::SET, true);
- return this;
-}
-
-jint
-java::nio::channels::FileChannelImpl::implRead (JArray<jbyte>* buffer,
- jint offset, jint len)
-{
- return fd->read (buffer, offset, len);
-}
-
-jint
-java::nio::channels::FileChannelImpl::implWrite (JArray<jbyte>* buffer,
- jint offset, jint len)
-{
- fd->write (buffer, offset, len);
- return len;
-}
-
-java::nio::channels::FileChannel*
-java::nio::channels::FileChannelImpl::implTruncate (jlong size)
-{
- fd->setLength (size);
- return this;
-}
-
-gnu::gcj::RawData*
-java::nio::channels::FileChannelImpl::nio_mmap_file (jlong /*pos*/, jlong /*size*/,
- jint /*mode*/)
-{
- throw new ::java::io::IOException (JvNewStringUTF ("mmap not implemented"));
-}
-
-void
-java::nio::channels::FileChannelImpl::nio_unmmap_file (gnu::gcj::RawData* /*address*/,
- jint /*size*/)
-{
- throw new ::java::io::IOException (JvNewStringUTF ("munmap not implemented"));
-}
-
-void
-java::nio::channels::FileChannelImpl::nio_msync (gnu::gcj::RawData* /*map_address*/,
- jint /*length*/)
-{
- throw new ::java::io::IOException (JvNewStringUTF ("msync not implemented"));
-}
-
-void
-java::nio::channels::FileChannelImpl::lockImpl(jlong position, jlong size, jboolean shared)
-{
- // FIXME: shared is unused, write is always true.
- fd->lock(position, size, true);
-}
-
-jboolean
-java::nio::channels::FileChannelImpl::tryLockImpl(jlong position, jlong size, jboolean shared)
-{
- // FIXME: shared is unused, write is always true.
- return fd->tryLock(position, size, true);
-}