A DESCRIPTION OF THE PROBLEM :
The code example contains blocks to handle -1 return values from SocketChannel.write() and SocketChannel.read(), and comments stating that these blocks handle the case where the SocketChannel is closed.
This is correct in the case of SocketChannel.read(), but SocketChannel.write() never returns -1,and throws a SocketException if the channel is closed.
The test for -1 after the write(), and the associated empty code block and comment should be deleted.
NOTE: this incorrect code sequence occurs not once but twice.
Note also that in the second occurrence, myNetData.hasRemaining() is incorrectly written as myNetData().hasRemaining().
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
while(myNetData.hasRemaining()) {
int num = socketChannel.write(myNetData);
if (num == 0) {
// no bytes written; try again later
}
ACTUAL -
while(myNetData.hasRemaining()) {
int num = socketChannel.write(myNetData);
if (num == -1) {
// handle closed channel
} else if (num == 0) {
// no bytes written; try again later
}
URL OF FAULTY DOCUMENTATION :
file:///%JAVA_HOME%/docs/guide/security/jsse/JSSERefGuide.html#SSLENG
The code example contains blocks to handle -1 return values from SocketChannel.write() and SocketChannel.read(), and comments stating that these blocks handle the case where the SocketChannel is closed.
This is correct in the case of SocketChannel.read(), but SocketChannel.write() never returns -1,and throws a SocketException if the channel is closed.
The test for -1 after the write(), and the associated empty code block and comment should be deleted.
NOTE: this incorrect code sequence occurs not once but twice.
Note also that in the second occurrence, myNetData.hasRemaining() is incorrectly written as myNetData().hasRemaining().
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
while(myNetData.hasRemaining()) {
int num = socketChannel.write(myNetData);
if (num == 0) {
// no bytes written; try again later
}
ACTUAL -
while(myNetData.hasRemaining()) {
int num = socketChannel.write(myNetData);
if (num == -1) {
// handle closed channel
} else if (num == 0) {
// no bytes written; try again later
}
URL OF FAULTY DOCUMENTATION :
file:///%JAVA_HOME%/docs/guide/security/jsse/JSSERefGuide.html#SSLENG