Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-5105752

(fc) FileChannel.transferFrom throws IllegalArgumentException count > Integer.MA

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.4.2
    • core-libs
    • x86
    • linux



      Name: rmT116609 Date: 09/22/2004


      FULL PRODUCT VERSION :
      java version "1.4.2_05"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_05-b04)
      Java HotSpot(TM) Client VM (build 1.4.2_05-b04, mixed mode)

      java version "1.5.0-rc"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-rc-b63)
      Java HotSpot(TM) Client VM (build 1.5.0-rc-b63, mixed mode, sharing)


      ADDITIONAL OS VERSION INFORMATION :
      Linux 2.6.8-1.521.smp #1 SMP Thu Sep 2 20:34:09 CEST 2004 i686 i686 i386 GNU/Linux

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      I could reproduce this on the Windows version too.

      A DESCRIPTION OF THE PROBLEM :
      The FileChannel#transferFrom throws IllegalArgumentException, when:
       - a custom ReadableByteChannel is used;
       - the count argument (last parameter in the call) is bigger than Integer.MAX_VALUE;

      The exception is not thrown if the count is <= Integer.MAX_VALUE.

      Looks like a casting problem in the algorithm.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Use the provided test case to reproduce it (needs junit on the classpath):

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      It will fail on the second assertion.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :

      java.lang.IllegalArgumentException
      at java.nio.Buffer.limit(Buffer.java:249)
      at sun.nio.ch.FileChannelImpl.transferFromArbitraryChannel(FileChannelImpl.java:565)
      at sun.nio.ch.FileChannelImpl.transferFrom(FileChannelImpl.java:606)
      at TestFileChannel.testTransferFromArbitraryChannel(TestFileChannel.java:27)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at com.intellij.rt.execution.junit2.JUnitStarter.main(JUnitStarter.java:31)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:78)


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import junit.framework.TestCase;

      import java.io.RandomAccessFile;
      import java.io.File;
      import java.io.IOException;
      import java.nio.channels.ReadableByteChannel;
      import java.nio.ByteBuffer;

      /**
       * @author <a href="mailto:###@###.###">Csaba Nagy</a>
       * @version $Revision: 1.2 $ $Date: 2004/09/21 09:23:49 $
       */
      public class TestFileChannel extends TestCase {

          public void testTransferFromArbitraryChannel() throws Exception {
              final byte[] bytes = "Test arbitrary channel".getBytes();
              ReadableByteChannel srcChannel = new DummyChannel(bytes);
              File test1 = File.createTempFile("test_transfer_from_arbitrary_channel_", ".txt");
              RandomAccessFile file1 = new RandomAccessFile(test1, "rw");
              file1.getChannel().transferFrom(srcChannel, 0, 1000);

              assertEquals(bytes.length, file1.length());

              srcChannel = new DummyChannel(bytes);
              File test2 = File.createTempFile("test_transfer_from_arbitrary_channel_", ".txt");
              RandomAccessFile file2 = new RandomAccessFile(test2, "rw");
              file2.getChannel().transferFrom(srcChannel, 0, Integer.MAX_VALUE + 1L);

              assertEquals(bytes.length, file2.length());

          }


          private static class DummyChannel implements ReadableByteChannel {
              int position = 0;
              private final byte[] bytes;
              public DummyChannel(byte[] bytes) {
                  this.bytes = bytes;
              }
              public int read(ByteBuffer dst) throws IOException {
                  int count = Math.min(bytes.length - position, dst.remaining());
                  dst.put(bytes, position, count);
                  position += count;
                  return count;
              }
              public void close() {}
              public boolean isOpen() { return true; }
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      use as second parameter values smaller than Integer.MAX_VALUE
      (Incident Review ID: 311366)
      ======================================================================

            iris Iris Clark
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: