KickJava   Java API By Example, From Geeks To Geeks.

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


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.OutputStream 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  * OutputStream 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 ASOutputStream extends OutputStream 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 Selector JavaDoc selector = null;
52     private boolean closed = false;
53     private ByteBuffer JavaDoc bb = null;
54     private byte[] bs = null; // Invoker's previous array
55
private byte[] b1 = null;
56
57     ASOutputStream(SocketChannel JavaDoc sc, Socket JavaDoc sock) throws IOException JavaDoc {
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 JavaDoc {
65         if (closed) {
66             return;
67         }
68         closed = true;
69         try {
70             selector.close();
71             selector = null;
72             sc = null;
73         } catch (Exception JavaDoc 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 JavaDoc {
81         checkClosed();
82     }
83
84     private void waitForSelect() throws IOException JavaDoc {
85         java.net.Socket JavaDoc sock = sc.socket();
86         if (sock.isClosed()) {
87             close();
88             throw new IOException JavaDoc("Socket closed");
89         }
90         Iterator JavaDoc it;
91         SelectionKey JavaDoc selKey;
92         
93         selectorblock:
94             while (true) {
95                 try {
96                     selector.select();
97                     if (sock.isOutputShutdown() || sock.isClosed()) {
98                          throw new IOException JavaDoc("Output Shutdown");
99                     }
100                     it = selector.selectedKeys().iterator();
101                     while (it.hasNext()) {
102                         selKey = (SelectionKey JavaDoc)it.next();
103                         if (selKey.isValid() && selKey.isWritable()) {
104                             it.remove();
105                             break selectorblock;
106                         }
107                     }
108                 } catch (Exception JavaDoc e) {
109                     throw (IOException JavaDoc) (new IOException JavaDoc()).initCause(e);
110                 }
111             }
112     }
113
114     public synchronized void write(int b) throws IOException JavaDoc {
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 JavaDoc {
122         checkClosed();
123         if ((off < 0) || (off > bs.length) || (len < 0) ||
124            ((off + len) > bs.length) || ((off + len) < 0)) {
125             throw new IndexOutOfBoundsException JavaDoc();
126         } else if (len == 0) {
127             return;
128         }
129         ByteBuffer JavaDoc 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 JavaDoc {
143         try {
144             close();
145         } catch (Throwable JavaDoc t) {}
146     }
147
148     private void checkClosed() throws IOException JavaDoc {
149         if (closed) {
150             throw new IOException JavaDoc("Stream is closed");
151         }
152         
153         if (sock.isOutputShutdown()) {
154             throw new IOException JavaDoc("Output Shutdown");
155         }
156     }
157 }
158
159
Popular Tags