1 17 18 package org.apache.coyote.http11.filters; 19 20 import java.io.IOException ; 21 import org.apache.coyote.Request; 22 import org.apache.coyote.InputBuffer; 23 import org.apache.coyote.http11.InputFilter; 24 import org.apache.tomcat.util.buf.ByteChunk; 25 26 30 public class BufferedInputFilter implements InputFilter { 31 32 34 private static final String ENCODING_NAME = "buffered"; 35 private static final ByteChunk ENCODING = new ByteChunk(); 36 37 38 40 private ByteChunk buffered = null; 41 private ByteChunk tempRead = new ByteChunk(1024); 42 private InputBuffer buffer; 43 private boolean hasRead = false; 44 45 46 48 static { 49 ENCODING.setBytes(ENCODING_NAME.getBytes(), 0, ENCODING_NAME.length()); 50 } 51 52 53 55 56 60 public void setLimit(int limit) { 61 if (buffered == null) { 62 buffered = new ByteChunk(4048); 63 buffered.setLimit(limit); 64 } 65 } 66 67 68 70 71 74 public void setRequest(Request request) { 75 try { 77 while (buffer.doRead(tempRead, request) >= 0) { 78 buffered.append(tempRead); 79 tempRead.recycle(); 80 } 81 } catch(IOException iex) { 82 } 84 } 85 86 89 public int doRead(ByteChunk chunk, Request request) throws IOException { 90 if (hasRead || buffered.getLength() <= 0) { 91 return -1; 92 } else { 93 chunk.setBytes(buffered.getBytes(), buffered.getStart(), 94 buffered.getLength()); 95 hasRead = true; 96 } 97 return chunk.getLength(); 98 } 99 100 public void setBuffer(InputBuffer buffer) { 101 this.buffer = buffer; 102 } 103 104 public void recycle() { 105 if (buffered.getBuffer().length > 65536) { 106 buffered = null; 107 } else { 108 buffered.recycle(); 109 } 110 tempRead.recycle(); 111 hasRead = false; 112 buffer = null; 113 } 114 115 public ByteChunk getEncodingName() { 116 return ENCODING; 117 } 118 119 public long end() throws IOException { 120 return 0; 121 } 122 123 } 124 | Popular Tags |