FileDocCategorySizeDatePackage
CountingOutputStream.javaAPI DocAndroid 1.5 API5494Wed May 06 22:42:46 BST 2009org.apache.commons.io.output

CountingOutputStream

public class CountingOutputStream extends ProxyOutputStream
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 $

Fields Summary
private long
count
The count of bytes that have passed.
Constructors Summary
public CountingOutputStream(OutputStream out)
Constructs a new CountingOutputStream.

param
out the OutputStream to write to

        super(out);
    
Methods Summary
public synchronized longgetByteCount()
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.

return
the number of bytes accumulated
since
Commons IO 1.3

        return this.count;
    
public synchronized intgetCount()
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 {@link #getByteCount()} for a method using a long.

return
the number of bytes accumulated
throws
ArithmeticException if the byte count is too large

        long result = getByteCount();
        if (result > Integer.MAX_VALUE) {
            throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int");
        }
        return (int) result;
    
public synchronized longresetByteCount()
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.

return
the count previous to resetting
since
Commons IO 1.3

        long tmp = this.count;
        this.count = 0;
        return tmp;
    
public synchronized intresetCount()
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 {@link #resetByteCount()} for a method using a long.

return
the count previous to resetting
throws
ArithmeticException if the byte count is too large

        long result = resetByteCount();
        if (result > Integer.MAX_VALUE) {
            throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int");
        }
        return (int) result;
    
public voidwrite(byte[] b)
Writes the contents of the specified byte array to this output stream keeping count of the number of bytes written.

param
b the bytes to write, not null
throws
IOException if an I/O error occurs
see
java.io.OutputStream#write(byte[])

        count += b.length;
        super.write(b);
    
public voidwrite(byte[] b, int off, int len)
Writes a portion of the specified byte array to this output stream keeping count of the number of bytes written.

param
b the bytes to write, not null
param
off the start offset in the buffer
param
len the maximum number of bytes to write
throws
IOException if an I/O error occurs
see
java.io.OutputStream#write(byte[], int, int)

        count += len;
        super.write(b, off, len);
    
public voidwrite(int b)
Writes a single byte to the output stream adding to the count of the number of bytes written.

param
b the byte to write
throws
IOException if an I/O error occurs
see
java.io.OutputStream#write(int)

        count++;
        super.write(b);