1 61 62 63 package org.apache.commons.fileupload; 64 65 import java.io.IOException ; 66 import java.io.OutputStream ; 67 68 69 87 public abstract class ThresholdingOutputStream 88 extends OutputStream 89 { 90 91 93 94 97 private int threshold; 98 99 100 103 private long written; 104 105 106 109 private boolean thresholdExceeded; 110 111 112 114 115 121 public ThresholdingOutputStream(int threshold) 122 { 123 this.threshold = threshold; 124 } 125 126 127 129 130 137 public void write(int b) throws IOException 138 { 139 checkThreshold(1); 140 getStream().write(b); 141 written++; 142 } 143 144 145 153 public void write(byte b[]) throws IOException 154 { 155 checkThreshold(b.length); 156 getStream().write(b); 157 written += b.length; 158 } 159 160 161 171 public void write(byte b[], int off, int len) throws IOException 172 { 173 checkThreshold(len); 174 getStream().write(b, off, len); 175 written += len; 176 } 177 178 179 185 public void flush() throws IOException 186 { 187 getStream().flush(); 188 } 189 190 191 197 public void close() throws IOException 198 { 199 try 200 { 201 flush(); 202 } 203 catch (IOException ignored) 204 { 205 } 207 getStream().close(); 208 } 209 210 211 213 214 219 public int getThreshold() 220 { 221 return threshold; 222 } 223 224 225 230 public long getByteCount() 231 { 232 return written; 233 } 234 235 236 243 public boolean isThresholdExceeded() 244 { 245 return (written > threshold); 246 } 247 248 249 251 252 262 protected void checkThreshold(int count) throws IOException 263 { 264 if (!thresholdExceeded && (written + count > threshold)) 265 { 266 thresholdReached(); 267 thresholdExceeded = true; 268 } 269 } 270 271 272 274 275 283 protected abstract OutputStream getStream() throws IOException ; 284 285 286 293 protected abstract void thresholdReached() throws IOException ; 294 } 295 | Popular Tags |