FileDocCategorySizeDatePackage
OutputWriter.javaAPI DocGlassfish v2 API8136Fri May 04 22:37:04 BST 2007com.sun.enterprise.web.connector.grizzly

OutputWriter

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

Fields Summary
private static int
defaultWriteTimeout
The default rime out before closing the connection
Constructors Summary
Methods Summary
public static longflushChannel(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.

    
    
                            
          
             
        return flushChannel(socketChannel,bb,defaultWriteTimeout);
    
public static longflushChannel(java.nio.channels.SocketChannel socketChannel, java.nio.ByteBuffer bb, long writeTimeout)
Flush the buffer by looping until the ByteBuffer is empty

param
bb the ByteBuffer to write.

    
        
        if (bb == null){
            throw new IllegalStateException("Invalid Response State. ByteBuffer" 
                    + " cannot be null.");
        }
        
        if (socketChannel == null){
            throw new IllegalStateException("Invalid Response State. " +
                    "SocketChannel cannot be null.");
        }    

        SelectionKey key = null;
        Selector writeSelector = null;
        int attempts = 0;
        int bytesProduced = 0;
        try {
            while ( bb.hasRemaining() ) {
                int len = socketChannel.write(bb);
                attempts++;
                if (len < 0){
                    throw new EOFException();
                } 
            
                bytesProduced += len;
                
                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(writeTimeout) == 0) {
                        if (attempts > 2)
                            throw new IOException("Client disconnected");
                    } else {
                        attempts--;
                    }
                } else {
                    attempts = 0;
                }
            }   
        } finally {
            if (key != null) {
                key.cancel();
                key = null;
            }
            
            if ( writeSelector != null ) {
                // Cancel the key.
                writeSelector.selectNow();
                SelectorFactory.returnSelector(writeSelector);
            }
        }
        return bytesProduced;
    
public static longflushChannel(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.

        return flushChannel(socketChannel,bb,defaultWriteTimeout);
    
public static longflushChannel(java.nio.channels.SocketChannel socketChannel, java.nio.ByteBuffer[] bb, long writeTimeout)
Flush the buffer by looping until the ByteBuffer is empty

param
bb the ByteBuffer to write.

      
        if (bb == null){
            throw new IllegalStateException("Invalid Response State. ByteBuffer" 
                    + " cannot be null.");
        }
        
        if (socketChannel == null){
            throw new IllegalStateException("Invalid Response State. " +
                    "SocketChannel cannot be null.");
        }   
        
        SelectionKey key = null;
        Selector writeSelector = null;
        int attempts = 0;
        long totalBytes = 0;
        for (ByteBuffer aBb : bb) {
            totalBytes += aBb.remaining();
        }
        
        long byteProduced = 0;
        try {
            while (byteProduced < totalBytes ) {
                long len = socketChannel.write(bb);
                attempts++;
                byteProduced += len;
                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(writeTimeout) == 0) {
                        if (attempts > 2)
                            throw new IOException("Client disconnected");
                    } else {
                        attempts--;
                    }
                } else {
                    attempts = 0;
                }
            }   
        } finally {
            if (key != null) {
                key.cancel();
                key = null;
            }
            
            if ( writeSelector != null ) {
                // Cancel the key.
                writeSelector.selectNow();
                SelectorFactory.returnSelector(writeSelector);
            }
        }
        return byteProduced;
    
public static intgetDefaultWriteTimeout()

        return defaultWriteTimeout;
    
public static voidsetDefaultWriteTimeout(int aDefaultWriteTimeout)

        defaultWriteTimeout = aDefaultWriteTimeout;