FileDocCategorySizeDatePackage
ASOutputStream.javaAPI DocGlassfish v2 API5954Fri May 04 22:35:52 BST 2007com.sun.enterprise.server.ss.provider

ASOutputStream

public final class ASOutputStream extends OutputStream
OutputStream implementation for the socket wrappers of Quick startup implementation. Implementation is thread safe while using nio to read and write simultaneusly in the non-blocking mode.

Fields Summary
private static final Logger
logger
private SocketChannel
sc
private final Socket
sock
private Selector
selector
private boolean
closed
private ByteBuffer
bb
private byte[]
bs
private byte[]
b1
Constructors Summary
ASOutputStream(SocketChannel sc, Socket sock)


          
        this.sc = sc;
        this.sock = sock;
        selector = Selector.open();
        this.sc.register(selector, SelectionKey.OP_WRITE); 
    
Methods Summary
private voidcheckClosed()

        if (closed) {
            throw new IOException("Stream is closed");
        }
        
        if (sock.isOutputShutdown()) {
            throw new IOException("Output Shutdown");
        }
    
public voidclose()

        if (closed) {
            return;
        }
        closed = true;
        try {
            selector.close();
            selector = null;
            sc = null;
        } catch (Exception ie) {
            if ( logger.isLoggable(Level.FINE) ) {
                logger.log(Level.FINE, "" + ie.getMessage(), ie);
            }
        }
    
protected voidfinalize()

        try {
            close();
        } catch (Throwable t) {}
    
public voidflush()

        checkClosed();
    
private voidwaitForSelect()

        java.net.Socket sock = sc.socket();
        if (sock.isClosed()) {
            close();
            throw new IOException("Socket closed");
        }       
        Iterator it;
        SelectionKey selKey;
        
        selectorblock:
            while (true) {
                try {
                    selector.select();
                    if (sock.isOutputShutdown() || sock.isClosed()) {
                         throw new IOException("Output Shutdown");
                    }
                    it = selector.selectedKeys().iterator();
                    while (it.hasNext()) {
                        selKey = (SelectionKey)it.next();
                        if (selKey.isValid() && selKey.isWritable()) {
                            it.remove();
                            break selectorblock;
                        }
                    }
                } catch (Exception e) {
                    throw (IOException) (new IOException()).initCause(e);
                }
            }
    
public synchronized voidwrite(int b)

        if (b1 == null)
            b1 = new byte[1];
        b1[0] = (byte)b;
        this.write(b1);
    
public synchronized voidwrite(byte[] bs, int off, int len)

        checkClosed();
        if ((off < 0) || (off > bs.length) || (len < 0) ||
           ((off + len) > bs.length) || ((off + len) < 0)) {
            throw new IndexOutOfBoundsException();
        } else if (len == 0) {
            return;
        }
        ByteBuffer bb = ((this.bs == bs)
                        ? this.bb
                        : ByteBuffer.wrap(bs));
        bb.limit(Math.min(off + len, bb.capacity()));
        bb.position(off);
        this.bb = bb;
        this.bs = bs;
        waitForSelect();
        while (bb.hasRemaining()) {
            sc.write(bb);
        }