RecordInputStream.java

/***************************************************************************
   Copyright 2015 Emily Estes

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
***************************************************************************/
package net.metanotion.util;


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

/** This class has shown to be useful in several bits of test code. It treats an input stream as a sequence of input
streams delimited by a character (the default is the ASCII/UTF-8 record separator character). In tests this is useful
for putting multiple test cases in the same file(separated by the delimiter) so that one input stream can contain all
the test cases. */
public final class RecordInputStream extends InputStream {
	public static final int RECORD_SEPARATOR = 30;

	private final InputStream in;
	private final int delimiter;
	/** Create a RecordInputStream with the default delimiter.
		@param in The inputstream to filter.
	*/
	public RecordInputStream(final InputStream in) { this(in, RECORD_SEPARATOR); }

	/** Create a RecordInputStream with your choice of delimiter.
		@param in The inputstream to filter.
		@param delimiter The character to use as the delimiter.
	*/
	public RecordInputStream(final InputStream in, final int delimiter) {
		this.in = in;
		this.delimiter = delimiter;
	}

	private boolean eof = false;
	private boolean rs = false;

	@Override public int read() throws IOException {
		if(rs || eof) { return -1; }
		final int r = this.in.read();
		if(r == delimiter) {
			rs = true;
			return -1;
		} else if(r == -1) {
			eof = true;
		}
		return r;
	}

	/** Reset the EOF marker so we can advance to the next record.
		@return true if there is another record, false if it's the actual end of the stream.
	*/
	public boolean nextRecord() {
		if(rs && !eof) {
			rs = false;
			return true;
		} else {
			return false;
		}
	}

	/** This method does nothing. */
	@Override public void close() { }
}