ObservableWriter.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smack.util;

  18. import java.io.IOException;
  19. import java.io.Writer;
  20. import java.util.ArrayList;
  21. import java.util.List;

  22. /**
  23.  * An ObservableWriter is a wrapper on a Writer that notifies to its listeners when
  24.  * writing to character streams.
  25.  *
  26.  * @author Gaston Dombiak
  27.  */
  28. public class ObservableWriter extends Writer {

  29.     Writer wrappedWriter = null;
  30.     List<WriterListener> listeners = new ArrayList<WriterListener>();

  31.     public ObservableWriter(Writer wrappedWriter) {
  32.         this.wrappedWriter = wrappedWriter;
  33.     }

  34.     public void write(char[] cbuf, int off, int len) throws IOException {
  35.         wrappedWriter.write(cbuf, off, len);
  36.         String str = new String(cbuf, off, len);
  37.         notifyListeners(str);
  38.     }

  39.     public void flush() throws IOException {
  40.         wrappedWriter.flush();
  41.     }

  42.     public void close() throws IOException {
  43.         wrappedWriter.close();
  44.     }

  45.     public void write(int c) throws IOException {
  46.         wrappedWriter.write(c);
  47.     }

  48.     public void write(char[] cbuf) throws IOException {
  49.         wrappedWriter.write(cbuf);
  50.         String str = new String(cbuf);
  51.         notifyListeners(str);
  52.     }

  53.     public void write(String str) throws IOException {
  54.         wrappedWriter.write(str);
  55.         notifyListeners(str);
  56.     }

  57.     public void write(String str, int off, int len) throws IOException {
  58.         wrappedWriter.write(str, off, len);
  59.         str = str.substring(off, off + len);
  60.         notifyListeners(str);
  61.     }

  62.     /**
  63.      * Notify that a new string has been written.
  64.      *
  65.      * @param str the written String to notify
  66.      */
  67.     private void notifyListeners(String str) {
  68.         WriterListener[] writerListeners = null;
  69.         synchronized (listeners) {
  70.             writerListeners = new WriterListener[listeners.size()];
  71.             listeners.toArray(writerListeners);
  72.         }
  73.         for (int i = 0; i < writerListeners.length; i++) {
  74.             writerListeners[i].write(str);
  75.         }
  76.     }

  77.     /**
  78.      * Adds a writer listener to this writer that will be notified when
  79.      * new strings are sent.
  80.      *
  81.      * @param writerListener a writer listener.
  82.      */
  83.     public void addWriterListener(WriterListener writerListener) {
  84.         if (writerListener == null) {
  85.             return;
  86.         }
  87.         synchronized (listeners) {
  88.             if (!listeners.contains(writerListener)) {
  89.                 listeners.add(writerListener);
  90.             }
  91.         }
  92.     }

  93.     /**
  94.      * Removes a writer listener from this writer.
  95.      *
  96.      * @param writerListener a writer listener.
  97.      */
  98.     public void removeWriterListener(WriterListener writerListener) {
  99.         synchronized (listeners) {
  100.             listeners.remove(writerListener);
  101.         }
  102.     }

  103. }