[kaffe] CVS kaffe (robilad): Resynced with GNU Classpath: FileChannelImpl fix

Kaffe CVS cvs-commits at kaffe.org
Mon Mar 14 18:14:50 PST 2005


PatchSet 5565 
Date: 2005/03/15 02:09:28
Author: robilad
Branch: HEAD
Tag: (none) 
Log:
Resynced with GNU Classpath: FileChannelImpl fix

2005-03-13  Dalibor Topic  <robilad at kaffe.org>

Resynced with GNU Classpath.

2005-03-10  Andrew Haley  <aph at redhat.com>

* gnu/java/nio/channels/FileChannelImpl.java (smallTransferFrom):
New.
(smallTransferTo): New.
(transferFrom): Loop around smallTransferFrom, copying pageSize
bytes each time.
(transferTo): Likewise.

Members: 
	ChangeLog:1.3739->1.3740 
	libraries/javalib/gnu/java/nio/channels/FileChannelImpl.java:1.6->1.7 

Index: kaffe/ChangeLog
diff -u kaffe/ChangeLog:1.3739 kaffe/ChangeLog:1.3740
--- kaffe/ChangeLog:1.3739	Tue Mar 15 02:00:24 2005
+++ kaffe/ChangeLog	Tue Mar 15 02:09:28 2005
@@ -2,6 +2,19 @@
 
 	Resynced with GNU Classpath.
 	
+	2005-03-10  Andrew Haley  <aph at redhat.com>
+
+	* gnu/java/nio/channels/FileChannelImpl.java (smallTransferFrom):
+	New.
+	(smallTransferTo): New.
+	(transferFrom): Loop around smallTransferFrom, copying pageSize
+	bytes each time.
+	(transferTo): Likewise.
+	
+2005-03-13  Dalibor Topic  <robilad at kaffe.org>
+
+	Resynced with GNU Classpath.
+	
 	2005-03-11  Chris Burdess  <dog at gnu.org>
 
 	* gnu/xml/aelfred2/SAXDriver.java: Corrected bug handling URI
Index: kaffe/libraries/javalib/gnu/java/nio/channels/FileChannelImpl.java
diff -u kaffe/libraries/javalib/gnu/java/nio/channels/FileChannelImpl.java:1.6 kaffe/libraries/javalib/gnu/java/nio/channels/FileChannelImpl.java:1.7
--- kaffe/libraries/javalib/gnu/java/nio/channels/FileChannelImpl.java:1.6	Fri Mar 11 23:13:46 2005
+++ kaffe/libraries/javalib/gnu/java/nio/channels/FileChannelImpl.java	Tue Mar 15 02:09:33 2005
@@ -299,7 +299,30 @@
       throw new ClosedChannelException ();
   }
 
-  public long transferTo (long position, long count, WritableByteChannel target)
+  // like transferTo, but with a count of less than 2Gbytes
+  private int smallTransferTo (long position, int count, 
+			       WritableByteChannel target)
+    throws IOException
+  {
+    ByteBuffer buffer;
+    try
+      {
+	// Try to use a mapped buffer if we can.  If this fails for
+	// any reason we'll fall back to using a ByteBuffer.
+	buffer = map (MapMode.READ_ONLY, position, count);
+      }
+    catch (IOException e)
+      {
+	buffer = ByteBuffer.allocate (count);
+	read (buffer, position);
+	buffer.flip();
+      }
+
+    return target.write (buffer);
+  }
+
+  public long transferTo (long position, long count, 
+			  WritableByteChannel target)
     throws IOException
   {
     if (position < 0
@@ -312,14 +335,57 @@
     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);
+    final int pageSize = 65536;
+    long total = 0;
+
+    while (count > 0)
+      {
+	int transferred 
+	  = smallTransferTo (position, (int)Math.min (count, pageSize), 
+			     target);
+	if (transferred < 0)
+	  break;
+	total += transferred;
+	position += transferred;
+	count -= transferred;
+      }
+
+    return total;
   }
 
-  public long transferFrom (ReadableByteChannel src, long position, long count)
+  // like transferFrom, but with a count of less than 2Gbytes
+  private int smallTransferFrom (ReadableByteChannel src, long position, 
+				 int count)
+    throws IOException
+  {
+    ByteBuffer buffer = null;
+
+    if (src instanceof FileChannel)
+      {
+	try
+	  {
+	    // Try to use a mapped buffer if we can.  If this fails
+	    // for any reason we'll fall back to using a ByteBuffer.
+	    buffer = ((FileChannel)src).map (MapMode.READ_ONLY, position, 
+					     count);
+	  }
+	catch (IOException e)
+	  {
+	  }
+      }
+
+    if (buffer == null)
+      {
+	buffer = ByteBuffer.allocate ((int) count);
+	src.read (buffer);
+	buffer.flip();
+      }
+
+    return write (buffer, position);
+  }
+
+  public long transferFrom (ReadableByteChannel src, long position, 
+			    long count)
     throws IOException
   {
     if (position < 0
@@ -332,11 +398,21 @@
     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);
+    final int pageSize = 65536;
+    long total = 0;
+
+    while (count > 0)
+      {
+	int transferred = smallTransferFrom (src, position, 
+					     (int)Math.min (count, pageSize));
+	if (transferred < 0)
+	  break;
+	total += transferred;
+	position += transferred;
+	count -= transferred;
+      }
+
+    return total;
   }
 
   public FileLock tryLock (long position, long size, boolean shared)




More information about the kaffe mailing list