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

public Class CountingOutputStream

extends ProxyOutputStream
Class Inheritance
Imports
java.io.IOException, .OutputStream

A decorating output 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 written as expected.

Version
$Id: CountingOutputStream.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
CountingOutputStream(OutputStream
the OutputStream to write to
out
)

Constructs a new CountingOutputStream.

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 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 void
write(byte[]
the bytes to write, not null
b
)

Overrides org.apache.commons.io.output.ProxyOutputStream.write.

Writes the contents of the specified byte array to this output stream keeping count of the number of bytes written.
public void
write(byte[]
the bytes to write, not null
b
,
int
the start offset in the buffer
off
,
int
the maximum number of bytes to write
len
)

Overrides org.apache.commons.io.output.ProxyOutputStream.write.

Writes a portion of the specified byte array to this output stream keeping count of the number of bytes written.
public void
write(int
the byte to write
b
)

Overrides org.apache.commons.io.output.ProxyOutputStream.write.

Writes a single byte to the output stream adding to the count of the number of bytes written.
Inherited from org.apache.commons.io.output.ProxyOutputStream:
closeflush

Field Detail

countback to summary
private long count

The count of bytes that have passed.

Constructor Detail

CountingOutputStreamback to summary
public CountingOutputStream(OutputStream out)

Constructs a new CountingOutputStream.

Parameters
out:OutputStream

the OutputStream to write 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(). It 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
resetByteCountback to summary
public synchronized long resetByteCount()

Set the byte count back to 0.

Note

This method is an alternative for resetCount(). It 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
writeback to summary
public void write(byte[] b) throws IOException

Overrides org.apache.commons.io.output.ProxyOutputStream.write.

Writes the contents of the specified byte array to this output stream keeping count of the number of bytes written.

Parameters
b:byte[]

the bytes to write, not null

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

Overrides org.apache.commons.io.output.ProxyOutputStream.write.

Writes a portion of the specified byte array to this output stream keeping count of the number of bytes written.

Parameters
b:byte[]

the bytes to write, not null

off:int

the start offset in the buffer

len:int

the maximum number of bytes to write

Exceptions
IOException:
if an I/O error occurs
See Also
java.io.OutputStream#write(byte[], int, int)
writeback to summary
public void write(int b) throws IOException

Overrides org.apache.commons.io.output.ProxyOutputStream.write.

Writes a single byte to the output stream adding to the count of the number of bytes written.

Parameters
b:int

the byte to write

Exceptions
IOException:
if an I/O error occurs
See Also
java.io.OutputStream#write(int)