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.OutputBuffer; 25 import org.apache.coyote.Response; 26 import org.apache.coyote.http11.OutputFilter; 27 28 33 public class IdentityOutputFilter implements OutputFilter { 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 OutputBuffer buffer; 70 71 72 74 75 78 public long getContentLength() { 79 return contentLength; 80 } 81 82 83 86 public long getRemaining() { 87 return remaining; 88 } 89 90 91 93 94 99 public int doWrite(ByteChunk chunk, Response res) 100 throws IOException { 101 102 int result = -1; 103 104 if (contentLength >= 0) { 105 if (remaining > 0) { 106 result = chunk.getLength(); 107 if (result > remaining) { 108 chunk.setBytes(chunk.getBytes(), chunk.getStart(), 112 (int) remaining); 113 result = (int) remaining; 114 remaining = 0; 115 } else { 116 remaining = remaining - result; 117 } 118 buffer.doWrite(chunk, res); 119 } else { 120 chunk.recycle(); 123 result = -1; 124 } 125 } else { 126 buffer.doWrite(chunk, res); 128 result = chunk.getLength(); 129 } 130 131 return result; 132 133 } 134 135 136 138 139 144 public void setResponse(Response response) { 145 contentLength = response.getContentLengthLong(); 146 remaining = contentLength; 147 } 148 149 150 153 public void setBuffer(OutputBuffer buffer) { 154 this.buffer = buffer; 155 } 156 157 158 162 public long end() 163 throws IOException { 164 165 if (remaining > 0) 166 return remaining; 167 return 0; 168 169 } 170 171 172 175 public void recycle() { 176 contentLength = -1; 177 remaining = 0; 178 } 179 180 181 185 public ByteChunk getEncodingName() { 186 return ENCODING; 187 } 188 189 190 } 191 | Popular Tags |