1 package net.jaekl.qd.util;
3 import java.io.ByteArrayInputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.nio.charset.Charset;
7 import java.util.ArrayList;
9 public class InputStreamMock extends InputStream {
10 private ByteArrayInputStream m_bais;
12 public InputStreamMock() {
13 m_bais = new ByteArrayInputStream(new byte[0]);
17 public int read() throws IOException {
21 public void mock_append(byte[] addition) {
22 byte[] old = readAll();
23 byte[] combined = new byte[old.length + addition.length];
24 for (int idx = 0; idx < old.length; ++idx) {
25 combined[idx] = old[idx];
27 for (int idx = 0; idx < addition.length; ++idx) {
28 combined[old.length + idx] = addition[idx];
34 public void mock_append(String addition, Charset charset)
36 mock_append(addition.getBytes(charset));
39 public void mock_reset(byte[] newData) {
40 m_bais = new ByteArrayInputStream(newData);
43 // Read all remaining bytes in the inputstream
44 private byte[] readAll() {
45 // This is horribly inefficient; don't use in production code
46 ArrayList<Byte> data = new ArrayList<Byte>();
47 int b = m_bais.read();
49 data.add(Byte.valueOf((byte)b));
51 byte[] result = new byte[data.size()];
52 for (int idx = 0; idx < result.length; ++idx) {
53 result[idx] = data.get(idx).byteValue();