com.mucommander.io
Class CounterInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by com.mucommander.io.CounterInputStream
All Implemented Interfaces:
java.io.Closeable

public class CounterInputStream
extends java.io.InputStream

An InputStream that keeps track of the number of bytes that have been read from it. Bytes that are skipped (using skip(long) are by default accounted for, setCountSkippedBytes(boolean) can be used to change this.

The actual number of bytes can be retrieved from the ByteCounter instance returned by getCounter(). The CounterInputStream(InputStream, ByteCounter) constructor can be used to specify an existing ByteCounter instance instead of creating a new one. The ByteCounter will always remain accessible, even after this stream has been closed.

Author:
Maxence Bernard
See Also:
ByteCounter

Constructor Summary
CounterInputStream(java.io.InputStream in)
          Creates a new CounterInputStream using the specified InputStream.
CounterInputStream(java.io.InputStream in, ByteCounter counter)
          Creates a new CounterInputStream using the specified InputStream and ByteCounter.
 
Method Summary
 int available()
           
 void close()
           
 ByteCounter getCounter()
          Returns the ByteCounter that holds the number of bytes that have been read (and optionally skipped) from this InputStream.
 boolean getCountSkippedBytes()
          Returns true if skipped bytes (using skip(long) are accounted for.
 void mark(int readLimit)
           
 boolean markSupported()
           
 int read()
           
 int read(byte[] b)
           
 int read(byte[] b, int off, int len)
           
 void reset()
           
 void setCountSkippedBytes(boolean countSkippedBytes)
          Specifies whether or not skipped bytes (using skip(long) should be accounted for.
 long skip(long n)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CounterInputStream

public CounterInputStream(java.io.InputStream in)
Creates a new CounterInputStream using the specified InputStream. A new ByteCounter will be created.

Parameters:
in - the underlying InputStream the data will be read from

CounterInputStream

public CounterInputStream(java.io.InputStream in,
                          ByteCounter counter)
Creates a new CounterInputStream using the specified InputStream and ByteCounter. The provided ByteCounter will NOT be reset, whatever value it contains will be kept.

Parameters:
in - the underlying InputStream the data will be read from
Method Detail

getCounter

public ByteCounter getCounter()
Returns the ByteCounter that holds the number of bytes that have been read (and optionally skipped) from this InputStream.


setCountSkippedBytes

public void setCountSkippedBytes(boolean countSkippedBytes)
Specifies whether or not skipped bytes (using skip(long) should be accounted for. This is by default enabled, bytes that are skipped are added to the ByteCounter.

Parameters:
countSkippedBytes - if true, skipped bytes will be accounted for, the ByteCounter will be increased by the number of skipped bytes

getCountSkippedBytes

public boolean getCountSkippedBytes()
Returns true if skipped bytes (using skip(long) are accounted for. This is by default enabled, bytes that are skipped are added to the ByteCounter.


read

public int read()
         throws java.io.IOException
Specified by:
read in class java.io.InputStream
Throws:
java.io.IOException

read

public int read(byte[] b)
         throws java.io.IOException
Overrides:
read in class java.io.InputStream
Throws:
java.io.IOException

read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException
Overrides:
read in class java.io.InputStream
Throws:
java.io.IOException

skip

public long skip(long n)
          throws java.io.IOException
Overrides:
skip in class java.io.InputStream
Throws:
java.io.IOException

available

public int available()
              throws java.io.IOException
Overrides:
available in class java.io.InputStream
Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.InputStream
Throws:
java.io.IOException

mark

public void mark(int readLimit)
Overrides:
mark in class java.io.InputStream

markSupported

public boolean markSupported()
Overrides:
markSupported in class java.io.InputStream

reset

public void reset()
           throws java.io.IOException
Overrides:
reset in class java.io.InputStream
Throws:
java.io.IOException


This file is part of muCommander - Copyright (C) 2002-2008 Maxence Bernard