BoundedReader.java
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.io.input;
import static org.apache.commons.io.IOUtils.EOF;
import java.io.IOException;
import java.io.Reader;
import org.apache.commons.io.IOUtils;
/**
* A reader that imposes a limit to the number of characters that can be read from an underlying reader, returning EOF
* when this limit is reached, regardless of state of underlying reader.
*
* <p>
* One use case is to avoid overrunning the read-ahead limit supplied to {@link Reader#mark(int)}, since reading too many characters may invalidate the mark.
* </p>
*
* @since 2.5
*/
public class BoundedReader extends ProxyReader {
private static final int INVALID = -1;
private int charsRead;
private int markedAt = INVALID;
private int readAheadLimit;
private final int maxCharsFromTargetReader;
/**
* Constructs a bounded reader.
*
* @param target The target stream that will be used.
* @param maxCharsFromTargetReader The maximum number of characters that can be read from target.
*/
public BoundedReader(final Reader target, final int maxCharsFromTargetReader) {
super(target);
this.maxCharsFromTargetReader = maxCharsFromTargetReader;
}
/**
* Marks the target reader at its current position.
*
* <p>
* The read-ahead limit is measured from the marked position. The overall character limit supplied to the constructor still applies.
* </p>
*
* @param readAheadLimit The number of characters allowed to be read from the marked position, subject to the overall character limit.
* @throws IOException If an I/O error occurs while calling the underlying reader's mark method.
* @see Reader#mark(int)
*/
@Override
public void mark(final int readAheadLimit) throws IOException {
this.readAheadLimit = readAheadLimit;
markedAt = charsRead;
super.mark(readAheadLimit);
}
/**
* Reads a single character.
*
* @return -1 on EOF or the character read.
* @throws IOException If an I/O error occurs while calling the underlying reader's read method.
* @see Reader#read()
*/
@Override
public int read() throws IOException {
if (charsRead >= maxCharsFromTargetReader || markedAt >= 0 && charsRead - markedAt >= readAheadLimit) {
return EOF;
}
charsRead++;
return super.read();
}
/**
* Reads into an array.
*
* @param cbuf The buffer to fill.
* @param off The offset.
* @param len The number of chars to read.
* @return The number of chars read.
* @throws NullPointerException if the buffer is {@code null}.
* @throws IndexOutOfBoundsException if {@code off} or {@code len} are negative, or if {@code off + len} is greater than {@code cbuf.length}.
* @throws IOException If an I/O error occurs while calling the underlying reader's read method.
* @see Reader#read(char[], int, int)
*/
@Override
public int read(final char[] cbuf, final int off, final int len) throws IOException {
IOUtils.checkFromIndexSize(cbuf, off, len);
int c;
for (int i = 0; i < len; i++) {
c = read();
if (c == EOF) {
return i == 0 ? EOF : i;
}
cbuf[off + i] = (char) c;
}
return len;
}
/**
* Resets the target to the latest mark.
*
* @throws IOException If an I/O error occurs while calling the underlying reader's reset method.
* @see Reader#reset()
*/
@Override
public void reset() throws IOException {
charsRead = markedAt;
super.reset();
}
/**
* Skips characters in the target reader.
*
* <p>
* Positive requests are limited to the remaining overall character limit supplied to the constructor. The read-ahead limit set by {@link #mark(int)}
* does not restrict skipping, so a skip may invalidate the target reader's mark.
* </p>
*
* <p>
* Zero and negative requests are passed directly to the target reader.
* </p>
*
* @param n The number of characters to skip.
* @return The actual number of characters skipped, or 0 if no characters were skipped.
* @throws IOException If an I/O error occurs while calling the underlying reader's skip method.
* @see Reader#skip(long)
*/
@Override
public long skip(final long n) throws IOException {
if (n <= 0) {
return super.skip(n);
}
final int remaining = maxCharsFromTargetReader - charsRead;
if (remaining <= 0) {
return 0;
}
final long skipped = super.skip(Math.min(n, remaining));
charsRead += (int) skipped;
return skipped;
}
}