1 23 package com.sun.enterprise.server.ss.provider; 24 25 import java.io.IOException ; 26 import java.io.InputStream ; 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 ASInputStream extends InputStream { 46 47 private static Logger logger = LogDomains.getLogger(LogDomains.CORE_LOGGER); 48 49 private SocketChannel sc = null; 50 private Socket sock = null; 51 private boolean closed = false; 52 private Selector selector = null; 53 54 private ByteBuffer bb = null; 55 private byte[] bs = null; private byte[] b1 = null; 57 58 ASInputStream(SocketChannel sc, Socket sock) throws IOException { 59 this.sc = sc; 60 this.sock = sock; 61 this.selector = Selector.open(); 62 this.sc.register(selector, SelectionKey.OP_READ); 63 } 64 65 66 public synchronized int read() throws IOException { 67 if (b1 == null) 68 b1 = new byte[1]; 69 int n = this.read(b1); 70 if (n == 1) 71 return b1[0] & 0xff; 72 return -1; 73 } 74 75 public synchronized int read(byte[] bs, int off, int len) 76 throws IOException 77 { 78 if ((off < 0) || (off > bs.length) || (len < 0) || 79 ((off + len) > bs.length) || ((off + len) < 0)) { 80 throw new IndexOutOfBoundsException (); 81 } else if (len == 0) 82 return 0; 83 84 ByteBuffer bb = ((this.bs == bs) 85 ? this.bb 86 : ByteBuffer.wrap(bs)); 87 bb.position(off); 88 bb.limit(Math.min(off + len, bb.capacity())); 89 this.bb = bb; 90 this.bs = bs; 91 return read(bb); 92 } 93 94 95 private int read(ByteBuffer bb) 96 throws IOException 97 { 98 checkClosed(); 99 waitForSelect(); 100 return sc.read(bb); 101 } 102 103 private void waitForSelect() throws IOException { 104 105 java.net.Socket sock = sc.socket(); 106 if (sock.isClosed()) { 107 close(); 108 throw new IOException ("Socket Closed"); 109 } 110 111 int timeout = sock.getSoTimeout(); 112 Iterator it; 113 SelectionKey selKey; 114 115 selectorblock: 116 while (true) { 117 boolean timedout = true; 118 try { 119 int n = selector.select(timeout); 120 if (sock.isInputShutdown() || sock.isClosed()) { 121 throw new IOException ("Input Shutdown"); 122 } 123 if (n > 0) { 124 timedout = false; 125 } 126 it = selector.selectedKeys().iterator(); 127 while (it.hasNext()) { 128 timedout = false; 129 selKey = (SelectionKey )it.next(); 130 if (selKey.isValid() && selKey.isReadable()) { 131 it.remove(); 132 break selectorblock; 133 } 134 } 135 } catch (Exception e) { 136 throw (IOException ) (new IOException ()).initCause(e); 137 } 138 if (timedout) { 139 boolean wakenup = ((ASSelector) selector).wakenUp(); 140 if ( !wakenup && !Thread.currentThread().isInterrupted()) { 141 throw new java.net.SocketTimeoutException ("Read timed out"); 142 } 143 } 144 } 145 } 146 147 148 public void close() throws IOException { 149 if (closed) { 150 return; 151 } 152 closed = true; 153 try { 154 selector.close(); 155 selector = null; 156 sc = null; 157 } catch (Exception ie) { 158 if ( logger.isLoggable(Level.FINE) ) { 159 logger.log(Level.FINE, "" + ie.getMessage(), ie); 160 } 161 } 162 } 163 164 protected void finalize() throws Throwable { 165 try { 166 close(); 167 } catch (Throwable t) {} 168 } 169 170 private void checkClosed() throws IOException { 171 if (closed) { 172 throw new IOException ("Stream is closed"); 173 } 174 175 if (sock.isInputShutdown()) { 176 throw new IOException ("Input Shutdown"); 177 } 178 } 179 180 } 181 182 | Popular Tags |