Add remembering input stream.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / io / RememberingInputStream.java
1 /*
2  * Sonitus - RememberingInputStream.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sonitus.io;
19
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;
25
26 /**
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}.
31  *
32  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33  */
34 public class RememberingInputStream extends FilterInputStream {
35
36         /** The buffer for the read bytes. */
37         private final ByteArrayOutputStream rememberBuffer = new ByteArrayOutputStream();
38
39         /**
40          * Creates a new remembering input stream.
41          *
42          * @param inputStream
43          *              The input stream to remember
44          */
45         public RememberingInputStream(InputStream inputStream) {
46                 super(inputStream);
47         }
48
49         //
50         // ACCESSORS
51         //
52
53         /**
54          * Returns all remembered bytes as an {@link InputStream}.
55          *
56          * @return All remembered bytes
57          */
58         public InputStream remembered() {
59                 return new ByteArrayInputStream(rememberBuffer.toByteArray());
60         }
61
62         //
63         // INPUTSTREAM METHODS
64         //
65
66         @Override
67         public boolean markSupported() {
68                 return false;
69         }
70
71         @Override
72         public int read() throws IOException {
73                 int read = super.read();
74                 if (read != -1) {
75                         rememberBuffer.write(read);
76                 }
77                 return read;
78         }
79
80         @Override
81         public int read(byte[] bytes) throws IOException {
82                 int read = super.read(bytes);
83                 if (read != -1) {
84                         rememberBuffer.write(bytes, 0, read);
85                 }
86                 return read;
87         }
88
89         @Override
90         public int read(byte[] bytes, int offset, int length) throws IOException {
91                 int read = super.read(bytes, offset, length);
92                 if (read != -1) {
93                         rememberBuffer.write(bytes, offset, read);
94                 }
95                 return read;
96         }
97
98         @Override
99         public synchronized void reset() throws IOException {
100                 throw new IOException("mark()/reset() not supported");
101         }
102
103         /**
104          * {@inheritDoc} <p> This method disallows seeking and always returns {@code
105          * 0}. </p>
106          */
107         @Override
108         public long skip(long l) throws IOException {
109                 return 0;
110         }
111
112 }