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.Writer;
021import java.util.ArrayList;
022import java.util.List;
023
024/**
025 * An ObservableWriter is a wrapper on a Writer that notifies to its listeners when
026 * writing to character streams.
027 * 
028 * @author Gaston Dombiak
029 */
030public class ObservableWriter extends Writer {
031
032    Writer wrappedWriter = null;
033    List<WriterListener> listeners = new ArrayList<WriterListener>();
034
035    public ObservableWriter(Writer wrappedWriter) {
036        this.wrappedWriter = wrappedWriter;
037    }
038
039    public void write(char[] cbuf, int off, int len) throws IOException {
040        wrappedWriter.write(cbuf, off, len);
041        String str = new String(cbuf, off, len);
042        notifyListeners(str);
043    }
044
045    public void flush() throws IOException {
046        wrappedWriter.flush();
047    }
048
049    public void close() throws IOException {
050        wrappedWriter.close();
051    }
052
053    public void write(int c) throws IOException {
054        wrappedWriter.write(c);
055    }
056
057    public void write(char[] cbuf) throws IOException {
058        wrappedWriter.write(cbuf);
059        String str = new String(cbuf);
060        notifyListeners(str);
061    }
062
063    public void write(String str) throws IOException {
064        wrappedWriter.write(str);
065        notifyListeners(str);
066    }
067
068    public void write(String str, int off, int len) throws IOException {
069        wrappedWriter.write(str, off, len);
070        str = str.substring(off, off + len);
071        notifyListeners(str);
072    }
073
074    /**
075     * Notify that a new string has been written.
076     * 
077     * @param str the written String to notify 
078     */
079    private void notifyListeners(String str) {
080        WriterListener[] writerListeners = null;
081        synchronized (listeners) {
082            writerListeners = new WriterListener[listeners.size()];
083            listeners.toArray(writerListeners);
084        }
085        for (int i = 0; i < writerListeners.length; i++) {
086            writerListeners[i].write(str);
087        }
088    }
089
090    /**
091     * Adds a writer listener to this writer that will be notified when
092     * new strings are sent.
093     *
094     * @param writerListener a writer listener.
095     */
096    public void addWriterListener(WriterListener writerListener) {
097        if (writerListener == null) {
098            return;
099        }
100        synchronized (listeners) {
101            if (!listeners.contains(writerListener)) {
102                listeners.add(writerListener);
103            }
104        }
105    }
106
107    /**
108     * Removes a writer listener from this writer.
109     *
110     * @param writerListener a writer listener.
111     */
112    public void removeWriterListener(WriterListener writerListener) {
113        synchronized (listeners) {
114            listeners.remove(writerListener);
115        }
116    }
117
118}