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

(ch) Channels.newOutputStream().write() does not write all data

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 7
    • 1.4.2_08, 5.0
    • core-libs
    • b25
    • x86, itanium
    • linux_redhat_3.0, solaris_8
    • Verified

        FULL PRODUCT VERSION :
        java version "1.5.0_06"
        Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
        Java HotSpot(TM) 64-Bit Server VM (build 1.5.0_06-b05, mixed mode)


        ADDITIONAL OS VERSION INFORMATION :
        Linux rhap-grid1 2.6.5-7.97-smp #1 SMP Fri Jul 2 14:21:59 UTC 2004 x86_64 x86_64 x86_64 GNU/Linux

        A DESCRIPTION OF THE PROBLEM :
        The implementation of the OutputStream returned by Channels.newOutputStream() ignores the return code from the underlying channel and hence does not write all the data if the channel performs short writes.

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        1/ Let the WritableByteChannel be:

        public class SillyWritableByteChannel implements WritableByteChannel {
            public int write(ByteBuffer src) throws IOException {
                if(src.remaining() > 0) {
                    System.out.println( src.get() );
                    return 1;
                }
                return 0;
            }

            public void close() throws IOException {
            }

            public boolean isOpen() {
                return true;
            }
        }

        2/ If the above channel is passed to Channels.newOutputStream() and a byte array of length > 1 passed to the write method, only the first byte will be written.

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        All bytes should be written according to the OutputStream contract.
        ACTUAL -
        The number of bytes written in the first call to WritableByteChannel.write() are written.

        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        import java.io.IOException;
        import java.io.OutputStream;
        import java.nio.ByteBuffer;
        import java.nio.channels.Channels;
        import java.nio.channels.WritableByteChannel;
        import java.util.Random;

        import junit.framework.TestCase;

        public class ChannelsNewOutputStreamTest extends TestCase {

        public void testWrite() throws Exception {
        Random random = new Random();
        byte[] data = new byte[8192];
        random.nextBytes(data);

        SillyWritableByteChannel writableByteChannel = new SillyWritableByteChannel();
        OutputStream outputStream = Channels.newOutputStream(writableByteChannel);
        outputStream.write(data);
        assertEquals(data.length, writableByteChannel.getNumBytesWritten());
        }

        static class SillyWritableByteChannel implements WritableByteChannel {
        private int numBytesWritten = 0;

        public int write(ByteBuffer src) throws IOException {
        if(src.remaining() > 0) {
        src.get();
        this.numBytesWritten++;
        return 1;
        }
        return 0;
        }

        public void close() throws IOException {
        ;
        }

        public boolean isOpen() {
        return true;
        }

        public int getNumBytesWritten() {
        return this.numBytesWritten;
        }
        }
        }

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

        CUSTOMER SUBMITTED WORKAROUND :
        Do not use Channels.newOutputStream().

              alanb Alan Bateman
              ndcosta Nelson Dcosta (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: