1 2 24 package com.sun.enterprise.web.connector.grizzly; 25 26 import java.io.InputStream ; 27 import java.io.IOException ; 28 import java.nio.ByteBuffer ; 29 import java.nio.channels.SelectionKey ; 30 import java.nio.channels.Selector ; 31 import java.nio.channels.SocketChannel ; 32 33 36 public class ByteBufferInputStream extends InputStream { 37 38 41 private ByteBuffer byteBuffer; 42 43 44 47 private SelectionKey key = null; 48 49 50 53 private int readTimeout = 15000; 54 55 56 59 protected int readTry = 2; 60 61 63 64 public ByteBufferInputStream () { 65 } 66 67 68 public ByteBufferInputStream (final ByteBuffer byteBuffer) { 69 this.byteBuffer = byteBuffer; 70 } 71 72 74 75 79 public void setByteBuffer (final ByteBuffer byteBuffer) { 80 this.byteBuffer = byteBuffer; 81 } 82 83 84 88 public int available () { 89 return (byteBuffer.remaining()); 90 } 91 92 93 96 public void close () { 97 } 98 99 100 103 public boolean markSupported () { 104 return (true); 105 } 106 107 108 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 128 public int read (byte[] b) { 129 return (read (b, 0, b.length)); 130 } 131 132 133 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 164 public void recycle(){ 165 byteBuffer = null; 166 } 167 168 169 172 public void setSelectionKey(SelectionKey key){ 173 this.key = key; 174 } 175 176 177 180 private int doRead(){ 181 if ( key == null ) return -1; 182 183 byteBuffer.clear(); 184 int count = 1; 185 int byteRead = 0; 186 Selector readSelector = null; 187 SelectionKey tmpKey = null; 188 189 try{ 190 SocketChannel socketChannel = (SocketChannel )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; } 213 214 while (count > 0){ 215 count = socketChannel.read(byteBuffer); 216 byteRead += count; 217 } 218 } 219 } catch (Throwable t){ 220 return -1; 221 } finally { 222 if (tmpKey != null) 223 tmpKey.cancel(); 224 225 if ( readSelector != null){ 226 try{ 228 readSelector.selectNow(); 229 } catch (IOException ex){ 230 ; 231 } 232 SelectorFactory.returnSelector(readSelector); 233 } 234 } 235 byteBuffer.flip(); 236 return byteRead; 237 } 238 } 239 240 | Popular Tags |