1 20 21 package org.jivesoftware.smack.util; 22 23 import java.io.*; 24 import java.util.*; 25 26 32 public class ObservableReader extends Reader { 33 34 Reader wrappedReader = null; 35 List listeners = new ArrayList(); 36 37 public ObservableReader(Reader wrappedReader) { 38 this.wrappedReader = wrappedReader; 39 } 40 41 public int read(char[] cbuf, int off, int len) throws IOException { 42 int count = wrappedReader.read(cbuf, off, len); 43 if (count > 0) { 44 String str = new String (cbuf, off, count); 45 ReaderListener[] readerListeners = null; 47 synchronized (listeners) { 48 readerListeners = new ReaderListener[listeners.size()]; 49 listeners.toArray(readerListeners); 50 } 51 for (int i = 0; i < readerListeners.length; i++) { 52 readerListeners[i].read(str); 53 } 54 } 55 return count; 56 } 57 58 public void close() throws IOException { 59 wrappedReader.close(); 60 } 61 62 public int read() throws IOException { 63 return wrappedReader.read(); 64 } 65 66 public int read(char cbuf[]) throws IOException { 67 return wrappedReader.read(cbuf); 68 } 69 70 public long skip(long n) throws IOException { 71 return wrappedReader.skip(n); 72 } 73 74 public boolean ready() throws IOException { 75 return wrappedReader.ready(); 76 } 77 78 public boolean markSupported() { 79 return wrappedReader.markSupported(); 80 } 81 82 public void mark(int readAheadLimit) throws IOException { 83 wrappedReader.mark(readAheadLimit); 84 } 85 86 public void reset() throws IOException { 87 wrappedReader.reset(); 88 } 89 90 96 public void addReaderListener(ReaderListener readerListener) { 97 if (readerListener == null) { 98 return; 99 } 100 synchronized (listeners) { 101 if (!listeners.contains(readerListener)) { 102 listeners.add(readerListener); 103 } 104 } 105 } 106 107 112 public void removeReaderListener(ReaderListener readerListener) { 113 synchronized (listeners) { 114 listeners.remove(readerListener); 115 } 116 } 117 118 } 119 | Popular Tags |