X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsonitus%2Fio%2FRememberingInputStream.java;fp=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsonitus%2Fio%2FRememberingInputStream.java;h=ac2e5f050cf2a0e127ada2d6ee931001a247729a;hb=73e0a751b87b2edbde70babe2e2578e139ba7ff8;hp=0000000000000000000000000000000000000000;hpb=ef136195e508933d207ba9bd0899a942273aba73;p=sonitus.git 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; + } + +}