11 Ağustos 2007 Cumartesi

Printing the data coming through an InputStream without copying that data into memory first

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class DebugInputStream extends FilterInputStream {

private OutputStream debugOut;

public DebugInputStream(InputStream in, OutputStream debugOut) {
super(in);
this.debugOut = debugOut;
}

public int read() throws IOException {
int c = super.read();
debugOut.write((char) c);
return c;
}

public int read(byte[] b) throws IOException {
int readCount = super.read(b);
for (int i = 0; i < readCount; i++) {
debugOut.write((char) b[i]);
}
return readCount;
}

public int read(byte[] b, int offset, int length) throws IOException {
int readCount = super.read(b, offset, length);
int readTo = offset + readCount;
for (int i = offset; i < readTo; i++) {
debugOut.write((char) b[i]);
}
return readCount;
}

}


0 Comments: