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