All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sap.it.commons.io.CountingInputStream Maven / Gradle / Ivy

The newest version!
package com.sap.it.commons.io;

import java.io.IOException;
import java.io.InputStream;

public class CountingInputStream extends InputStreamDelegator {

    private long readCount = 0;

    private long markedReadCount = 0;

    public CountingInputStream(InputStream inputStream) {
        super(inputStream);
    }

    private int addCounter(int count) {
        if (count != -1) {
            readCount += count;
        }
        return count;
    }

    @Override
    public int read() throws IOException {
        int res = super.read();
        if (res != -1) {
            readCount++;
        }
        return res;
    }

    @Override
    public int read(byte[] cbuf) throws IOException {
        return addCounter(super.read(cbuf));
    }

    @Override
    public int read(byte[] cbuf, int off, int len) throws IOException {
        return addCounter(super.read(cbuf, off, len));
    }

    @Override
    public void mark(int readAheadLimit) {
        super.mark(readAheadLimit);
        markedReadCount = readCount;
    }

    @Override
    public void reset() throws IOException {
        super.reset();
        readCount = markedReadCount;
    }

    public long getCount() {
        return readCount;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy