1 20 21 package org.jivesoftware.smack.util; 22 23 import java.io.*; 24 import java.util.*; 25 26 32 public class ObservableWriter extends Writer { 33 34 Writer wrappedWriter = null; 35 List listeners = new ArrayList(); 36 37 public ObservableWriter(Writer wrappedWriter) { 38 this.wrappedWriter = wrappedWriter; 39 } 40 41 public void write(char cbuf[], int off, int len) throws IOException { 42 wrappedWriter.write(cbuf, off, len); 43 String str = new String (cbuf, off, len); 44 notifyListeners(str); 45 } 46 47 public void flush() throws IOException { 48 wrappedWriter.flush(); 49 } 50 51 public void close() throws IOException { 52 wrappedWriter.close(); 53 } 54 55 public void write(int c) throws IOException { 56 wrappedWriter.write(c); 57 } 58 59 public void write(char cbuf[]) throws IOException { 60 wrappedWriter.write(cbuf); 61 String str = new String (cbuf); 62 notifyListeners(str); 63 } 64 65 public void write(String str) throws IOException { 66 wrappedWriter.write(str); 67 notifyListeners(str); 68 } 69 70 public void write(String str, int off, int len) throws IOException { 71 wrappedWriter.write(str, off, len); 72 str = str.substring(off, off + len); 73 notifyListeners(str); 74 } 75 76 81 private void notifyListeners(String str) { 82 WriterListener[] writerListeners = null; 83 synchronized (listeners) { 84 writerListeners = new WriterListener[listeners.size()]; 85 listeners.toArray(writerListeners); 86 } 87 for (int i = 0; i < writerListeners.length; i++) { 88 writerListeners[i].write(str); 89 } 90 } 91 92 98 public void addWriterListener(WriterListener writerListener) { 99 if (writerListener == null) { 100 return; 101 } 102 synchronized (listeners) { 103 if (!listeners.contains(writerListener)) { 104 listeners.add(writerListener); 105 } 106 } 107 } 108 109 114 public void removeWriterListener(WriterListener writerListener) { 115 synchronized (listeners) { 116 listeners.remove(writerListener); 117 } 118 } 119 120 } 121 | Popular Tags |