OutputWriterpublic final class OutputWriter extends Object NIO utility to flush ByteBuffer |
Fields Summary |
---|
private static final Logger | logger |
Methods Summary |
---|
public static void | flushChannel(java.nio.channels.SocketChannel socketChannel, java.nio.ByteBuffer bb)Flush the buffer by looping until the ByteBuffer is empty
if (logger.isLoggable(Level.FINEST)) {
Socket socket = socketChannel.socket();
logger.log(Level.FINEST, MessagesMessages.WSTCP_1070_OUTPUT_WRITER_DUMP(socket.getInetAddress().getHostAddress(), socketChannel.socket().getPort()));
logger.log(Level.FINEST, DumpUtils.dumpBytes(bb));
}
SelectionKey key = null;
Selector writeSelector = null;
int attempts = 0;
try {
while ( bb.hasRemaining() ) {
final int len = socketChannel.write(bb);
attempts++;
if (len < 0){
throw new EOFException();
}
if (len == 0) {
if ( writeSelector == null ){
writeSelector = SelectorFactory.getSelector();
if ( writeSelector == null){
// Continue using the main one.
continue;
}
}
key = socketChannel.register(writeSelector, key.OP_WRITE);
if (writeSelector.select(30 * 1000) == 0) {
if (attempts > 2) {
Socket socket = socketChannel.socket();
throw new IOException(MessagesMessages.WSTCP_0019_PEER_DISCONNECTED(socket.getInetAddress().getHostAddress(), socketChannel.socket().getPort()));
}
} else {
attempts--;
}
} else {
attempts = 0;
}
}
} finally {
if (key != null) {
key.cancel();
key = null;
}
if ( writeSelector != null ) {
// Cancel the key.
writeSelector.selectNow();
SelectorFactory.returnSelector(writeSelector);
}
}
| public static void | flushChannel(java.nio.channels.SocketChannel socketChannel, java.nio.ByteBuffer[] bb)Flush the buffer by looping until the ByteBuffer is empty
if (logger.isLoggable(Level.FINEST)) {
Socket socket = socketChannel.socket();
logger.log(Level.FINEST, MessagesMessages.WSTCP_1070_OUTPUT_WRITER_DUMP(socket.getInetAddress().getHostAddress(), socketChannel.socket().getPort()));
logger.log(Level.FINEST, DumpUtils.dumpBytes(bb));
}
SelectionKey key = null;
Selector writeSelector = null;
int attempts = 0;
try {
while (hasRemaining(bb)) {
final long len = socketChannel.write(bb);
attempts++;
if (len < 0){
throw new EOFException();
}
if (len == 0) {
if ( writeSelector == null ){
writeSelector = SelectorFactory.getSelector();
if ( writeSelector == null){
// Continue using the main one.
continue;
}
}
key = socketChannel.register(writeSelector, key.OP_WRITE);
if (writeSelector.select(30 * 1000) == 0) {
if (attempts > 2) {
Socket socket = socketChannel.socket();
throw new IOException(MessagesMessages.WSTCP_0019_PEER_DISCONNECTED(socket.getInetAddress().getHostAddress(), socketChannel.socket().getPort()));
}
} else {
attempts--;
}
} else {
attempts = 0;
}
}
} finally {
if (key != null) {
key.cancel();
key = null;
}
if ( writeSelector != null ) {
// Cancel the key.
writeSelector.selectNow();
SelectorFactory.returnSelector(writeSelector);
}
}
| private static boolean | hasRemaining(java.nio.ByteBuffer[] bb)
for(int i=bb.length - 1; i>=0; i--) {
if (bb[i].hasRemaining()) {
return true;
}
}
return false;
|
|