Add remembering input stream.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 14 Mar 2013 21:23:25 +0000 (22:23 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 14 Mar 2013 21:23:25 +0000 (22:23 +0100)
src/main/java/net/pterodactylus/sonitus/io/RememberingInputStream.java [new file with mode: 0644]

diff --git a/src/main/java/net/pterodactylus/sonitus/io/RememberingInputStream.java b/src/main/java/net/pterodactylus/sonitus/io/RememberingInputStream.java
new file mode 100644 (file)
index 0000000..ac2e5f0
--- /dev/null
@@ -0,0 +1,112 @@
+/*
+ * Sonitus - RememberingInputStream.java - Copyright © 2013 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sonitus.io;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.FilterInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * Wrapper around an {@link InputStream} that remembers all bytes that have been
+ * read from the wrapped input stream. The remembered bytes can be retrieved
+ * from this stream as another input stream, suitable for use with {@link
+ * java.io.SequenceInputStream}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class RememberingInputStream extends FilterInputStream {
+
+       /** The buffer for the read bytes. */
+       private final ByteArrayOutputStream rememberBuffer = new ByteArrayOutputStream();
+
+       /**
+        * Creates a new remembering input stream.
+        *
+        * @param inputStream
+        *              The input stream to remember
+        */
+       public RememberingInputStream(InputStream inputStream) {
+               super(inputStream);
+       }
+
+       //
+       // ACCESSORS
+       //
+
+       /**
+        * Returns all remembered bytes as an {@link InputStream}.
+        *
+        * @return All remembered bytes
+        */
+       public InputStream remembered() {
+               return new ByteArrayInputStream(rememberBuffer.toByteArray());
+       }
+
+       //
+       // INPUTSTREAM METHODS
+       //
+
+       @Override
+       public boolean markSupported() {
+               return false;
+       }
+
+       @Override
+       public int read() throws IOException {
+               int read = super.read();
+               if (read != -1) {
+                       rememberBuffer.write(read);
+               }
+               return read;
+       }
+
+       @Override
+       public int read(byte[] bytes) throws IOException {
+               int read = super.read(bytes);
+               if (read != -1) {
+                       rememberBuffer.write(bytes, 0, read);
+               }
+               return read;
+       }
+
+       @Override
+       public int read(byte[] bytes, int offset, int length) throws IOException {
+               int read = super.read(bytes, offset, length);
+               if (read != -1) {
+                       rememberBuffer.write(bytes, offset, read);
+               }
+               return read;
+       }
+
+       @Override
+       public synchronized void reset() throws IOException {
+               throw new IOException("mark()/reset() not supported");
+       }
+
+       /**
+        * {@inheritDoc} <p> This method disallows seeking and always returns {@code
+        * 0}. </p>
+        */
+       @Override
+       public long skip(long l) throws IOException {
+               return 0;
+       }
+
+}