This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

FYI: Patch: java.nio - first working file channel implementation


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi list,


I commited the attached patch to trunk to make 
gnu.java.nio.FileChannelImpl a little bit working. Normal file 
operations should work now as expected (modulo bugs). Working with 
mapped files is not implemented yet.


Michael
- -- 
Homepage: http://www.worldforge.org/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQE+sgQtWSOgCCdjSDsRAgBEAJ9VQ2TIBOjuDxIZR7bo3Kwj1m37qQCaApQL
0MuM6Ch98iKcjQ3O5DmInhg=
=Dc+S
-----END PGP SIGNATURE-----
Index: ChangeLog
===================================================================
RCS file: /cvs/gcc/gcc/libjava/ChangeLog,v
retrieving revision 1.1867
diff -u -r1.1867 ChangeLog
--- ChangeLog	1 May 2003 21:36:16 -0000	1.1867
+++ ChangeLog	2 May 2003 05:32:54 -0000
@@ -1,3 +1,25 @@
+2003-05-02  Michael Koch  <konqueror@gmx.de>
+
+	* gnu/java/nio/FileChannelImpl.java
+	(read): New implementation.
+	(implRead): New methods.
+	(write): New implementation, call other write insteal of read method.
+	(implWrite): New methods.
+	(map): Added comment.
+	(transferFrom): Implemented.
+	(transferTo): Implemented.
+	(lock): Added checks to throw exceptions.
+	(truncate): Added check to throw exception.
+	* gnu/java/nio/natFileChannelImpl.cc
+	(implRead): New method.
+	(implWrite): New method.
+	* java/nio/ByteBuffer.java
+	(hashCode): Fixed comment.
+	(get): Fixed exception documentation.
+	(put): Fixed exception documentation.
+	* java/nio/CharBuffer.java:
+	Added comment for later optimizations.
+
 2003-04-30  Tom Tromey  <tromey@redhat.com>
 
 	PR libgcj/10582:
Index: gnu/java/nio/FileChannelImpl.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/gnu/java/nio/FileChannelImpl.java,v
retrieving revision 1.4
diff -u -r1.4 FileChannelImpl.java
--- gnu/java/nio/FileChannelImpl.java	20 Mar 2003 11:14:35 -0000	1.4
+++ gnu/java/nio/FileChannelImpl.java	2 May 2003 05:32:54 -0000
@@ -126,19 +126,15 @@
 
   public int read (ByteBuffer dst) throws IOException
   {
-    int s = (int)size();
-
-    if (buf == null)
+    // Check if file is mapped into memory.
+    if (buf != null)
       {
-        throw new EOFException("file not mapped");
+	// FIXME: implement this
+        throw new Error ("Accessing mapped buffers not implemented.");
       }
 
-    for (int i = 0; i < s; i++)
-      {
-        dst.put (buf.get());
-      }
-
-    return s;
+    // File not mapped, access it directly.
+    return implRead (dst);
   }
 
   public int read (ByteBuffer dst, long position)
@@ -149,11 +145,33 @@
 
     if (!isOpen ())
       throw new ClosedChannelException ();
+   
+    if (file_obj instanceof FileOutputStream)
+      throw new NonReadableChannelException ();
+
+    int result;
+    long oldPosition;
+
+    oldPosition = implPosition ();
+    result = implRead (dst);
+    implPosition (oldPosition);
     
-    // FIXME: check for NonReadableChannelException
+    return result;
+  }
 
-    throw new Error ("Not implemented");
+  private int implRead (ByteBuffer dst) throws IOException
+  {
+    int result;
+    byte[] buffer = new byte [dst.remaining ()];
+    
+    result = implRead (buffer, 0, buffer.length);
+    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
@@ -162,7 +180,7 @@
 
     for (int i = offset; i < offset + length; i++)
       {
-        result += write (dsts [i]);
+        result += read (dsts [i]);
       }
 
     return result;
@@ -170,20 +188,15 @@
 
   public int write (ByteBuffer src) throws IOException
   {
-    int w = 0;
-
-    if (buf == null)
+    // Check if file is mapped into memory.
+    if (buf != null)
       {
-        throw new EOFException ("file not mapped");
+	// FIXME: implement this
+        throw new Error ("Accessing mapped buffers not implemented.");
       }
-
-    while (src.hasRemaining ())
-      {
-        buf.put (src.get ());
-        w++;
-      }
-
-    return w;
+    
+    // File not mapped, access it directly.
+    return implWrite (src);
   }
     
   public int write (ByteBuffer src, long position)
@@ -195,11 +208,30 @@
     if (!isOpen ())
       throw new ClosedChannelException ();
     
-    // FIXME: check for NonWritableChannelException
+    if (file_obj instanceof FileInputStream)
+       throw new NonWritableChannelException ();
 
-    throw new Error ("Not implemented");
+    int result;
+    long oldPosition;
+
+    oldPosition = implPosition ();
+    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
   {
@@ -225,6 +257,7 @@
         || 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;
@@ -272,10 +305,13 @@
     if (!isOpen ())
       throw new ClosedChannelException ();
 
-    // FIXME: check for NonReadableChannelException
-    // FIXME: check for NonWritableChannelException
-    
-    throw new Error ("Not implemented");
+    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);
+    return target.write (buffer);
   }
 
   public long transferFrom (ReadableByteChannel src, long position, long count)
