2 * Sonitus - RememberingInputStream.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sonitus.io;
20 import java.io.ByteArrayInputStream;
21 import java.io.ByteArrayOutputStream;
22 import java.io.FilterInputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
27 * Wrapper around an {@link InputStream} that remembers all bytes that have been
28 * read from the wrapped input stream. The remembered bytes can be retrieved
29 * from this stream as another input stream, suitable for use with {@link
30 * java.io.SequenceInputStream}.
32 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34 public class RememberingInputStream extends FilterInputStream {
36 /** The buffer for the read bytes. */
37 private final ByteArrayOutputStream rememberBuffer = new ByteArrayOutputStream();
40 * Creates a new remembering input stream.
43 * The input stream to remember
45 public RememberingInputStream(InputStream inputStream) {
54 * Returns all remembered bytes as an {@link InputStream}.
56 * @return All remembered bytes
58 public InputStream remembered() {
59 return new ByteArrayInputStream(rememberBuffer.toByteArray());
63 // INPUTSTREAM METHODS
67 public boolean markSupported() {
72 public int read() throws IOException {
73 int read = super.read();
75 rememberBuffer.write(read);
81 public int read(byte[] bytes) throws IOException {
82 int read = super.read(bytes);
84 rememberBuffer.write(bytes, 0, read);
90 public int read(byte[] bytes, int offset, int length) throws IOException {
91 int read = super.read(bytes, offset, length);
93 rememberBuffer.write(bytes, offset, read);
99 public synchronized void reset() throws IOException {
100 throw new IOException("mark()/reset() not supported");
104 * {@inheritDoc} <p> This method disallows seeking and always returns {@code
108 public long skip(long l) throws IOException {