1 17 18 19 package org.apache.tomcat.util.http.fileupload; 20 21 import java.io.IOException ; 22 import java.io.OutputStream ; 23 24 25 43 public abstract class ThresholdingOutputStream 44 extends OutputStream 45 { 46 47 49 50 53 private int threshold; 54 55 56 59 private long written; 60 61 62 65 private boolean thresholdExceeded; 66 67 68 70 71 77 public ThresholdingOutputStream(int threshold) 78 { 79 this.threshold = threshold; 80 } 81 82 83 85 86 93 public void write(int b) throws IOException 94 { 95 checkThreshold(1); 96 getStream().write(b); 97 written++; 98 } 99 100 101 109 public void write(byte b[]) throws IOException 110 { 111 checkThreshold(b.length); 112 getStream().write(b); 113 written += b.length; 114 } 115 116 117 127 public void write(byte b[], int off, int len) throws IOException 128 { 129 checkThreshold(len); 130 getStream().write(b, off, len); 131 written += len; 132 } 133 134 135 141 public void flush() throws IOException 142 { 143 getStream().flush(); 144 } 145 146 147 153 public void close() throws IOException 154 { 155 try 156 { 157 flush(); 158 } 159 catch (IOException ignored) 160 { 161 } 163 getStream().close(); 164 } 165 166 167 169 170 175 public int getThreshold() 176 { 177 return threshold; 178 } 179 180 181 186 public long getByteCount() 187 { 188 return written; 189 } 190 191 192 199 public boolean isThresholdExceeded() 200 { 201 return (written > threshold); 202 } 203 204 205 207 208 218 protected void checkThreshold(int count) throws IOException 219 { 220 if (!thresholdExceeded && (written + count > threshold)) 221 { 222 thresholdReached(); 223 thresholdExceeded = true; 224 } 225 } 226 227 228 230 231 239 protected abstract OutputStream getStream() throws IOException ; 240 241 242 249 protected abstract void thresholdReached() throws IOException ; 250 } 251 | Popular Tags |