KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > web > connector > grizzly > ByteBufferInputStream


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

24 package com.sun.enterprise.web.connector.grizzly;
25
26 import java.io.InputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.nio.ByteBuffer JavaDoc;
29 import java.nio.channels.SelectionKey JavaDoc;
30 import java.nio.channels.Selector JavaDoc;
31 import java.nio.channels.SocketChannel JavaDoc;
32
33 /**
34  * Stream wrapper around a <code>ByteBuffer</code>
35  */

36 public class ByteBufferInputStream extends InputStream JavaDoc {
37
38     /**
39      * The wrapped <code>ByteBuffer</code<
40      */

41     private ByteBuffer JavaDoc byteBuffer;
42
43     
44     /**
45      * The <code>SelectionKey</code> used by this stream.
46      */

47     private SelectionKey JavaDoc key = null;
48     
49     
50     /**
51      * The time to wait before timing out when reading bytes
52      */

53     private int readTimeout = 15000;
54     
55     
56     /**
57      * Number of times to retry before return EOF
58      */

59     protected int readTry = 2;
60     
61     // ------------------------------------------------- Constructor -------//
62

63     
64     public ByteBufferInputStream () {
65     }
66
67     
68     public ByteBufferInputStream (final ByteBuffer JavaDoc byteBuffer) {
69         this.byteBuffer = byteBuffer;
70     }
71
72     // ---------------------------------------------------------------------//
73

74     
75     /**
76      * Set the wrapped <code>ByteBuffer</code>
77      * @param byteBuffer The wrapped byteBuffer
78      */

79     public void setByteBuffer (final ByteBuffer JavaDoc byteBuffer) {
80         this.byteBuffer = byteBuffer;
81     }
82
83     
84     /**
85      * Return the available bytes
86      * @return the wrapped byteBuffer.remaining()
87      */

88     public int available () {
89         return (byteBuffer.remaining());
90     }
91
92     
93     /**
94      * Close this stream.
95      */

96     public void close () {
97     }
98
99     
100     /**
101      * Return true if mark is supported.
102      */

103     public boolean markSupported () {
104         return (true);
105     }
106
107     
108     /**
109      * Read the first byte from the wrapped <code>ByteBuffer</code>.
110      */

111     public int read () {
112         if (!byteBuffer.hasRemaining()){
113             int eof = 0;
114             for (int i=0; i < readTry; i++) {
115                 eof = doRead();
116                 if ( eof != 0 ){
117                     break;
118                 }
119             }
120         }
121         return (byteBuffer.hasRemaining() ? (byteBuffer.get () & 0xff): -1);
122      }
123
124     
125     /**
126      * Read the bytes from the wrapped <code>ByteBuffer</code>.
127      */

128     public int read (byte[] b) {
129         return (read (b, 0, b.length));
130     }
131
132     
133     /**
134      * Read the first byte of the wrapped <code>ByteBuffer</code>.
135      */

136     public int read (byte[] b, int offset, int length) {
137         if (!byteBuffer.hasRemaining()) {
138             int eof = 0;
139             for (int i=0; i < readTry; i++) {
140                 eof = doRead();
141                 
142                 if ( eof != 0 ){
143                     break;
144                 }
145             }
146             
147             if (eof <= 0){
148                 return -1;
149             }
150         }
151  
152         if (length > byteBuffer.remaining()) {
153             length = byteBuffer.remaining();
154         }
155         byteBuffer.get(b, offset, length);
156          
157         return (length);
158     }
159     
160     
161     /**
162      * Recycle this object.
163      */

164     public void recycle(){
165         byteBuffer = null;
166     }
167     
168     
169     /**
170      * Set the <code>SelectionKey</code> used to reads bytes.
171      */

172     public void setSelectionKey(SelectionKey JavaDoc key){
173         this.key = key;
174     }
175     
176     
177     /**
178      * Read bytes using the read <code>ReadSelector</code>
179      */

180     private int doRead(){
181         if ( key == null ) return -1;
182         
183         byteBuffer.clear();
184         int count = 1;
185         int byteRead = 0;
186         Selector JavaDoc readSelector = null;
187         SelectionKey JavaDoc tmpKey = null;
188         
189         try{
190             SocketChannel JavaDoc socketChannel = (SocketChannel JavaDoc)key.channel();
191             while (count > 0){
192                 count = socketChannel.read(byteBuffer);
193                 byteRead += count;
194             }
195             
196             if ( byteRead == 0 ){
197                 readSelector = SelectorFactory.getSelector();
198
199                 if ( readSelector == null ){
200                     return 0;
201                 }
202                 count = 1;
203                 tmpKey = socketChannel
204                         .register(readSelector,SelectionKey.OP_READ);
205                 tmpKey.interestOps(tmpKey.interestOps() | SelectionKey.OP_READ);
206                 int code = readSelector.select(readTimeout);
207                 tmpKey.interestOps(
208                     tmpKey.interestOps() & (~SelectionKey.OP_READ));
209
210                 if ( code == 0 ){
211                     return 0; // Return on the main Selector and try again.
212
}
213
214                 while (count > 0){
215                     count = socketChannel.read(byteBuffer);
216                     byteRead += count;
217                 }
218             }
219         } catch (Throwable JavaDoc t){
220             return -1;
221         } finally {
222             if (tmpKey != null)
223                 tmpKey.cancel();
224
225             if ( readSelector != null){
226                 // Bug 6403933
227
try{
228                     readSelector.selectNow();
229                 } catch (IOException JavaDoc ex){
230                     ;
231                 }
232                 SelectorFactory.returnSelector(readSelector);
233             }
234         }
235         byteBuffer.flip();
236         return byteRead;
237     }
238 }
239
240
Popular Tags