Top Description Fields Constructors Methods
org.apache.commons.io.input

public Class CountingInputStream

extends ProxyInputStream
Class Inheritance
Imports
java.io.IOException, .InputStream

A decorating input stream that counts the number of bytes that have passed through the stream so far.

A typical use case would be during debugging, to ensure that data is being read as expected.

Author
Marcelo Liberato
Version
$Id: CountingInputStream.java 471628 2006-11-06 04:06:45Z bayard $

Field Summary

Modifier and TypeField and Description
private long
count

The count of bytes that have passed.

Constructor Summary

AccessConstructor and Description
public
CountingInputStream(InputStream
the InputStream to delegate to
in
)

Constructs a new CountingInputStream.

Method Summary

Modifier and TypeMethod and Description
public synchronized long

Returns:

the number of bytes accumulated
getByteCount
()

The number of bytes that have passed through this stream.

public synchronized int

Returns:

the number of bytes accumulated
getCount
()

The number of bytes that have passed through this stream.

public int

Returns:

the total number of bytes read into the buffer, -1 if end of stream
read
(byte[]
the buffer into which the data is read, not null
b
)

Overrides org.apache.commons.io.input.ProxyInputStream.read.

Reads a number of bytes into the byte array, keeping count of the number read.
public int

Returns:

the total number of bytes read into the buffer, -1 if end of stream
read
(byte[]
the buffer into which the data is read, not null
b
,
int
the start offset in the buffer
off
,
int
the maximum number of bytes to read
len
)

Overrides org.apache.commons.io.input.ProxyInputStream.read.

Reads a number of bytes into the byte array at a specific offset, keeping count of the number read.
public int

Returns:

the byte read, -1 if end of stream
read
()

Overrides org.apache.commons.io.input.ProxyInputStream.read.

Reads the next byte of data adding to the count of bytes received if a byte is successfully read.
public synchronized long

Returns:

the count previous to resetting
resetByteCount
()

Set the byte count back to 0.

public synchronized int

Returns:

the count previous to resetting
resetCount
()

Set the byte count back to 0.

public long

Returns:

the actual number of bytes skipped
skip
(final long
the number of bytes to skip
length
)

Overrides org.apache.commons.io.input.ProxyInputStream.skip.

Skips the stream over the specified number of bytes, adding the skipped amount to the count.
Inherited from org.apache.commons.io.input.ProxyInputStream:
availableclosemarkmarkSupportedreset

Field Detail

countback to summary
private long count

The count of bytes that have passed.

Constructor Detail

CountingInputStreamback to summary
public CountingInputStream(InputStream in)

Constructs a new CountingInputStream.

Parameters
in:InputStream

the InputStream to delegate to

Method Detail

getByteCountback to summary
public synchronized long getByteCount()

The number of bytes that have passed through this stream.

Note

This method is an alternative for getCount() and was added because that method returns an integer which will result in incorrect count for files over 2GB.

Returns:long

the number of bytes accumulated

Since
Commons IO 1.3
getCountback to summary
public synchronized int getCount()

The number of bytes that have passed through this stream.

Note

From v1.3 this method throws an ArithmeticException if the count is greater than can be expressed by an int. See getByteCount() for a method using a long.

Returns:int

the number of bytes accumulated

Exceptions
ArithmeticException:
if the byte count is too large
readback to summary
public int read(byte[] b) throws IOException

Overrides org.apache.commons.io.input.ProxyInputStream.read.

Reads a number of bytes into the byte array, keeping count of the number read.

Parameters
b:byte[]

the buffer into which the data is read, not null

Returns:int

the total number of bytes read into the buffer, -1 if end of stream

Exceptions
IOException:
if an I/O error occurs
See Also
java.io.InputStream#read(byte[])
readback to summary
public int read(byte[] b, int off, int len) throws IOException

Overrides org.apache.commons.io.input.ProxyInputStream.read.

Reads a number of bytes into the byte array at a specific offset, keeping count of the number read.

Parameters
b:byte[]

the buffer into which the data is read, not null

off:int

the start offset in the buffer

len:int

the maximum number of bytes to read

Returns:int

the total number of bytes read into the buffer, -1 if end of stream

Exceptions
IOException:
if an I/O error occurs
See Also
java.io.InputStream#read(byte[], int, int)
readback to summary
public int read() throws IOException

Overrides org.apache.commons.io.input.ProxyInputStream.read.

Reads the next byte of data adding to the count of bytes received if a byte is successfully read.

Returns:int

the byte read, -1 if end of stream

Exceptions
IOException:
if an I/O error occurs
See Also
java.io.InputStream#read()
resetByteCountback to summary
public synchronized long resetByteCount()

Set the byte count back to 0.

Note

This method is an alternative for resetCount() and was added because that method returns an integer which will result in incorrect count for files over 2GB.

Returns:long

the count previous to resetting

Since
Commons IO 1.3
resetCountback to summary
public synchronized int resetCount()

Set the byte count back to 0.

Note

From v1.3 this method throws an ArithmeticException if the count is greater than can be expressed by an int. See resetByteCount() for a method using a long.

Returns:int

the count previous to resetting

Exceptions
ArithmeticException:
if the byte count is too large
skipback to summary
public long skip(final long length) throws IOException

Overrides org.apache.commons.io.input.ProxyInputStream.skip.

Skips the stream over the specified number of bytes, adding the skipped amount to the count.

Parameters
length:long

the number of bytes to skip

Returns:long

the actual number of bytes skipped

Exceptions
IOException:
if an I/O error occurs
See Also
java.io.InputStream#skip(long)