KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tomcat > util > net > NioChannel


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 package org.apache.tomcat.util.net;
20
21 import java.io.IOException JavaDoc;
22 import java.nio.ByteBuffer JavaDoc;
23 import java.nio.channels.ByteChannel JavaDoc;
24 import java.nio.channels.SocketChannel JavaDoc;
25
26 import org.apache.tomcat.util.net.NioEndpoint.Poller;
27 import org.apache.tomcat.util.net.SecureNioChannel.ApplicationBufferHandler;
28 import java.nio.channels.Selector JavaDoc;
29 import java.nio.channels.SelectionKey JavaDoc;
30
31 /**
32  *
33  * Base class for a SocketChannel wrapper used by the endpoint.
34  * This way, logic for a SSL socket channel remains the same as for
35  * a non SSL, making sure we don't need to code for any exception cases.
36  *
37  * @author Filip Hanik
38  * @version 1.0
39  */

40 public class NioChannel implements ByteChannel JavaDoc{
41
42     protected static ByteBuffer JavaDoc emptyBuf = ByteBuffer.allocate(0);
43
44     protected SocketChannel JavaDoc sc = null;
45
46     protected ApplicationBufferHandler bufHandler;
47
48     protected Poller poller;
49
50     public NioChannel(SocketChannel JavaDoc channel, ApplicationBufferHandler bufHandler) throws IOException JavaDoc {
51         this.sc = channel;
52         this.bufHandler = bufHandler;
53     }
54
55     public void reset() throws IOException JavaDoc {
56         bufHandler.getReadBuffer().clear();
57         bufHandler.getWriteBuffer().clear();
58     }
59     
60     public int getBufferSize() {
61         if ( bufHandler == null ) return 0;
62         int size = 0;
63         size += bufHandler.getReadBuffer()!=null?bufHandler.getReadBuffer().capacity():0;
64         size += bufHandler.getWriteBuffer()!=null?bufHandler.getWriteBuffer().capacity():0;
65         return size;
66     }
67
68     /**
69      * returns true if the network buffer has
70      * been flushed out and is empty
71      * @return boolean
72      */

73     public boolean flush(Selector JavaDoc s) throws IOException JavaDoc {
74         return true; //no network buffer in the regular channel
75
}
76
77
78     /**
79      * Closes this channel.
80      *
81      * @throws IOException If an I/O error occurs
82      * @todo Implement this java.nio.channels.Channel method
83      */

84     public void close() throws IOException JavaDoc {
85         getIOChannel().socket().close();
86         getIOChannel().close();
87     }
88
89     public void close(boolean force) throws IOException JavaDoc {
90         if (isOpen() || force ) close();
91     }
92     /**
93      * Tells whether or not this channel is open.
94      *
95      * @return <tt>true</tt> if, and only if, this channel is open
96      * @todo Implement this java.nio.channels.Channel method
97      */

98     public boolean isOpen() {
99         return sc.isOpen();
100     }
101
102     /**
103      * Writes a sequence of bytes to this channel from the given buffer.
104      *
105      * @param src The buffer from which bytes are to be retrieved
106      * @return The number of bytes written, possibly zero
107      * @throws IOException If some other I/O error occurs
108      * @todo Implement this java.nio.channels.WritableByteChannel method
109      */

110     public int write(ByteBuffer JavaDoc src) throws IOException JavaDoc {
111         return sc.write(src);
112     }
113
114     /**
115      * Reads a sequence of bytes from this channel into the given buffer.
116      *
117      * @param dst The buffer into which bytes are to be transferred
118      * @return The number of bytes read, possibly zero, or <tt>-1</tt> if the channel has reached end-of-stream
119      * @throws IOException If some other I/O error occurs
120      * @todo Implement this java.nio.channels.ReadableByteChannel method
121      */

122     public int read(ByteBuffer JavaDoc dst) throws IOException JavaDoc {
123         return sc.read(dst);
124     }
125
126     public Object JavaDoc getAttachment(boolean remove) {
127         Poller pol = getPoller();
128         Selector JavaDoc sel = pol!=null?pol.getSelector():null;
129         SelectionKey JavaDoc key = sel!=null?getIOChannel().keyFor(sel):null;
130         Object JavaDoc att = key!=null?key.attachment():null;
131         if (key != null && att != null && remove ) key.attach(null);
132         return att;
133     }
134     /**
135      * getBufHandler
136      *
137      * @return ApplicationBufferHandler
138      * @todo Implement this org.apache.tomcat.util.net.SecureNioChannel method
139      */

140     public ApplicationBufferHandler getBufHandler() {
141         return bufHandler;
142     }
143
144     public Poller getPoller() {
145         return poller;
146     }
147     /**
148      * getIOChannel
149      *
150      * @return SocketChannel
151      * @todo Implement this org.apache.tomcat.util.net.SecureNioChannel method
152      */

153     public SocketChannel JavaDoc getIOChannel() {
154         return sc;
155     }
156
157     /**
158      * isClosing
159      *
160      * @return boolean
161      * @todo Implement this org.apache.tomcat.util.net.SecureNioChannel method
162      */

163     public boolean isClosing() {
164         return false;
165     }
166
167     /**
168      * isInitHandshakeComplete
169      *
170      * @return boolean
171      * @todo Implement this org.apache.tomcat.util.net.SecureNioChannel method
172      */

173     public boolean isInitHandshakeComplete() {
174         return true;
175     }
176
177     public int handshake(boolean read, boolean write) throws IOException JavaDoc {
178         return 0;
179     }
180
181     public void setPoller(Poller poller) {
182         this.poller = poller;
183     }
184
185     public void setIOChannel(SocketChannel JavaDoc IOChannel) {
186         this.sc = IOChannel;
187     }
188
189     public String JavaDoc toString() {
190         return super.toString()+":"+this.sc.toString();
191     }
192
193 }
194
Popular Tags