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