yacy_search_server/source/de/anomic/http/httpdByteCountOutputStream.java
orbiter 37f88b4017 code cleanup
git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@1176 6c8d7289-2bf4-0310-a012-ef5d649a1542
2005-12-06 23:51:29 +00:00

86 lines
2.3 KiB
Java

package de.anomic.http;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public final class httpdByteCountOutputStream extends BufferedOutputStream {
private static final Object syncObject = new Object();
private static long globalByteCount = 0;
private boolean finished = false;
private long byteCount;
/**
* Constructor of this class
* @param outputStream the {@link OutputStream} to write to
*/
public httpdByteCountOutputStream(OutputStream outputStream) {
super(outputStream);
}
/**
* Constructor of this class
* @param outputStream the {@link OutputStream} to write to
* @param initByteCount to initialize the bytecount with a given value
*/
public httpdByteCountOutputStream(OutputStream outputStream, int initByteCount) {
super(outputStream);
this.byteCount = initByteCount;
}
/** @see java.io.OutputStream#write(byte[]) */
public void write(byte[] b) throws IOException {
super.write(b);
this.byteCount += b.length;
}
/** @see java.io.OutputStream#write(byte[], int, int) */
public void write(byte[] b, int off, int len) throws IOException {
super.write(b, off, len);
this.byteCount += len;
}
/** @see java.io.OutputStream#write(int) */
public void write(int b) throws IOException {
super.write(b);
this.byteCount++;
}
/**
* The number of bytes that have passed through this stream.
* @return the number of bytes accumulated
*/
public long getCount() {
return this.byteCount;
}
public static long getGlobalCount() {
synchronized (syncObject) {
return globalByteCount;
}
}
public static void resetCount() {
synchronized (syncObject) {
globalByteCount = 0;
}
}
public void finish() {
if (this.finished) return;
this.finished = true;
synchronized (syncObject) {
globalByteCount += this.byteCount;
}
}
protected void finalize() throws Throwable {
if (!this.finished)
finish();
super.finalize();
}
}