Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
InputReader |
|
| 0.0;0 | ||||
InputReader$ChannelType |
|
| 0.0;0 |
1 | /* | |
2 | * | |
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
4 | * | |
5 | * Copyright 2007-2008 Sun Microsystems, Inc. All rights reserved. | |
6 | * | |
7 | * The contents of this file are subject to the terms of either the GNU | |
8 | * General Public License Version 2 only ("GPL") or the Common Development | |
9 | * and Distribution License("CDDL") (collectively, the "License"). You | |
10 | * may not use this file except in compliance with the License. You can obtain | |
11 | * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html | |
12 | * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific | |
13 | * language governing permissions and limitations under the License. | |
14 | * | |
15 | * When distributing the software, include this License Header Notice in each | |
16 | * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt. | |
17 | * Sun designates this particular file as subject to the "Classpath" exception | |
18 | * as provided by Sun in the GPL Version 2 section of the License file that | |
19 | * accompanied this code. If applicable, add the following below the License | |
20 | * Header, with the fields enclosed by brackets [] replaced by your own | |
21 | * identifying information: "Portions Copyrighted [year] | |
22 | * [name of copyright owner]" | |
23 | * | |
24 | * Contributor(s): | |
25 | * | |
26 | * If you wish your version of this file to be governed by only the CDDL or | |
27 | * only the GPL Version 2, indicate your decision by adding "[Contributor] | |
28 | * elects to include this software in this distribution under the [CDDL or GPL | |
29 | * Version 2] license." If you don't indicate a single choice of license, a | |
30 | * recipient has the option to distribute your version of this file under | |
31 | * either the CDDL, the GPL Version 2 or to extend the choice of license to | |
32 | * its licensees as provided above. However, if you add GPL Version 2 code | |
33 | * and therefore, elected the GPL Version 2 license, then the option applies | |
34 | * only if the new code is made subject to such option by the copyright | |
35 | * holder. | |
36 | * | |
37 | */ | |
38 | package com.sun.grizzly.util; | |
39 | ||
40 | import java.io.InputStream; | |
41 | import java.io.IOException; | |
42 | import java.nio.ByteBuffer; | |
43 | import java.nio.channels.SelectionKey; | |
44 | import java.nio.channels.SocketChannel; | |
45 | ||
46 | /** | |
47 | * This class implement IO stream operations on top of a {@link ByteBuffer}. | |
48 | * Under the hood, this class use a temporary Selector pool for reading | |
49 | * bytes when the client ask for more and the current Selector is not yet ready. | |
50 | * | |
51 | * @author Jeanfrancois Arcand | |
52 | */ | |
53 | public class InputReader extends InputStream { | |
54 | ||
55 | /** | |
56 | * The {@link Channel} type is used to avoid invoking the instanceof | |
57 | * operation when registering the Socket|Datagram Channel to the Selector. | |
58 | */ | |
59 | 3 | public enum ChannelType { SocketChannel, DatagramChannel } |
60 | ||
61 | ||
62 | /** | |
63 | * By default this class will cast the Channel to a SocketChannel. | |
64 | */ | |
65 | 52 | private ChannelType defaultChannelType = ChannelType.SocketChannel; |
66 | ||
67 | ||
68 | 1 | private static int defaultReadTimeout = 30000; |
69 | ||
70 | /** | |
71 | * The wrapped <code>ByteBuffer</code< | |
72 | */ | |
73 | protected ByteBuffer byteBuffer; | |
74 | ||
75 | ||
76 | /** | |
77 | * The {@link SelectionKey} used by this stream. | |
78 | */ | |
79 | 52 | protected SelectionKey key = null; |
80 | ||
81 | ||
82 | /** | |
83 | * The time to wait before timing out when reading bytes | |
84 | */ | |
85 | 52 | protected int readTimeout = defaultReadTimeout; |
86 | ||
87 | ||
88 | /** | |
89 | * Is the stream secure. | |
90 | */ | |
91 | 52 | private boolean secure = false; |
92 | ||
93 | ||
94 | // ------------------------------------------------- Constructor -------// | |
95 | ||
96 | ||
97 | 52 | public InputReader () { |
98 | 52 | } |
99 | ||
100 | ||
101 | 0 | public InputReader (final ByteBuffer byteBuffer) { |
102 | 0 | this.byteBuffer = byteBuffer; |
103 | 0 | } |
104 | ||
105 | // ---------------------------------------------------------------------// | |
106 | ||
107 | ||
108 | /** | |
109 | * Set the wrapped {@link ByteBuffer} | |
110 | * @param byteBuffer The wrapped byteBuffer | |
111 | */ | |
112 | public void setByteBuffer(final ByteBuffer byteBuffer) { | |
113 | 0 | this.byteBuffer = byteBuffer; |
114 | 0 | } |
115 | ||
116 | ||
117 | /** | |
118 | * Get the wrapped {@link ByteBuffer} | |
119 | * @return {@link ByteBuffer} | |
120 | */ | |
121 | public ByteBuffer getByteBuffer() { | |
122 | 0 | return byteBuffer; |
123 | } | |
124 | ||
125 | ||
126 | /** | |
127 | * Return the available bytes | |
128 | * @return the wrapped byteBuffer.remaining() | |
129 | */ | |
130 | @Override | |
131 | public int available () { | |
132 | 0 | return (byteBuffer.remaining()); |
133 | } | |
134 | ||
135 | ||
136 | /** | |
137 | * Close this stream. | |
138 | */ | |
139 | @Override | |
140 | public void close () { | |
141 | 0 | } |
142 | ||
143 | ||
144 | /** | |
145 | * Return true if mark is supported. | |
146 | */ | |
147 | @Override | |
148 | public boolean markSupported() { | |
149 | 0 | return false; |
150 | } | |
151 | ||
152 | ||
153 | /** | |
154 | * Read the first byte from the wrapped {@link ByteBuffer}. | |
155 | */ | |
156 | @Override | |
157 | public int read() throws IOException { | |
158 | 0 | if (!byteBuffer.hasRemaining()){ |
159 | 0 | byteBuffer.clear(); |
160 | 0 | int eof = doRead(); |
161 | ||
162 | 0 | if (eof <= 0){ |
163 | 0 | return -1; |
164 | } | |
165 | } | |
166 | 0 | return (byteBuffer.hasRemaining() ? (byteBuffer.get () & 0xff): -1); |
167 | } | |
168 | ||
169 | ||
170 | /** | |
171 | * Read the bytes from the wrapped {@link ByteBuffer}. | |
172 | */ | |
173 | @Override | |
174 | public int read(byte[] b) throws IOException { | |
175 | 0 | return (read (b, 0, b.length)); |
176 | } | |
177 | ||
178 | ||
179 | /** | |
180 | * Read the first byte of the wrapped {@link ByteBuffer}. | |
181 | * @param offset | |
182 | * @param length | |
183 | */ | |
184 | @Override | |
185 | public int read(byte[] b, int offset, int length) throws IOException { | |
186 | 0 | if (!byteBuffer.hasRemaining()) { |
187 | 0 | byteBuffer.clear(); |
188 | 0 | int eof = doRead(); |
189 | ||
190 | 0 | if (eof <= 0){ |
191 | 0 | return -1; |
192 | } | |
193 | } | |
194 | ||
195 | 0 | if (length > byteBuffer.remaining()) { |
196 | 0 | length = byteBuffer.remaining(); |
197 | } | |
198 | 0 | byteBuffer.get(b, offset, length); |
199 | ||
200 | 0 | return (length); |
201 | } | |
202 | ||
203 | ||
204 | /** | |
205 | * Read the bytes of the wrapped {@link ByteBuffer}. | |
206 | * @param bb {@link ByteBuffer} | |
207 | * @return - number of bytes read | |
208 | * @throws java.io.IOException | |
209 | */ | |
210 | public int read(ByteBuffer bb) throws IOException { | |
211 | //Switch Buffer | |
212 | 312 | ByteBuffer oldBB = byteBuffer; |
213 | 312 | byteBuffer = bb; |
214 | 312 | int initialPosition = bb.position(); |
215 | 312 | int eof = doRead(); |
216 | ||
217 | 311 | if (eof <= 0){ |
218 | 1 | return -1; |
219 | } | |
220 | // Back to the default one. | |
221 | 310 | byteBuffer = oldBB; |
222 | ||
223 | // Calculate the number of bytes were read | |
224 | 310 | int bytesRead = bb.limit() - initialPosition; |
225 | 310 | return bytesRead; |
226 | } | |
227 | ||
228 | ||
229 | /** | |
230 | * Recycle this object. | |
231 | */ | |
232 | public void recycle(){ | |
233 | 0 | byteBuffer = null; |
234 | 0 | key = null; |
235 | 0 | } |
236 | ||
237 | ||
238 | /** | |
239 | * Set the {@link SelectionKey} used to reads bytes. | |
240 | * @param key {@link SelectionKey} | |
241 | */ | |
242 | public void setSelectionKey(SelectionKey key){ | |
243 | 312 | this.key = key; |
244 | 312 | } |
245 | ||
246 | ||
247 | /** | |
248 | * Read bytes using the read <code>ReadSelector</code> | |
249 | * @return - number of bytes read | |
250 | * @throws java.io.IOException | |
251 | */ | |
252 | protected int doRead() throws IOException{ | |
253 | 312 | if ( key == null ) return -1; |
254 | ||
255 | 312 | if (secure){ |
256 | 0 | return doSecureRead(); |
257 | } else { | |
258 | 312 | return doClearRead(); |
259 | } | |
260 | } | |
261 | ||
262 | ||
263 | /** | |
264 | * Read and decrypt bytes from the underlying SSL connections. All | |
265 | * the SSLEngine operations are delegated to class {@link SSLUtils}. | |
266 | * @return number of bytes read | |
267 | * @throws java.io.IOException | |
268 | */ | |
269 | protected int doSecureRead() throws IOException{ | |
270 | 0 | final WorkerThread workerThread = |
271 | (WorkerThread)Thread.currentThread(); | |
272 | ||
273 | 0 | int bytesRead = SSLUtils.doSecureRead((SocketChannel) key.channel(), |
274 | workerThread.getSSLEngine(), byteBuffer, | |
275 | workerThread.getInputBB()); | |
276 | 0 | byteBuffer.flip(); |
277 | ||
278 | 0 | return bytesRead; |
279 | } | |
280 | ||
281 | ||
282 | protected int doClearRead() throws IOException{ | |
283 | 312 | int bytesRead = Utils.readWithTemporarySelector(key.channel(), |
284 | byteBuffer, readTimeout); | |
285 | 311 | byteBuffer.flip(); |
286 | ||
287 | 311 | return bytesRead; |
288 | } | |
289 | ||
290 | ||
291 | /** | |
292 | * Return the timeout between two consecutives Selector.select() when a | |
293 | * temporary Selector is used. | |
294 | * @return read timeout being used | |
295 | */ | |
296 | public int getReadTimeout() { | |
297 | 0 | return readTimeout; |
298 | } | |
299 | ||
300 | ||
301 | /** | |
302 | * Set the timeout between two consecutives Selector.select() when a | |
303 | * temporary Selector is used. | |
304 | * @param rt - read timeout | |
305 | */ | |
306 | public void setReadTimeout(int rt) { | |
307 | 0 | readTimeout = rt; |
308 | 0 | } |
309 | ||
310 | ||
311 | /** | |
312 | * Return the Selector.select() default time out. | |
313 | * @return default time out | |
314 | */ | |
315 | public static int getDefaultReadTimeout() { | |
316 | 0 | return defaultReadTimeout; |
317 | } | |
318 | ||
319 | ||
320 | /** | |
321 | * Set the default Selector.select() time out. | |
322 | * @param aDefaultReadTimeout time out value | |
323 | */ | |
324 | public static void setDefaultReadTimeout(int aDefaultReadTimeout) { | |
325 | 0 | defaultReadTimeout = aDefaultReadTimeout; |
326 | 0 | } |
327 | ||
328 | ||
329 | /** | |
330 | * Return the {@link Channel} type. The return value is SocketChannel | |
331 | * or DatagramChannel. | |
332 | * @return {@link Channel} being used | |
333 | */ | |
334 | public ChannelType getChannelType() { | |
335 | 0 | return defaultChannelType; |
336 | } | |
337 | ||
338 | ||
339 | /** | |
340 | * Set the {@link Channel} type, which is ocketChannel | |
341 | * or DatagramChannel. | |
342 | * @param channelType {@link Channel} to use | |
343 | */ | |
344 | public void setChannelType(ChannelType channelType) { | |
345 | 0 | this.defaultChannelType = channelType; |
346 | 0 | } |
347 | ||
348 | ||
349 | /** | |
350 | * Is this Stream secure. | |
351 | * @return true is stream is secure, otherwise false | |
352 | */ | |
353 | public boolean isSecure() { | |
354 | 0 | return secure; |
355 | } | |
356 | ||
357 | ||
358 | /** | |
359 | * Set this stream secure. | |
360 | * @param secure true to set stream secure, otherwise false | |
361 | */ | |
362 | public void setSecure(boolean secure) { | |
363 | 0 | this.secure = secure; |
364 | 0 | } |
365 | } | |
366 |