ObservableReader.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.Reader;
  20. import java.util.ArrayList;
  21. import java.util.List;


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

  29.     Reader wrappedReader = null;
  30.     List<ReaderListener> listeners = new ArrayList<ReaderListener>();

  31.     public ObservableReader(Reader wrappedReader) {
  32.         this.wrappedReader = wrappedReader;
  33.     }
  34.        
  35.     public int read(char[] cbuf, int off, int len) throws IOException {
  36.         int count = wrappedReader.read(cbuf, off, len);
  37.         if (count > 0) {
  38.             String str = new String(cbuf, off, count);
  39.             // Notify that a new string has been read
  40.             ReaderListener[] readerListeners = null;
  41.             synchronized (listeners) {
  42.                 readerListeners = new ReaderListener[listeners.size()];
  43.                 listeners.toArray(readerListeners);
  44.             }
  45.             for (int i = 0; i < readerListeners.length; i++) {
  46.                 readerListeners[i].read(str);
  47.             }
  48.         }
  49.         return count;
  50.     }

  51.     public void close() throws IOException {
  52.         wrappedReader.close();
  53.     }

  54.     public int read() throws IOException {
  55.         return wrappedReader.read();
  56.     }

  57.     public int read(char[] cbuf) throws IOException {
  58.         return wrappedReader.read(cbuf);
  59.     }

  60.     public long skip(long n) throws IOException {
  61.         return wrappedReader.skip(n);
  62.     }

  63.     public boolean ready() throws IOException {
  64.         return wrappedReader.ready();
  65.     }

  66.     public boolean markSupported() {
  67.         return wrappedReader.markSupported();
  68.     }

  69.     public void mark(int readAheadLimit) throws IOException {
  70.         wrappedReader.mark(readAheadLimit);
  71.     }

  72.     public void reset() throws IOException {
  73.         wrappedReader.reset();
  74.     }

  75.     /**
  76.      * Adds a reader listener to this reader that will be notified when
  77.      * new strings are read.
  78.      *
  79.      * @param readerListener a reader listener.
  80.      */
  81.     public void addReaderListener(ReaderListener readerListener) {
  82.         if (readerListener == null) {
  83.             return;
  84.         }
  85.         synchronized (listeners) {
  86.             if (!listeners.contains(readerListener)) {
  87.                 listeners.add(readerListener);
  88.             }
  89.         }
  90.     }

  91.     /**
  92.      * Removes a reader listener from this reader.
  93.      *
  94.      * @param readerListener a reader listener.
  95.      */
  96.     public void removeReaderListener(ReaderListener readerListener) {
  97.         synchronized (listeners) {
  98.             listeners.remove(readerListener);
  99.         }
  100.     }

  101. }