KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ChannelIO


1 /*
2  * @(#)ChannelIO.java 1.3 05/11/17
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 import java.io.*;
38 import java.nio.*;
39 import java.nio.channels.*;
40
41 /**
42  * A helper class for properly sizing inbound byte buffers and
43  * redirecting I/O calls to the proper SocketChannel call.
44  * <P>
45  * Many of these calls may seem unnecessary until you consider
46  * that they are placeholders for the secure variant, which is much
47  * more involved. See ChannelIOSecure for more information.
48  *
49  * @author Brad R. Wetmore
50  * @author Mark Reinhold
51  * @version 1.3, 05/11/17
52  */

53 class ChannelIO {
54
55     protected SocketChannel sc;
56
57     /*
58      * All of the inbound request data lives here until we determine
59      * that we've read everything, then we pass that data back to the
60      * caller.
61      */

62     protected ByteBuffer requestBB;
63     static private int requestBBSize = 4096;
64
65     protected ChannelIO(SocketChannel sc, boolean blocking)
66         throws IOException {
67     this.sc = sc;
68     sc.configureBlocking(blocking);
69     }
70
71     static ChannelIO getInstance(SocketChannel sc, boolean blocking)
72         throws IOException {
73     ChannelIO cio = new ChannelIO(sc, blocking);
74     cio.requestBB = ByteBuffer.allocate(requestBBSize);
75
76     return cio;
77     }
78
79     SocketChannel getSocketChannel() {
80     return sc;
81     }
82
83     /*
84      * Return a ByteBuffer with "remaining" space to work. If you have to
85      * reallocate the ByteBuffer, copy the existing info into the new buffer.
86      */

87     protected void resizeRequestBB(int remaining) {
88     if (requestBB.remaining() < remaining) {
89         // Expand buffer for large request
90
ByteBuffer bb = ByteBuffer.allocate(requestBB.capacity() * 2);
91         requestBB.flip();
92         bb.put(requestBB);
93         requestBB = bb;
94     }
95     }
96
97     /*
98      * Perform any handshaking processing.
99      * <P>
100      * This variant is for Servers without SelectionKeys (e.g.
101      * blocking).
102      * <P>
103      * return true when we're done with handshaking.
104      */

105     boolean doHandshake() throws IOException {
106     return true;
107     }
108
109     /*
110      * Perform any handshaking processing.
111      * <P>
112      * This variant is for Servers with SelectionKeys, so that
113      * we can register for selectable operations (e.g. selectable
114      * non-blocking).
115      * <P>
116      * return true when we're done with handshaking.
117      */

118     boolean doHandshake(SelectionKey sk) throws IOException {
119     return true;
120     }
121
122     /*
123      * Resize (if necessary) the inbound data buffer, and then read more
124      * data into the read buffer.
125      */

126     int read() throws IOException {
127     /*
128      * Allocate more space if less than 5% remains
129      */

130     resizeRequestBB(requestBBSize/20);
131     return sc.read(requestBB);
132     }
133
134     /*
135      * All data has been read, pass back the request in one buffer.
136      */

137     ByteBuffer getReadBuf() {
138     return requestBB;
139     }
140
141     /*
142      * Write the src buffer into the socket channel.
143      */

144     int write(ByteBuffer src) throws IOException {
145     return sc.write(src);
146     }
147
148     /*
149      * Perform a FileChannel.TransferTo on the socket channel.
150      */

151     long transferTo(FileChannel fc, long pos, long len) throws IOException {
152     return fc.transferTo(pos, len, sc);
153     }
154
155     /*
156      * Flush any outstanding data to the network if possible.
157      * <P>
158      * This isn't really necessary for the insecure variant, but needed
159      * for the secure one where intermediate buffering must take place.
160      * <P>
161      * Return true if successful.
162      */

163     boolean dataFlush() throws IOException {
164     return true;
165     }
166
167     /*
168      * Start any connection shutdown processing.
169      * <P>
170      * This isn't really necessary for the insecure variant, but needed
171      * for the secure one where intermediate buffering must take place.
172      * <P>
173      * Return true if successful, and the data has been flushed.
174      */

175     boolean shutdown() throws IOException {
176     return true;
177     }
178
179     /*
180      * Close the underlying connection.
181      */

182     void close() throws IOException {
183     sc.close();
184     }
185
186 }
187
Popular Tags