final int remaining = buffer.remaining();
if (remaining > 0) {
final SocketUDT socket = socketUDT;
final boolean isBlocking = isBlockingMode;
final int sizeReceived;
try {
if (isBlocking) {
begin(); // JDK contract for NIO blocking calls
}
if (buffer.isDirect()) {
sizeReceived = socket.receive(buffer);
} else {
final byte[] array = buffer.array();
final int position = buffer.position();
final int limit = buffer.limit();
sizeReceived = socket.receive(array, position, limit);
if (0 < sizeReceived && sizeReceived <= remaining) {
buffer.position(position + sizeReceived);
}
}
} finally {
if (isBlocking) {
end(true); // JDK contract for NIO blocking calls
}
}
// see contract for receive()
if (sizeReceived < 0) {
log.trace("nothing was received; socketID={}",
socket.getSocketId());
return 0;
}
if (sizeReceived == 0) {
log.trace("receive timeout; socketID={}", socket.getSocketId());
return 0;
}
if (sizeReceived <= remaining) {
return sizeReceived;
} else { // should not happen
log.error("unexpected: sizeReceived > remaining; socketID={}",
socket.getSocketId());
return 0;
}
} else {
return 0;