Created
June 17, 2011 08:10
-
-
Save manzke/1031058 to your computer and use it in GitHub Desktop.
InputStream which copies your available Data
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
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If we know the length, we could also copy it into a NIO ByteBuffer.