All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fasterxml.clustermate.service.util.StatsCollectingOutputStream Maven / Gradle / Ivy

There is a newer version: 0.10.5
Show newest version
package com.fasterxml.clustermate.service.util;

import java.io.*;

public class StatsCollectingOutputStream extends OutputStream
{
    protected final OutputStream _out;

    protected long _bytesWritten;

    protected boolean _closed;

    public StatsCollectingOutputStream(OutputStream out) {
        _out = out;
    }

    public long getBytesWritten() {
        return _bytesWritten;
    }

    @Override
    public void close() throws IOException
    {
        _closed = true;
        _out.close();
    }

    @Override
    public void flush() throws IOException
    {
        _out.flush();
    }

    @Override
    public final void write(byte[] b) throws IOException {
        write(b, 0, b.length);
    }

    @Override
    public void write(byte[] b, int off, int len) throws IOException
    {
        _checkClosed();
        _out.write(b, off, len);
        _bytesWritten += len;
    }

    @Override
    public void write(int b) throws IOException
    {
        _checkClosed();
        _out.write(b);
        ++_bytesWritten;
    }


    private final void _checkClosed() throws IOException {
        if (_closed) {
            throw new IOException("Can not write to "+getClass().getName()+" after close() ("
                    +_bytesWritten+" bytes written)");
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy