org.entityfs.util.io
Class CountingDataSource

java.lang.Object
  extended by org.entityfs.util.io.CountingDataSource
All Implemented Interfaces:
DataSource

public class CountingDataSource
extends Object
implements DataSource

This DataSource counts how many bytes that are read or skipped past from a proxied data source.

Since:
1.1
Author:
Karl Gustafsson

Constructor Summary
CountingDataSource(DataSource proxied)
           
 
Method Summary
 int available()
          Returns an estimate of the number of bytes that can be read from this data source without blocking by the next invocation of a method for this data source.
 void close()
          Close the source and free all resources associated with it.
 long getCount()
          Get the number of bytes read or skipped past in this data source.
 int read()
          Read a byte of data from the source.
 int read(byte[] barr)
          Read up to b.length bytes of data from the source into the array.
 int read(byte[] barr, int off, int len)
          Read up to len bytes of data from the source into the array.
 long skipBytes(long n)
          Attempt to skip over n bytes in the source.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CountingDataSource

public CountingDataSource(DataSource proxied)
Method Detail

available

public int available()
              throws IllegalStateException,
                     WrappedIOException
Description copied from interface: DataSource
Returns an estimate of the number of bytes that can be read from this data source without blocking by the next invocation of a method for this data source.

Specified by:
available in interface DataSource
Returns:
An estimate of the number of bytes that can be read without blocking.
Throws:
IllegalStateException - If the source is closed.
WrappedIOException - On I/O errors.
See Also:
InputStream.available()

read

public int read()
         throws IllegalStateException,
                WrappedIOException
Description copied from interface: DataSource
Read a byte of data from the source. The byte is returned as an integer in the range 0 to 255 (0x00-0x0ff).

Specified by:
read in interface DataSource
Returns:
The next byte of data, or -1 if the end of the source has been reached.
Throws:
IllegalStateException - If the source is closed.
WrappedIOException - On I/O errors.

read

public int read(byte[] barr)
         throws IllegalStateException,
                WrappedIOException
Description copied from interface: DataSource
Read up to b.length bytes of data from the source into the array.

Specified by:
read in interface DataSource
Parameters:
barr - The byte array into which data is read.
Returns:
The total number of bytes read into the array, or -1 if no data could be read because the the end of the source was reached before the read started.
Throws:
IllegalStateException - If the source is closed.
WrappedIOException - On I/O errors.

read

public int read(byte[] barr,
                int off,
                int len)
         throws IllegalStateException,
                WrappedIOException
Description copied from interface: DataSource
Read up to len bytes of data from the source into the array.

Specified by:
read in interface DataSource
Parameters:
barr - The byte array into which data is read.
off - The start offset in the array b at which data is written.
len - The maximum number of bytes to read.
Returns:
The total number of bytes read into the array, or -1 if no data could be read because the end of the source was reached before the read started.
Throws:
IllegalStateException - If the source is closed.
WrappedIOException - On I/O errors.

skipBytes

public long skipBytes(long n)
               throws IllegalStateException
Description copied from interface: DataSource
Attempt to skip over n bytes in the source. If n is negative, no bytes are skipped.

The pointer in the source cannot be moved beyond EOF using this method (unlike RandomAccess.seek(long)).

Specified by:
skipBytes in interface DataSource
Parameters:
n - The number of bytes to increment the source pointer with.
Returns:
The number of bytes actually skipped.
Throws:
IllegalStateException - If the source is closed.
See Also:
RandomAccess.seek(long)

close

public void close()
           throws WrappedIOException
Description copied from interface: DataSource
Close the source and free all resources associated with it. If the object is already closed, this method does nothing.

Specified by:
close in interface DataSource
Throws:
WrappedIOException - On I/O errors.

getCount

public long getCount()
Get the number of bytes read or skipped past in this data source.

Returns:
The number of bytes read or skipped past.