FileDocCategorySizeDatePackage
CountingInputStream.javaAPI DocJaudiotagger 2.0.42729Wed Mar 30 16:11:50 BST 2011org.jaudiotagger.audio.asf.io

CountingInputStream

public class CountingInputStream extends FilterInputStream
This implementation of {@link FilterInputStream} counts each read byte.
So at each time, with {@link #getReadCount()} one can determine how many bytes have been read, by this classes read and skip methods (mark and reset are also taken into account).
author
Christian Laireiter

Fields Summary
private long
markPos
If {@link #mark(int)} has been called, the current value of {@link #readCount} is stored, in order to reset it upon {@link #reset()}.
private long
readCount
The amount of read or skipped bytes.
Constructors Summary
public CountingInputStream(InputStream stream)
Creates an instance, which delegates the commands to the given stream.

param
stream stream to actually work with.

        super(stream);
        this.markPos = 0;
        this.readCount = 0;
    
Methods Summary
private synchronized voidbytesRead(long amountRead)
Counts the given amount of bytes.

param
amountRead number of bytes to increase.

        if (amountRead >= 0)
        {
            this.readCount += amountRead;
        }
    
public synchronized longgetReadCount()

return
the readCount

        return this.readCount;
    
public synchronized voidmark(int readlimit)
{@inheritDoc}

        super.mark(readlimit);
        this.markPos = this.readCount;
    
public intread()
{@inheritDoc}

        final int result = super.read();
        bytesRead(1);
        return result;
    
public intread(byte[] destination, int off, int len)
{@inheritDoc}

        final int result = super.read(destination, off, len);
        bytesRead(result);
        return result;
    
public synchronized voidreset()
{@inheritDoc}

        super.reset();
        synchronized (this) {
            this.readCount = this.markPos;
        }
    
public longskip(long amount)
{@inheritDoc}

        final long skipped = super.skip(amount);
        bytesRead(skipped);
        return skipped;