ThresholdingOutputStream.java
上传用户:u_thks
上传日期:2022-07-31
资源大小:1910k
文件大小:2k
源码类别:
WEB源码(ASP,PHP,...)
开发平台:
Java
- package com.gamvan.fileUpload;
- import java.io.IOException;
- import java.io.OutputStream;
- public abstract class ThresholdingOutputStream
- extends OutputStream
- {
- private int threshold;
- private long written;
- private boolean thresholdExceeded;
- public ThresholdingOutputStream(int threshold)
- {
- this.threshold = threshold;
- }
- public void write(int b) throws IOException
- {
- checkThreshold(1);
- getStream().write(b);
- written++;
- }
- public void write(byte b[]) throws IOException
- {
- checkThreshold(b.length);
- getStream().write(b);
- written += b.length;
- }
- public void write(byte b[], int off, int len) throws IOException
- {
- checkThreshold(len);
- getStream().write(b, off, len);
- written += len;
- }
- public void flush() throws IOException
- {
- getStream().flush();
- }
- public void close() throws IOException
- {
- try
- {
- flush();
- }
- catch (IOException ignored)
- {
- // ignore
- }
- getStream().close();
- }
- public int getThreshold()
- {
- return threshold;
- }
- public long getByteCount()
- {
- return written;
- }
- public boolean isThresholdExceeded()
- {
- return (written > threshold);
- }
- protected void checkThreshold(int count) throws IOException
- {
- if (!thresholdExceeded && (written + count > threshold))
- {
- thresholdReached();
- thresholdExceeded = true;
- }
- }
- protected abstract OutputStream getStream() throws IOException;
- protected abstract void thresholdReached() throws IOException;
- }