1 // Copyright (C) 2014 Christian Jaekl
3 // Wrap an inputstream, keeping a copy of the first few bytes that are read from it.
4 // This is useful when passing an inputstream directly to the SAX parser,
5 // because we may want to do a post-mortem examination of the input being parsed
8 package net.jaekl.qd.util;
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.util.Arrays;
14 public class InputStreamWrapper extends InputStream {
15 final static int HEAD_MAX = 1024;
17 InputStream m_is; // the stream being wrapped
18 byte[] m_head; // the first (up to HEAD_MAX) bytes that were read from the wrapped stream
19 int m_headBytes; // number of bytes stored in m_head
21 public InputStreamWrapper(InputStream is)
26 m_head = new byte[HEAD_MAX];
31 public int read() throws IOException {
37 if (m_headBytes < HEAD_MAX) {
38 m_head[m_headBytes] = (byte)b;
44 public byte[] getHeadBytes() {
45 return Arrays.copyOf(m_head, m_headBytes);