1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.io.input.buffer; 18 19 import java.io.IOException; 20 import java.io.InputStream; 21 import java.util.Objects; 22 23 import org.apache.commons.io.IOUtils; 24 25 /** 26 * Implements a buffered input stream, which allows to peek into the buffers first bytes. This comes in handy when 27 * manually implementing scanners, lexers, parsers, and the like. 28 */ 29 public class PeekableInputStream extends CircularBufferInputStream { 30 31 /** 32 * Constructs a new instance, which filters the given input stream, and uses a reasonable default buffer size ({@link IOUtils#DEFAULT_BUFFER_SIZE}). 33 * 34 * @param inputStream The input stream, which is being buffered. 35 */ 36 public PeekableInputStream(final InputStream inputStream) { 37 super(inputStream); 38 } 39 40 /** 41 * Constructs a new instance, which filters the given input stream, and uses the given buffer size. 42 * 43 * @param inputStream The input stream, which is being buffered. 44 * @param bufferSize The size of the {@link CircularByteBuffer}, which is used internally. 45 */ 46 public PeekableInputStream(final InputStream inputStream, final int bufferSize) { 47 super(inputStream, bufferSize); 48 } 49 50 /** 51 * Returns whether the next bytes in the buffer are as given by {@code sourceBuffer}. This is equivalent to 52 * {@link #peek(byte[], int, int)} with {@code offset} == 0, and {@code length} == {@code sourceBuffer.length} 53 * 54 * @param sourceBuffer the buffer to compare against 55 * @return true if the next bytes are as given 56 * @throws IOException Refilling the buffer failed. 57 */ 58 public boolean peek(final byte[] sourceBuffer) throws IOException { 59 Objects.requireNonNull(sourceBuffer, "sourceBuffer"); 60 return peek(sourceBuffer, 0, sourceBuffer.length); 61 } 62 63 /** 64 * Returns whether the next bytes in the buffer are as given by {@code sourceBuffer}, {code offset}, and 65 * {@code length}. 66 * 67 * @param sourceBuffer the buffer to compare against 68 * @param offset the start offset 69 * @param length the length to compare 70 * @return true if the next bytes in the buffer are as given 71 * @throws IOException if there is a problem calling fillBuffer() 72 */ 73 public boolean peek(final byte[] sourceBuffer, final int offset, final int length) throws IOException { 74 Objects.requireNonNull(sourceBuffer, "sourceBuffer"); 75 if (sourceBuffer.length > bufferSize) { 76 throw new IllegalArgumentException("Peek request size of " + sourceBuffer.length 77 + " bytes exceeds buffer size of " + bufferSize + " bytes"); 78 } 79 if (buffer.getCurrentNumberOfBytes() < sourceBuffer.length) { 80 fillBuffer(); 81 } 82 return buffer.peek(sourceBuffer, offset, length); 83 } 84 }