@@ -288,10 +324,13 @@
     if (!isOpen ())
       throw new ClosedChannelException ();
 
-    // FIXME: check for NonReadableChannelException
-    // FIXME: check for NonWritableChannelException
-    
-    throw new Error ("Not implemented");
+    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);
+    return write (buffer, position);
   }
 
   public FileLock lock (long position, long size, boolean shared)
@@ -304,9 +343,14 @@
     if (!isOpen ())
       throw new ClosedChannelException ();
 
-    // FIXME: check for NonReadableChannelException
-    // FIXME: check for NonWritableChannelException
-    
+    if (shared &&
+        file_obj instanceof FileOutputStream)
+      throw new NonReadableChannelException ();
+	
+    if (!shared &&
+        file_obj instanceof FileInputStream)
+      throw new NonWritableChannelException ();
+	
     throw new Error ("Not implemented");
   }
   
@@ -353,7 +397,8 @@
     if (!isOpen ())
       throw new ClosedChannelException ();
 
-    // FIXME: check for NonWritableChannelException
+    if (file_obj instanceof FileInputStream)
+       throw new NonWritableChannelException ();
 
     return implTruncate (size);
   }
Index: gnu/java/nio/natFileChannelImpl.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/gnu/java/nio/natFileChannelImpl.cc,v
retrieving revision 1.3
diff -u -r1.3 natFileChannelImpl.cc
--- gnu/java/nio/natFileChannelImpl.cc	20 Mar 2003 11:14:35 -0000	1.3
+++ gnu/java/nio/natFileChannelImpl.cc	2 May 2003 05:32:54 -0000
@@ -28,6 +28,7 @@
 #include <gnu/java/nio/FileChannelImpl.h>
 #include <java/io/FileDescriptor.h>
 #include <java/io/IOException.h>
+#include <java/nio/ByteBuffer.h>
 #include <java/nio/channels/FileChannel.h>
 
 jlong
@@ -47,6 +48,21 @@
 {
   fd->seek (newPosition, ::java::io::FileDescriptor::SET, true);
   return this;
+}
+
+jint
+gnu::java::nio::FileChannelImpl::implRead (JArray<jbyte>* buffer,
+                                           jint offset, jint len)
+{
+  return fd->read (buffer, offset, len);
+}
+
+jint
+gnu::java::nio::FileChannelImpl::implWrite (JArray<jbyte>* buffer,
+                                            jint offset, jint len)
+{
+  fd->write (buffer, offset, len);
+  return len;
 }
 
 java::nio::channels::FileChannel*
Index: java/nio/ByteBuffer.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/nio/ByteBuffer.java,v
retrieving revision 1.9
diff -u -r1.9 ByteBuffer.java
--- java/nio/ByteBuffer.java	17 Mar 2003 15:31:33 -0000	1.9
+++ java/nio/ByteBuffer.java	2 May 2003 05:32:54 -0000
@@ -251,7 +251,7 @@
    */
   public int hashCode()
   {
-    // FIXME: Check what SUN calcs here
+    // FIXME: Check what SUN calculates here
     return super.hashCode();
   }
 
@@ -344,7 +344,8 @@
   /**
    * Absolute get method.
    *
-   * @exception IndexOutOfBoundsException FIXME
+   * @exception IndexOutOfBoundsException If index &lt; 0 or index &gt;= this
+   * buffers limit.
    */
   public abstract byte get (int index);
   
@@ -352,7 +353,8 @@
    * Absolute put method.
    *
    * @exception ReadOnlyBufferException If this buffer is read-only
-   * @exception IndexOutOfBoundsException FIXME
+   * @exception IndexOutOfBoundsException If index &lt; 0 or index &gt;= this
+   * buffers limit.
    */
   public abstract ByteBuffer put (int index, byte b);
   
Index: java/nio/CharBuffer.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/nio/CharBuffer.java,v
retrieving revision 1.7
diff -u -r1.7 CharBuffer.java
--- java/nio/CharBuffer.java	11 Mar 2003 10:30:52 -0000	1.7
+++ java/nio/CharBuffer.java	2 May 2003 05:32:54 -0000
@@ -83,6 +83,9 @@
    */
   final public static CharBuffer wrap (CharSequence a, int offset, int length)
   {
+    // FIXME: implement better handling of java.lang.String.
+    // Probably share data with String via reflection.
+	  
     if ((offset < 0)
         || (offset > a.length ())
         || (length < 0)

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]