KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > server > ss > provider > ASInputStream


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.server.ss.provider;
24
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.nio.ByteBuffer JavaDoc;
28 import java.nio.channels.SelectionKey JavaDoc;
29 import java.nio.channels.Selector JavaDoc;
30 import java.nio.channels.SocketChannel JavaDoc;
31 import java.net.Socket JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.logging.Level JavaDoc;
34 import java.util.logging.Logger JavaDoc;
35
36 import com.sun.logging.LogDomains;
37
38
39 /**
40  * InputStream implementation for the socket wrappers of Quick startup
41  * implementation. Implementation is thread safe while using nio to read
42  * and write simultaneusly in the non-blocking mode.
43  */

44
45 class ASInputStream extends InputStream JavaDoc {
46
47     private static Logger JavaDoc logger = LogDomains.getLogger(LogDomains.CORE_LOGGER);
48
49     private SocketChannel JavaDoc sc = null;
50     private Socket JavaDoc sock = null;
51     private boolean closed = false;
52     private Selector JavaDoc selector = null;
53
54     private ByteBuffer JavaDoc bb = null;
55     private byte[] bs = null; // Invoker's previous array
56
private byte[] b1 = null;
57
58     ASInputStream(SocketChannel JavaDoc sc, Socket JavaDoc sock) throws IOException JavaDoc{
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 JavaDoc {
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 JavaDoc
77     {
78     if ((off < 0) || (off > bs.length) || (len < 0) ||
79         ((off + len) > bs.length) || ((off + len) < 0)) {
80         throw new IndexOutOfBoundsException JavaDoc();
81     } else if (len == 0)
82         return 0;
83
84     ByteBuffer JavaDoc 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 JavaDoc bb)
96     throws IOException JavaDoc
97     {
98         checkClosed();
99         waitForSelect();
100         return sc.read(bb);
101     }
102
103     private void waitForSelect() throws IOException JavaDoc {
104         
105         java.net.Socket JavaDoc sock = sc.socket();
106         if (sock.isClosed()) {
107             close();
108             throw new IOException JavaDoc("Socket Closed");
109         }
110
111         int timeout = sock.getSoTimeout();
112         Iterator JavaDoc it;
113         SelectionKey JavaDoc 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 JavaDoc("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 JavaDoc)it.next();
130                         if (selKey.isValid() && selKey.isReadable()) {
131                             it.remove();
132                             break selectorblock;
133                         }
134                     }
135                 } catch (Exception JavaDoc e) {
136                     throw (IOException JavaDoc) (new IOException JavaDoc()).initCause(e);
137                 }
138                 if (timedout) {
139                      boolean wakenup = ((ASSelector) selector).wakenUp();
140                      if ( !wakenup && !Thread.currentThread().isInterrupted()) {
141                         throw new java.net.SocketTimeoutException JavaDoc("Read timed out");
142                      }
143                 }
144             }
145     }
146
147
148     public void close() throws IOException JavaDoc {
149         if (closed) {
150             return;
151         }
152         closed = true;
153         try {
154             selector.close();
155             selector = null;
156             sc = null;
157         } catch (Exception JavaDoc 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 JavaDoc {
165         try {
166             close();
167         } catch (Throwable JavaDoc t) {}
168     }
169
170     private void checkClosed() throws IOException JavaDoc {
171         if (closed) {
172             throw new IOException JavaDoc("Stream is closed");
173         }
174
175         if (sock.isInputShutdown()) {
176             throw new IOException JavaDoc("Input Shutdown");
177         }
178     }
179
180 }
181
182
Popular Tags