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 * https://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 * @since 2.7
30 */
31 public class PeekableInputStream extends CircularBufferInputStream {
32
33 /**
34 * Constructs a new instance, which filters the given input stream, and uses a reasonable default buffer size ({@link IOUtils#DEFAULT_BUFFER_SIZE}).
35 *
36 * @param inputStream The input stream, which is being buffered.
37 */
38 public PeekableInputStream(final InputStream inputStream) {
39 super(inputStream);
40 }
41
42 /**
43 * Constructs a new instance, which filters the given input stream, and uses the given buffer size.
44 *
45 * @param inputStream The input stream, which is being buffered.
46 * @param bufferSize The size of the {@link CircularByteBuffer}, which is used internally.
47 */
48 public PeekableInputStream(final InputStream inputStream, final int bufferSize) {
49 super(inputStream, bufferSize);
50 }
51
52 /**
53 * Returns whether the next bytes in the buffer are as given by {@code sourceBuffer}. This is equivalent to
54 * {@link #peek(byte[], int, int)} with {@code offset} == 0, and {@code length} == {@code sourceBuffer.length}
55 *
56 * @param sourceBuffer the buffer to compare against
57 * @return true if the next bytes are as given
58 * @throws IOException Refilling the buffer failed.
59 */
60 public boolean peek(final byte[] sourceBuffer) throws IOException {
61 Objects.requireNonNull(sourceBuffer, "sourceBuffer");
62 return peek(sourceBuffer, 0, sourceBuffer.length);
63 }
64
65 /**
66 * Returns whether the next bytes in the buffer are as given by {@code sourceBuffer}, {code offset}, and
67 * {@code length}.
68 *
69 * @param sourceBuffer the buffer to compare against
70 * @param offset the start offset
71 * @param length the length to compare
72 * @return true if the next bytes in the buffer are as given
73 * @throws IOException if there is a problem calling fillBuffer()
74 */
75 public boolean peek(final byte[] sourceBuffer, final int offset, final int length) throws IOException {
76 Objects.requireNonNull(sourceBuffer, "sourceBuffer");
77 if (sourceBuffer.length > bufferSize) {
78 throw new IllegalArgumentException("Peek request size of " + sourceBuffer.length
79 + " bytes exceeds buffer size of " + bufferSize + " bytes");
80 }
81 if (buffer.getCurrentNumberOfBytes() < sourceBuffer.length) {
82 fillBuffer();
83 }
84 return buffer.peek(sourceBuffer, offset, length);
85 }
86 }