commit-classpath
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Make FileChannelImpl.read(byte[]) less blocking


From: Mark Wielaard
Subject: Make FileChannelImpl.read(byte[]) less blocking
Date: Sun, 25 Apr 2004 17:13:26 +0200

Hi,

The following patch makes the native JNU FileChannelImpl.read(byte[],
int, int) return as soon as at least some part of the given byte buffer
has been filled. This also includes a small patch from Stephen Crawley
(patch #2951 in Savannah) that I rediscovered during testing. Only the
read version was applied since the write version was already correct.
(Sorry for not applying this earlier Stephen. It would also have saved
 me some time...)

2004-04-25  Stephen Crawley  <address@hidden>
            Mark Wielaard  <address@hidden>

    * native/jni/java-nio/gnu_java_nio_channels_FileChannelImpl.c
    (Java_gnu_java_nio_channels_FileChannelImpl_read___3BII):
    Fill buffer with a  do-while bytes_read < 1 loop.
    Check for length == 0.

This makes sure that Mauve test FileInputStream.read: empty byte[] read
PASSes and that the following program terminates correctly (it hangs
with the current code):

import java.io.*;

public class T2
{
  public static void main(String[] args) throws Exception
  {
    Reader r = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(r);
    String line = br.readLine();
    System.out.println(line);
  }
}

Comments? Objections?

Cheers,

Mark
Index: native/jni/java-nio/gnu_java_nio_channels_FileChannelImpl.c
===================================================================
RCS file: 
/cvsroot/classpath/classpath/native/jni/java-nio/gnu_java_nio_channels_FileChannelImpl.c,v
retrieving revision 1.5
diff -u -r1.5 gnu_java_nio_channels_FileChannelImpl.c
--- native/jni/java-nio/gnu_java_nio_channels_FileChannelImpl.c 12 Apr 2004 
14:38:55 -0000      1.5
+++ native/jni/java-nio/gnu_java_nio_channels_FileChannelImpl.c 25 Apr 2004 
15:09:14 -0000
@@ -549,8 +549,12 @@
       return(-1);
     }
 
+  /* Must return 0 if an attempt is made to read 0 bytes. */
+  if (length == 0)
+    return 0;
+
   bytes_read = 0;
-  while (bytes_read < length)
+  do
     {
       TARGET_NATIVE_FILE_READ(native_fd, (bufptr + offset + bytes_read), 
(length - bytes_read), n, result);
       if ((result == TARGET_NATIVE_OK) && (n == 0))
@@ -571,6 +575,7 @@
         }
       bytes_read += n;
     }
+  while (bytes_read < 1);
 
   (*env)->ReleaseByteArrayElements(env, buffer, bufptr, 0);
   return CONVERT_SSIZE_T_TO_JINT(bytes_read);

Attachment: signature.asc
Description: This is a digitally signed message part


reply via email to

[Prev in Thread] Current Thread [Next in Thread]