001/**
002 *
003 * Copyright 2003-2007 Jive Software.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smack.util;
018
019import java.io.IOException;
020import java.io.Reader;
021import java.util.ArrayList;
022import java.util.List;
023
024
025/**
026 * An ObservableReader is a wrapper on a Reader that notifies to its listeners when
027 * reading character streams.
028 * 
029 * @author Gaston Dombiak
030 */
031public class ObservableReader extends Reader {
032
033    Reader wrappedReader = null;
034    List<ReaderListener> listeners = new ArrayList<ReaderListener>();
035
036    public ObservableReader(Reader wrappedReader) {
037        this.wrappedReader = wrappedReader;
038    }
039        
040    public int read(char[] cbuf, int off, int len) throws IOException {
041        int count = wrappedReader.read(cbuf, off, len);
042        if (count > 0) {
043            String str = new String(cbuf, off, count);
044            // Notify that a new string has been read
045            ReaderListener[] readerListeners = null;
046            synchronized (listeners) {
047                readerListeners = new ReaderListener[listeners.size()];
048                listeners.toArray(readerListeners);
049            }
050            for (int i = 0; i < readerListeners.length; i++) {
051                readerListeners[i].read(str);
052            }
053        }
054        return count;
055    }
056
057    public void close() throws IOException {
058        wrappedReader.close();
059    }
060
061    public int read() throws IOException {
062        return wrappedReader.read();
063    }
064
065    public int read(char[] cbuf) throws IOException {
066        return wrappedReader.read(cbuf);
067    }
068
069    public long skip(long n) throws IOException {
070        return wrappedReader.skip(n);
071    }
072
073    public boolean ready() throws IOException {
074        return wrappedReader.ready();
075    }
076
077    public boolean markSupported() {
078        return wrappedReader.markSupported();
079    }
080
081    public void mark(int readAheadLimit) throws IOException {
082        wrappedReader.mark(readAheadLimit);
083    }
084
085    public void reset() throws IOException {
086        wrappedReader.reset();
087    }
088
089    /**
090     * Adds a reader listener to this reader that will be notified when
091     * new strings are read.
092     *
093     * @param readerListener a reader listener.
094     */
095    public void addReaderListener(ReaderListener readerListener) {
096        if (readerListener == null) {
097            return;
098        }
099        synchronized (listeners) {
100            if (!listeners.contains(readerListener)) {
101                listeners.add(readerListener);
102            }
103        }
104    }
105
106    /**
107     * Removes a reader listener from this reader.
108     *
109     * @param readerListener a reader listener.
110     */
111    public void removeReaderListener(ReaderListener readerListener) {
112        synchronized (listeners) {
113            listeners.remove(readerListener);
114        }
115    }
116
117}