bsd implementation of Java_sun_nio_ch_FileChannelImpl_transferTo0 ignores "count" argument

Michael Allman msa at allman.ms
Fri Jan 18 00:47:33 PST 2008


On Fri, 18 Jan 2008, Greg Lewis wrote:

> On Thu, Jan 17, 2008 at 08:13:00PM -0800, Michael Allman wrote:
>> I can attempt to provide a patch, but C is not my thing, and I could not
>> make a reliable claim as to the correctness of my patch.  Also, there's
>> the whole license issue.  Fixing this seems simple enough, though.  I
>> would like to help insofar as I can.
>
> Something like this maybe? (Untested)
>
> --- ../../j2se/src/solaris/native/sun/nio/ch/FileChannelImpl.c	6 Mar 2007 17:45:05 -0000	1.11
> +++ ../../j2se/src/solaris/native/sun/nio/ch/FileChannelImpl.c	18 Jan 2008 08:11:36 -0000
> @@ -264,11 +264,12 @@
>         return IOS_THROWN;
>     }
>
> -    while ((r = pread(srcFD, buf, 4096, offset)) > 0) {
> +    while (count > 0 && (r = pread(srcFD, buf, (count < 4096) ? count : 4096, offset)) > 0) {
>         w = write(dstFD, buf, r);
>         if (w == -1)
> 	    break;
>         offset += w;
> +        count -= w;
>     }
>     free(buf);

You're reading my mind.

>> Also, I know this is getting pushy, but it seems like fixing this issue
>> would be a great time to replace the current implementation of this method
>> with a true bsd sendfile() call.  I could give that a try, too.
>
> As noted in the comments, you can't since the BSD implementation of
> sendfile(2) expects to be sending the file to a socket, not just any old
> file descriptor.  Solaris and Linux are both ok with any old fd.

I think the calling code can handle a failure gracefully.  See 
FileChannelImpl.java, transferToDirectly().

I am going to have a go at this (sendfile support) in leopard.  The 
freebsd call is a little different, and I don't have ready access to a 
freebsd machine.

Michael


More information about the freebsd-java mailing list