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

com.eg.agent.android.io.CounterOutputStream Maven / Gradle / Ivy

The newest version!
package com.eg.agent.android.io;

import java.io.IOException;
import java.io.OutputStream;

public final class CounterOutputStream extends OutputStream implements StreamListenerSource
{
    private final OutputStream impl;
    private long count = 0L;
    private final StreamListenerList listenerManager = new StreamListenerList();
    public CounterOutputStream(OutputStream impl)
    {
        this.impl = impl;
    }
    public void addStreamListener(StreamListener streamCompleteListener)
    {
        this.listenerManager.addStreamListener(streamCompleteListener);
    }
    public void removeStreamListener(StreamListener streamCompleteListener)
    {
        this.listenerManager.removeStreamListener(streamCompleteListener);
    }
    public long getCount()
    {
        return this.count;
    }
    public void write(int oneByte) throws IOException
    {
        try
        {
            this.impl.write(oneByte);
            ++this.count;
        }
        catch (IOException var3)
        {
            this.notifyStreamError(var3);
            throw var3;
        }
    }
    public void write(byte[] buffer) throws IOException
    {
        try
        {
            this.impl.write(buffer);
            this.count += (long)buffer.length;
        }
        catch (IOException var3)
        {
            this.notifyStreamError(var3);
            throw var3;
        }
    }
    public void write(byte[] buffer, int offset, int count) throws IOException
    {
        try
        {
            this.impl.write(buffer, offset, count);
            this.count += (long)count;
        }
        catch (IOException var5)
        {
            this.notifyStreamError(var5);
            throw var5;
        }
    }
    public void flush() throws IOException
    {
        try
        {
            this.impl.flush();
        }
        catch (IOException var2)
        {
            this.notifyStreamError(var2);
            throw var2;
        }
    }

    public void close() throws IOException {
        try {
            this.impl.close();
            this.notifyStreamComplete();
        } catch (IOException var2) {
            this.notifyStreamError(var2);
            throw var2;
        }
    }
    private void notifyStreamComplete()
    {
        if(!this.listenerManager.isComplete())
        {
            this.listenerManager.notifyStreamComplete(new StreamEvent(this, this.count));
        }
    }
    private void notifyStreamError(Exception e)
    {
        if(!this.listenerManager.isComplete())
        {
            this.listenerManager.notifyStreamError(new StreamEvent(this, this.count, e));
        }

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy