package de.siphalor.was.util;

import org.jetbrains.annotations.NotNull;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * An input stream that can be reused. <br>
 * Close with {@link PersistentInputStream#realClose()} <br>
 * <b>Taken from here: https://stackoverflow.com/questions/924990/how-to-cache-inputstream-for-multiple-use#1303314</b>
 */
public class PersistentInputStream extends BufferedInputStream {
	/**
	 * Constructs a new persistent input stream.
	 * @param in The base input stream
	 */
	public PersistentInputStream(@NotNull InputStream in) {
		super(in);
		super.mark(Integer.MAX_VALUE);
	}

	/**
	 * Resets this input stream.
	 */
	@Override
	public void close() throws IOException {
		super.reset();
	}

	/**
	 * Closes this input stream.
	 */
	public void realClose() throws IOException {
		super.close();
	}
}
