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