JDK-6448457 : (ch) Channels.newOutputStream().write() does not write all data
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 1.4.2_08,5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: linux_redhat_3.0,solaris_8
  • CPU: x86,itanium
  • Submitted: 2006-07-13
  • Updated: 2011-02-16
  • Resolved: 2010-11-30
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availabitlity Release.

To download the current JDK release, click here.
Other JDK 6 JDK 7
1.4.2_18-revFixed 6u19-revFixed 7 b25Fixed
Related Reports
Duplicate :  
Description
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().

Comments
EVALUATION As the channel, depending on its type and state, may only write some (or none) of the requested bytes then this case must be handled so as to implement the OutputStream contract.
2006-07-13