/******************************************************************************
* Copyright © 2013-2016 The Nxt Core Developers. *
* *
* See the AUTHORS.txt, DEVELOPER-AGREEMENT.txt and LICENSE.txt files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* Nxt software, including this file, may be copied, modified, propagated, *
* or distributed except according to the terms contained in the LICENSE.txt *
* file. *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/
package nxt.util;
import nxt.NxtException;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
public class CountingInputStream extends FilterInputStream {
private long count;
private final long limit;
public CountingInputStream(InputStream in, long limit) {
super(in);
this.limit = limit;
}
@Override
public int read() throws IOException {
int read = super.read();
if (read >= 0) {
incCount(1);
}
return read;
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
int read = super.read(b, off, len);
if (read >= 0) {
incCount(read);
}
return read;
}
@Override
public long skip(long n) throws IOException {
long skipped = super.skip(n);
if (skipped >= 0) {
incCount(skipped);
}
return skipped;
}
public long getCount() {
return count;
}
private void incCount(long n) throws NxtException.NxtIOException {
count += n;
if (count > limit) {
throw new NxtException.NxtIOException("Maximum size exceeded: " + count);
}
}
}