001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.io.input.buffer;
018
019import java.io.IOException;
020import java.io.InputStream;
021import java.util.Objects;
022
023/**
024 * Implements a buffered input stream, which allows to peek into the buffers first bytes. This comes in handy when
025 * manually implementing scanners, lexers, parsers, and the like.
026 */
027public class PeekableInputStream extends CircularBufferInputStream {
028
029    /**
030     * Creates a new instance, which filters the given input stream, and uses the given buffer size.
031     *
032     * @param inputStream The input stream, which is being buffered.
033     * @param bufferSize The size of the {@link CircularByteBuffer}, which is used internally.
034     */
035    public PeekableInputStream(final InputStream inputStream, final int bufferSize) {
036        super(inputStream, bufferSize);
037    }
038
039    /**
040     * Creates a new instance, which filters the given input stream, and uses a reasonable default buffer size (8192).
041     *
042     * @param inputStream The input stream, which is being buffered.
043     */
044    public PeekableInputStream(final InputStream inputStream) {
045        super(inputStream);
046    }
047
048    /**
049     * Returns whether the next bytes in the buffer are as given by {@code sourceBuffer}. This is equivalent to
050     * {@link #peek(byte[], int, int)} with {@code offset} == 0, and {@code length} == {@code sourceBuffer.length}
051     *
052     * @param sourceBuffer the buffer to compare against
053     * @return true if the next bytes are as given
054     * @throws IOException Refilling the buffer failed.
055     */
056    public boolean peek(final byte[] sourceBuffer) throws IOException {
057        Objects.requireNonNull(sourceBuffer, "sourceBuffer");
058        return peek(sourceBuffer, 0, sourceBuffer.length);
059    }
060
061    /**
062     * Returns whether the next bytes in the buffer are as given by {@code sourceBuffer}, {code offset}, and
063     * {@code length}.
064     *
065     * @param sourceBuffer the buffer to compare against
066     * @param offset the start offset
067     * @param length the length to compare
068     * @return true if the next bytes in the buffer are as given
069     * @throws IOException if there is a problem calling fillBuffer()
070     */
071    public boolean peek(final byte[] sourceBuffer, final int offset, final int length) throws IOException {
072        Objects.requireNonNull(sourceBuffer, "sourceBuffer");
073        if (sourceBuffer.length > bufferSize) {
074            throw new IllegalArgumentException("Peek request size of " + sourceBuffer.length
075                + " bytes exceeds buffer size of " + bufferSize + " bytes");
076        }
077        if (buffer.getCurrentNumberOfBytes() < sourceBuffer.length) {
078            fillBuffer();
079        }
080        return buffer.peek(sourceBuffer, offset, length);
081    }
082}