1 16 17 18 package org.apache.commons.io.output; 19 20 import java.io.IOException ; 21 import java.io.OutputStream ; 22 23 24 42 public abstract class ThresholdingOutputStream 43 extends OutputStream 44 { 45 46 48 49 52 private int threshold; 53 54 55 58 private long written; 59 60 61 64 private boolean thresholdExceeded; 65 66 67 69 70 76 public ThresholdingOutputStream(int threshold) 77 { 78 this.threshold = threshold; 79 } 80 81 82 84 85 92 public void write(int b) throws IOException 93 { 94 checkThreshold(1); 95 getStream().write(b); 96 written++; 97 } 98 99 100 108 public void write(byte b[]) throws IOException 109 { 110 checkThreshold(b.length); 111 getStream().write(b); 112 written += b.length; 113 } 114 115 116 126 public void write(byte b[], int off, int len) throws IOException 127 { 128 checkThreshold(len); 129 getStream().write(b, off, len); 130 written += len; 131 } 132 133 134 140 public void flush() throws IOException 141 { 142 getStream().flush(); 143 } 144 145 146 152 public void close() throws IOException 153 { 154 try 155 { 156 flush(); 157 } 158 catch (IOException ignored) 159 { 160 } 162 getStream().close(); 163 } 164 165 166 168 169 174 public int getThreshold() 175 { 176 return threshold; 177 } 178 179 180 185 public long getByteCount() 186 { 187 return written; 188 } 189 190 191 198 public boolean isThresholdExceeded() 199 { 200 return (written > threshold); 201 } 202 203 204 206 207 217 protected void checkThreshold(int count) throws IOException 218 { 219 if (!thresholdExceeded && (written + count > threshold)) 220 { 221 thresholdReached(); 222 thresholdExceeded = true; 223 } 224 } 225 226 227 229 230 238 protected abstract OutputStream getStream() throws IOException ; 239 240 241 248 protected abstract void thresholdReached() throws IOException ; 249 } 250 | Popular Tags |