1 17 18 package org.apache.coyote.http11.filters; 19 20 import java.io.IOException ; 21 22 import org.apache.tomcat.util.buf.ByteChunk; 23 24 import org.apache.coyote.InputBuffer; 25 import org.apache.coyote.Request; 26 import org.apache.coyote.http11.InputFilter; 27 28 33 public class IdentityInputFilter implements InputFilter { 34 35 36 38 39 protected static final String ENCODING_NAME = "identity"; 40 protected static final ByteChunk ENCODING = new ByteChunk(); 41 42 43 45 46 static { 47 ENCODING.setBytes(ENCODING_NAME.getBytes(), 0, ENCODING_NAME.length()); 48 } 49 50 51 53 54 57 protected long contentLength = -1; 58 59 60 63 protected long remaining = 0; 64 65 66 69 protected InputBuffer buffer; 70 71 72 75 protected ByteChunk endChunk = new ByteChunk(); 76 77 78 80 81 84 public long getContentLength() { 85 return contentLength; 86 } 87 88 89 92 public long getRemaining() { 93 return remaining; 94 } 95 96 97 99 100 109 public int doRead(ByteChunk chunk, Request req) 110 throws IOException { 111 112 int result = -1; 113 114 if (contentLength >= 0) { 115 if (remaining > 0) { 116 int nRead = buffer.doRead(chunk, req); 117 if (nRead > remaining) { 118 chunk.setBytes(chunk.getBytes(), chunk.getStart(), 122 (int) remaining); 123 result = (int) remaining; 124 } else { 125 result = nRead; 126 } 127 remaining = remaining - nRead; 128 } else { 129 chunk.recycle(); 132 result = -1; 133 } 134 } 135 136 return result; 137 138 } 139 140 141 143 144 147 public void setRequest(Request request) { 148 contentLength = request.getContentLengthLong(); 149 remaining = contentLength; 150 } 151 152 153 156 public long end() 157 throws IOException { 158 159 while (remaining > 0) { 161 int nread = buffer.doRead(endChunk, null); 162 if (nread > 0 ) { 163 remaining = remaining - nread; 164 } else { remaining = 0; 166 } 167 } 168 169 return -remaining; 171 172 } 173 174 175 178 public void setBuffer(InputBuffer buffer) { 179 this.buffer = buffer; 180 } 181 182 183 186 public void recycle() { 187 contentLength = -1; 188 remaining = 0; 189 endChunk.recycle(); 190 } 191 192 193 197 public ByteChunk getEncodingName() { 198 return ENCODING; 199 } 200 201 202 } 203 | Popular Tags |