FileDocCategorySizeDatePackage
OutputWriter.javaAPI DocExample7815Tue May 29 16:57:10 BST 2007com.sun.xml.ws.transport.tcp.io

OutputWriter

public final class OutputWriter extends Object
NIO utility to flush ByteBuffer
author
Scott Oaks
author
Alexey Stashok

Fields Summary
private static final Logger
logger
Constructors Summary
Methods Summary
public static voidflushChannel(java.nio.channels.SocketChannel socketChannel, java.nio.ByteBuffer bb)
Flush the buffer by looping until the ByteBuffer is empty

param
bb the ByteBuffer to write.

    
                         
            
     
        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 voidflushChannel(java.nio.channels.SocketChannel socketChannel, java.nio.ByteBuffer[] bb)
Flush the buffer by looping until the ByteBuffer is empty

param
bb the ByteBuffer to write.

        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 booleanhasRemaining(java.nio.ByteBuffer[] bb)

        for(int i=bb.length - 1; i>=0; i--) {
            if (bb[i].hasRemaining()) {
                return true;
            }
        }
        
        return false;