Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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
- Loading branch information
theli
committed
Jun 9, 2005
1 parent
318dfc4
commit 7b0b72d
Showing
3 changed files
with
209 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |