Skip to content

Instantly share code, notes, and snippets.

@manzke
Created June 17, 2011 08:10
Show Gist options
  • Save manzke/1031058 to your computer and use it in GitHub Desktop.
Save manzke/1031058 to your computer and use it in GitHub Desktop.
InputStream which copies your available Data
import java.io.ByteArrayOutputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
public class CopyInputStream extends FilterInputStream {
private final ByteArrayOutputStream _copyTo = new ByteArrayOutputStream();
public CopyInputStream(InputStream in) {
super(in);
}
@Override
public int read() throws IOException {
int ch = in.read();
if (ch != -1) {
_copyTo.write(ch);
}
return ch;
}
@Override
public synchronized int read(byte[] b, int off, int len) throws IOException {
int size = super.read(b, off, len);
if (size != -1) {
_copyTo.write(b, off, size);
}
return size;
}
public byte[] toByteArray() {
return _copyTo.toByteArray();
}
}
@manzke
Copy link
Author

manzke commented Jun 17, 2011

If we know the length, we could also copy it into a NIO ByteBuffer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment