Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-2192005 | 6u21 | Sunita Koppar | P3 | Resolved | Fixed | b03 |
JDK-2191888 | 6u20-rev | Sunita Koppar | P3 | Closed | Fixed | b03 |
JDK-2188587 | 6u19-rev | Sunita Koppar | P3 | Closed | Fixed | b06 |
JDK-2168688 | 5.0u17 | Sunita Koppar | P3 | Resolved | Fixed | b02 |
JDK-2153750 | 5.0u16-rev | Sunita Koppar | P3 | Closed | Fixed | b07 |
JDK-2168772 | 1.4.2_19 | Sunita Koppar | P3 | Resolved | Fixed | b01 |
JDK-2157730 | 1.4.2_18-rev | Sunita Koppar | P3 | Closed | Fixed | b08 |
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().
- backported by
-
JDK-2168688 (ch) Channels.newOutputStream().write() does not write all data
- Resolved
-
JDK-2168772 (ch) Channels.newOutputStream().write() does not write all data
- Resolved
-
JDK-2192005 (ch) Channels.newOutputStream().write() does not write all data
- Resolved
-
JDK-2153750 (ch) Channels.newOutputStream().write() does not write all data
- Closed
-
JDK-2157730 (ch) Channels.newOutputStream().write() does not write all data
- Closed
-
JDK-2188587 (ch) Channels.newOutputStream().write() does not write all data
- Closed
-
JDK-2191888 (ch) Channels.newOutputStream().write() does not write all data
- Closed
- duplicates
-
JDK-6600423 [1.4.2]packets are lost in socket communication in java.nio
- Closed