1 23 24 package com.sun.enterprise.web.connector.grizzly.algorithms; 25 26 import com.sun.enterprise.web.connector.grizzly.Constants; 27 import com.sun.enterprise.web.connector.grizzly.Handler; 28 import com.sun.enterprise.web.connector.grizzly.StreamAlgorithm; 29 import com.sun.enterprise.web.connector.grizzly.ByteBufferFactory; 30 31 import java.nio.ByteBuffer ; 32 import java.nio.channels.SocketChannel ; 33 34 35 39 public abstract class StreamAlgorithmBase implements StreamAlgorithm{ 40 41 private int port = 8080; 42 43 48 public abstract boolean parse(ByteBuffer byteBuffer); 49 50 51 54 protected int contentLength = -1; 55 56 57 60 protected int curLimit = -1; 61 62 63 66 protected int curPosition = -1; 67 68 69 73 protected int headerLength = -1; 74 75 76 80 protected int lastStatePosition = -1; 81 82 83 86 protected int state = 0; 87 88 89 94 protected ByteBuffer primaryByteBuffer = null; 95 96 97 101 protected boolean useByteBufferView = false; 102 103 104 107 protected boolean useDirectByteBuffer; 108 109 110 113 protected SocketChannel socketChannel; 114 115 116 120 protected Handler handler; 121 122 123 125 126 130 public int contentLength(){ 131 return contentLength; 132 } 133 134 135 139 public int headerLength(){ 140 return headerLength; 141 } 142 143 144 150 public ByteBuffer allocate(boolean useDirect, boolean useView){ 151 useByteBufferView = useView; 152 useDirectByteBuffer = useDirect; 153 154 return allocateByteBuffer(useDirect,useView); 155 } 156 157 158 163 public ByteBuffer preParse(ByteBuffer byteBuffer){ 164 if (byteBuffer.position() == byteBuffer.capacity()){ 165 int bufferSize = contentLength > 0 ? 167 contentLength + headerLength + 5: 168 byteBuffer.capacity() * 2; 169 byteBuffer = swapBuffer(byteBuffer,bufferSize); 170 } 171 return byteBuffer; 172 } 173 174 179 public ByteBuffer postParse(ByteBuffer byteBuffer){ 180 if ( primaryByteBuffer != null) { 182 primaryByteBuffer.clear(); 183 byteBuffer = primaryByteBuffer; 184 primaryByteBuffer = null; 185 } 186 return byteBuffer; 187 } 188 189 190 193 public void recycle(){ 194 contentLength = -1; 195 lastStatePosition= -1; 196 headerLength = -1; 197 curLimit = -1; 198 curPosition = -1; 199 state = 0; 200 } 201 202 203 205 206 213 protected ByteBuffer allocateByteBuffer(boolean useDirectByteBuffer, 214 boolean useByteBufferView){ 215 216 return allocateByteBuffer(useDirectByteBuffer, 217 useByteBufferView, 218 Constants.CHANNEL_BYTE_SIZE); 219 } 220 221 222 229 protected ByteBuffer allocateByteBuffer(boolean useDirectByteBuffer, 230 boolean useByteBufferView, 231 int size){ 232 ByteBuffer byteBuffer; 233 if (useByteBufferView){ 234 byteBuffer = ByteBufferFactory.allocateView(size, 235 useDirectByteBuffer); 236 } else if ( useDirectByteBuffer ){ 237 byteBuffer = ByteBuffer.allocateDirect(size); 238 } else { 239 byteBuffer = ByteBuffer.allocate(size); 240 } 241 return byteBuffer; 242 } 243 244 245 249 private ByteBuffer swapBuffer(ByteBuffer byteBuffer, int size){ 250 ByteBuffer tmp = allocateByteBuffer(useDirectByteBuffer, 251 useByteBufferView, size); 252 253 byteBuffer.flip(); 254 tmp.put(byteBuffer); 255 256 if ( primaryByteBuffer == null) { 258 primaryByteBuffer = byteBuffer; 259 } 260 byteBuffer = tmp; 261 return byteBuffer; 262 } 263 264 265 268 protected String dump(ByteBuffer byteBuffer){ 269 ByteBuffer dd = byteBuffer.duplicate(); 270 dd.flip(); 271 272 int length = dd.limit(); 273 byte[] dump = new byte[length]; 274 dd.get(dump,0,length); 275 return(new String (dump) + "\n----------------------------" + dd 276 + "\ncontentLength: " + contentLength 277 + "\nheaderLength: " + headerLength); 278 } 279 280 281 284 public void setSocketChannel(SocketChannel socketChannel){ 285 this.socketChannel = socketChannel; 286 } 287 288 289 292 public void setPort(int port){ 293 this.port = port; 294 } 295 296 297 300 public int getPort(){ 301 return port; 302 } 303 } 304 | Popular Tags |