- implementation of outgoing chunked transfer encoding (httpChunkedOutputStream.java) - byte counting for proxy access logging / global traffic count (httpdByteCount(In|Out)putStream.java git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@241 6c8d7289-2bf4-0310-a012-ef5d649a1542pull/1/head
parent
318dfc4636
commit
7b0b72dd23
@ -0,0 +1,60 @@
|
||||
package de.anomic.http;
|
||||
|
||||
import java.io.FilterOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
final class httpChunkedOutputStream extends FilterOutputStream
|
||||
{
|
||||
private boolean finished = false;
|
||||
private static final byte[] crlf = {(byte)13,(byte)10};
|
||||
|
||||
public httpChunkedOutputStream(OutputStream out) {
|
||||
super(out);
|
||||
}
|
||||
|
||||
public void close() throws IOException {
|
||||
if (!this.finished) this.finish();
|
||||
this.out.close();
|
||||
}
|
||||
|
||||
public void finish() throws IOException {
|
||||
if (!this.finished) {
|
||||
this.out.write("0\r\n\r\n".getBytes());
|
||||
this.out.flush();
|
||||
this.finished = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void write(byte[] b) throws IOException {
|
||||
if (this.finished) throw new IOException("ChunkedOutputStream already finalized.");
|
||||
if (b.length == 0) return;
|
||||
|
||||
this.out.write(Integer.toHexString(b.length).getBytes());
|
||||
this.out.write(crlf);
|
||||
this.out.write(b);
|
||||
this.out.write(crlf);
|
||||
this.out.flush();
|
||||
}
|
||||
|
||||
public void write(byte[] b, int off, int len) throws IOException {
|
||||
if (this.finished) throw new IOException("ChunkedOutputStream already finalized.");
|
||||
if (len == 0) return;
|
||||
|
||||
this.out.write(Integer.toHexString(len).getBytes());
|
||||
this.out.write(crlf);
|
||||
this.out.write(b, off, len);
|
||||
this.out.write(crlf);
|
||||
this.out.flush();
|
||||
}
|
||||
|
||||
public void write(int b) throws IOException {
|
||||
if (this.finished) throw new IOException("ChunkedOutputStream already finalized.");
|
||||
|
||||
this.out.write("1".getBytes());
|
||||
this.out.write(crlf);
|
||||
this.out.write(b);
|
||||
this.out.write(crlf);
|
||||
this.out.flush();
|
||||
}
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
package de.anomic.http;
|
||||
|
||||
import java.io.FilterInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public final class httpdByteCountInputStream extends FilterInputStream {
|
||||
|
||||
private static final Object syncObject = new Object();
|
||||
private static long globalByteCount = 0;
|
||||
|
||||
private boolean finished = false;
|
||||
private long byteCount;
|
||||
|
||||
/**
|
||||
* Constructor of this class
|
||||
* @param inputStream the {@link InputStream} to read from
|
||||
*/
|
||||
public httpdByteCountInputStream(InputStream inputStream) {
|
||||
super(inputStream);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor of this class
|
||||
* @param inputStream the {@link InputStream} to read from
|
||||
* @param initByteCount to initialize the bytecount with a given value
|
||||
*/
|
||||
public httpdByteCountInputStream(InputStream inputStream, int initByteCount) {
|
||||
super(inputStream);
|
||||
this.byteCount = initByteCount;
|
||||
}
|
||||
|
||||
public int read(byte[] b) throws IOException {
|
||||
int readCount = super.read(b);
|
||||
this.byteCount += readCount;
|
||||
return readCount;
|
||||
}
|
||||
|
||||
public int read(byte[] b, int off, int len) throws IOException {
|
||||
int readCount = super.read(b, off, len);
|
||||
this.byteCount += readCount;
|
||||
return readCount;
|
||||
}
|
||||
|
||||
public int read() throws IOException {
|
||||
this.byteCount++;
|
||||
return super.read();
|
||||
}
|
||||
|
||||
public long getCount() {
|
||||
return this.byteCount;
|
||||
}
|
||||
|
||||
public static long getGlobalCount() {
|
||||
synchronized (syncObject) {
|
||||
return globalByteCount;
|
||||
}
|
||||
}
|
||||
|
||||
public void finish() throws IOException {
|
||||
this.finished = true;
|
||||
synchronized (syncObject) {
|
||||
globalByteCount += this.byteCount;
|
||||
}
|
||||
}
|
||||
|
||||
protected void finalize() throws Throwable {
|
||||
if (!this.finished)
|
||||
finish();
|
||||
super.finalize();
|
||||
}
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
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 void finish() throws IOException {
|
||||
this.finished = true;
|
||||
synchronized (syncObject) {
|
||||
globalByteCount += this.byteCount;
|
||||
}
|
||||
}
|
||||
|
||||
protected void finalize() throws Throwable {
|
||||
if (!this.finished)
|
||||
finish();
|
||||
super.finalize();
|
||||
}
|
||||
}
|
Loading…
Reference in new issue