From: David ‘Bombe’ Roden Date: Thu, 14 Mar 2013 21:23:25 +0000 (+0100) Subject: Add remembering input stream. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=73e0a751b87b2edbde70babe2e2578e139ba7ff8;p=sonitus.git Add remembering input stream. --- 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 index 0000000..ac2e5f0 --- /dev/null +++ b/src/main/java/net/pterodactylus/sonitus/io/RememberingInputStream.java @@ -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 . + */ + +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 David ‘Bombe’ Roden + */ +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}

This method disallows seeking and always returns {@code + * 0}.

+ */ + @Override + public long skip(long l) throws IOException { + return 0; + } + +}