yacy_search_server/source/de/anomic/http/httpChunkedOutputStream.java
theli 7b0b72dd23 *) 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
2005-06-09 09:47:56 +00:00

61 lines
1.8 KiB
Java

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();
}
}