1 23 package com.sun.enterprise.server.ss.provider; 24 25 import java.io.IOException ; 26 import java.io.OutputStream ; 27 import java.nio.ByteBuffer ; 28 import java.nio.channels.SelectionKey ; 29 import java.nio.channels.Selector ; 30 import java.nio.channels.SocketChannel ; 31 import java.net.Socket ; 32 import java.util.Iterator ; 33 import java.util.logging.Level ; 34 import java.util.logging.Logger ; 35 36 import com.sun.logging.LogDomains; 37 38 39 44 45 class ASOutputStream extends OutputStream { 46 47 private static Logger logger = LogDomains.getLogger(LogDomains.CORE_LOGGER); 48 49 private SocketChannel sc = null; 50 private Socket sock = null; 51 private Selector selector = null; 52 private boolean closed = false; 53 private ByteBuffer bb = null; 54 private byte[] bs = null; private byte[] b1 = null; 56 57 ASOutputStream(SocketChannel sc, Socket sock) throws IOException { 58 this.sc = sc; 59 this.sock = sock; 60 selector = Selector.open(); 61 this.sc.register(selector, SelectionKey.OP_WRITE); 62 } 63 64 public void close() throws IOException { 65 if (closed) { 66 return; 67 } 68 closed = true; 69 try { 70 selector.close(); 71 selector = null; 72 sc = null; 73 } catch (Exception ie) { 74 if ( logger.isLoggable(Level.FINE) ) { 75 logger.log(Level.FINE, "" + ie.getMessage(), ie); 76 } 77 } 78 } 79 80 public void flush() throws IOException { 81 checkClosed(); 82 } 83 84 private void waitForSelect() throws IOException { 85 java.net.Socket sock = sc.socket(); 86 if (sock.isClosed()) { 87 close(); 88 throw new IOException ("Socket closed"); 89 } 90 Iterator it; 91 SelectionKey selKey; 92 93 selectorblock: 94 while (true) { 95 try { 96 selector.select(); 97 if (sock.isOutputShutdown() || sock.isClosed()) { 98 throw new IOException ("Output Shutdown"); 99 } 100 it = selector.selectedKeys().iterator(); 101 while (it.hasNext()) { 102 selKey = (SelectionKey )it.next(); 103 if (selKey.isValid() && selKey.isWritable()) { 104 it.remove(); 105 break selectorblock; 106 } 107 } 108 } catch (Exception e) { 109 throw (IOException ) (new IOException ()).initCause(e); 110 } 111 } 112 } 113 114 public synchronized void write(int b) throws IOException { 115 if (b1 == null) 116 b1 = new byte[1]; 117 b1[0] = (byte)b; 118 this.write(b1); 119 } 120 121 public synchronized void write(byte[] bs, int off, int len) throws IOException { 122 checkClosed(); 123 if ((off < 0) || (off > bs.length) || (len < 0) || 124 ((off + len) > bs.length) || ((off + len) < 0)) { 125 throw new IndexOutOfBoundsException (); 126 } else if (len == 0) { 127 return; 128 } 129 ByteBuffer bb = ((this.bs == bs) 130 ? this.bb 131 : ByteBuffer.wrap(bs)); 132 bb.limit(Math.min(off + len, bb.capacity())); 133 bb.position(off); 134 this.bb = bb; 135 this.bs = bs; 136 waitForSelect(); 137 while (bb.hasRemaining()) { 138 sc.write(bb); 139 } 140 } 141 142 protected void finalize() throws Throwable { 143 try { 144 close(); 145 } catch (Throwable t) {} 146 } 147 148 private void checkClosed() throws IOException { 149 if (closed) { 150 throw new IOException ("Stream is closed"); 151 } 152 153 if (sock.isOutputShutdown()) { 154 throw new IOException ("Output Shutdown"); 155 } 156 } 157 } 158 159 | Popular Tags |