Issue Type: Bug Bug
Affects Versions: JRuby 1.7.3
Assignee: Unassigned
Components: Java Integration
Created: 26/Apr/13 4:33 PM
Description:

There is a bug in the copy_streams method whereby the destination (file) that is being written ends up being at most 4k long. The bug, I believe, is here:

RubyIO.java
    private static long transfer(ReadableByteChannel from, FileChannel to) throws IOException {
        long transferred = 0;
        long bytes;
        while ((bytes = to.transferFrom(from, to.position(), 4196)) > 0) {
            transferred += bytes;
        }

        return transferred;
    }

The problem is with the use of to.position(). According to the JavaDocs the FileChannel (http://docs.oracle.com/javase/7/docs/api/java/nio/channels/FileChannel.html) the transferFrom method does not update the destination's position. This means that if the source is more than 4196 bytes it'll just repeatedly overwrite the first 4196 bytes of the file and the end result will be a 4k file. Additionally, the sum of the bytes written will be the expected value. Just the file will be too short. The fix would be this:

RubyIO.java
    private static long transfer(ReadableByteChannel from, FileChannel to) throws IOException {
        long transferred = 0;
        long bytes;
        while ((bytes = to.transferFrom(from, to.position()+transferred, 4196)) > 0) {
            transferred += bytes;
        }

        return transferred;
    }
Environment: Embedded Jetty server running JRuby/Rails via a RackServlet
Project: JRuby
Priority: Major Major
Reporter: Seth Wright
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira
--------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email

Reply via email to