From 7b0b72dd23047298bf7a82f9231bb9ca0bab3c19 Mon Sep 17 00:00:00 2001 From: theli Date: Thu, 9 Jun 2005 09:47:56 +0000 Subject: [PATCH] *) adding new streams for - 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-ef5d649a1542 --- .../anomic/http/httpChunkedOutputStream.java | 60 +++++++++++++++ .../http/httpdByteCountInputStream.java | 72 +++++++++++++++++ .../http/httpdByteCountOutputStream.java | 77 +++++++++++++++++++ 3 files changed, 209 insertions(+) create mode 100644 source/de/anomic/http/httpChunkedOutputStream.java create mode 100644 source/de/anomic/http/httpdByteCountInputStream.java create mode 100644 source/de/anomic/http/httpdByteCountOutputStream.java diff --git a/source/de/anomic/http/httpChunkedOutputStream.java b/source/de/anomic/http/httpChunkedOutputStream.java new file mode 100644 index 000000000..7c0bc974c --- /dev/null +++ b/source/de/anomic/http/httpChunkedOutputStream.java @@ -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(); + } +} diff --git a/source/de/anomic/http/httpdByteCountInputStream.java b/source/de/anomic/http/httpdByteCountInputStream.java new file mode 100644 index 000000000..93fc22efd --- /dev/null +++ b/source/de/anomic/http/httpdByteCountInputStream.java @@ -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(); + } +} diff --git a/source/de/anomic/http/httpdByteCountOutputStream.java b/source/de/anomic/http/httpdByteCountOutputStream.java new file mode 100644 index 000000000..60a129dfa --- /dev/null +++ b/source/de/anomic/http/httpdByteCountOutputStream.java @@ -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(); + } +}