求证,一段开源项目上的源代码。
xmpp 2004-02-25 02:48:50 项目:apache上的jakarta下的commons-fileupload。
其中有一个类ThresholdingOutputStream.java
完整代码如下:
/*
* $Header: /home/cvs/jakarta-commons/fileupload/src/java/org/apache/commons/fileupload/ThresholdingOutputStream.java,v 1.3 2003/05/31 22:31:08 martinc Exp $
* $Revision: 1.3 $
* $Date: 2003/05/31 22:31:08 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
package org.apache.commons.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;
}
其中的checkThreshold(int count)方法应该有问题的啊。因为thresholdExceeded属性还没有初始化啊。