/*
 * Decompiled with CFR 0.152.
 */
package org.jivesoftware.smack.util;

import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import org.jivesoftware.smack.util.ReaderListener;

public class ObservableReader
extends Reader {
    Reader wrappedReader = null;
    List<ReaderListener> listeners = new ArrayList<ReaderListener>();

    public ObservableReader(Reader wrappedReader) {
        this.wrappedReader = wrappedReader;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    @Override
    public int read(char[] cbuf, int off, int len) throws IOException {
        int count = this.wrappedReader.read(cbuf, off, len);
        if (count > 0) {
            String str = new String(cbuf, off, count);
            ReaderListener[] readerListeners = null;
            List<ReaderListener> list = this.listeners;
            synchronized (list) {
                readerListeners = new ReaderListener[this.listeners.size()];
                this.listeners.toArray(readerListeners);
            }
            for (int i = 0; i < readerListeners.length; ++i) {
                readerListeners[i].read(str);
            }
        }
        return count;
    }

    @Override
    public void close() throws IOException {
        this.wrappedReader.close();
    }

    @Override
    public int read() throws IOException {
        return this.wrappedReader.read();
    }

    @Override
    public int read(char[] cbuf) throws IOException {
        return this.wrappedReader.read(cbuf);
    }

    @Override
    public long skip(long n) throws IOException {
        return this.wrappedReader.skip(n);
    }

    @Override
    public boolean ready() throws IOException {
        return this.wrappedReader.ready();
    }

    @Override
    public boolean markSupported() {
        return this.wrappedReader.markSupported();
    }

    @Override
    public void mark(int readAheadLimit) throws IOException {
        this.wrappedReader.mark(readAheadLimit);
    }

    @Override
    public void reset() throws IOException {
        this.wrappedReader.reset();
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public void addReaderListener(ReaderListener readerListener) {
        if (readerListener == null) {
            return;
        }
        List<ReaderListener> list = this.listeners;
        synchronized (list) {
            if (!this.listeners.contains(readerListener)) {
                this.listeners.add(readerListener);
            }
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public void removeReaderListener(ReaderListener readerListener) {
        List<ReaderListener> list = this.listeners;
        synchronized (list) {
            this.listeners.remove(readerListener);
        }
    }
}
