    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/**
    025 * Implementation of a buffered input stream, which allows to peek into
    026 * the buffers first bytes. This comes in handy when manually implementing
    027 * scanners, lexers, parsers, or the like.
    028 */
    029public class PeekableInputStream extends CircularBufferInputStream {
    030
    031    /**
    032     * Creates a new instance, which filters the given input stream, and
    033     * uses the given buffer size.
    034     *
    035     * @param inputStream         The input stream, which is being buffered.
    036     * @param bufferSize The size of the {@link CircularByteBuffer}, which is
    037     *                    used internally.
    038     */
    039    public PeekableInputStream(final InputStream inputStream, final int bufferSize) {
    040        super(inputStream, bufferSize);
    041    }
    042
    043    /**
    044     * Creates a new instance, which filters the given input stream, and
    045     * uses a reasonable default buffer size (8192).
    046     *
    047     * @param inputStream The input stream, which is being buffered.
    048     */
    049    public PeekableInputStream(final InputStream inputStream) {
    050        super(inputStream);
    051    }
    052
    053    /**
    054     * Returns, whether the next bytes in the buffer are as given by
    055     * {@code sourceBuffer}. This is equivalent to {@link #peek(byte[], int, int)}
    056     * with {@code offset} == 0, and {@code length} == {@code sourceBuffer.length}
    057     *
    058     * @param sourceBuffer the buffer to compare against
    059     * @return true if the next bytes are as given
    060     * @throws IOException Refilling the buffer failed.
    061     */
    062    public boolean peek(final byte[] sourceBuffer) throws IOException {
    063        Objects.requireNonNull(sourceBuffer, "Buffer");
    064        if (sourceBuffer.length > bufferSize) {
    065            throw new IllegalArgumentException("Peek request size of " + sourceBuffer.length
    066                    + " bytes exceeds buffer size of " + bufferSize + " bytes");
    067        }
    068        if (buffer.getCurrentNumberOfBytes() < sourceBuffer.length) {
    069            fillBuffer();
    070        }
    071        return buffer.peek(sourceBuffer, 0, sourceBuffer.length);
    072    }
    073
    074    /**
    075     * Returns, whether the next bytes in the buffer are as given by
    076     * {@code sourceBuffer}, {code offset}, and {@code length}.
    077     *
    078     * @param sourceBuffer the buffer to compare against
    079     * @param offset the start offset
    080     * @param length the length to compare
    081     * @return true if the next bytes in the buffer are as given
    082     * @throws IOException if there is a problem calling fillBuffer()
    083     */
    084    public boolean peek(final byte[] sourceBuffer, final int offset, final int length) throws IOException {
    085        Objects.requireNonNull(sourceBuffer, "Buffer");
    086        if (sourceBuffer.length > bufferSize) {
    087            throw new IllegalArgumentException("Peek request size of " + sourceBuffer.length
    088                    + " bytes exceeds buffer size of " + bufferSize + " bytes");
    089        }
    090        if (buffer.getCurrentNumberOfBytes() < sourceBuffer.length) {
    091            fillBuffer();
    092        }
    093        return buffer.peek(sourceBuffer, offset, length);
    094    }
    095}