• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * $RCSfile$
3  * $Revision$
4  * $Date$
5  *
6  * Copyright 2003-2007 Jive Software.
7  *
8  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 package org.jivesoftware.smack.util;
22 
23 import java.io.*;
24 import java.util.*;
25 
26 /**
27  * An ObservableWriter is a wrapper on a Writer that notifies to its listeners when
28  * writing to character streams.
29  *
30  * @author Gaston Dombiak
31  */
32 public class ObservableWriter extends Writer {
33 
34     Writer wrappedWriter = null;
35     List<WriterListener> listeners = new ArrayList<WriterListener>();
36 
ObservableWriter(Writer wrappedWriter)37     public ObservableWriter(Writer wrappedWriter) {
38         this.wrappedWriter = wrappedWriter;
39     }
40 
write(char cbuf[], int off, int len)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 
flush()47     public void flush() throws IOException {
48         wrappedWriter.flush();
49     }
50 
close()51     public void close() throws IOException {
52         wrappedWriter.close();
53     }
54 
write(int c)55     public void write(int c) throws IOException {
56         wrappedWriter.write(c);
57     }
58 
write(char cbuf[])59     public void write(char cbuf[]) throws IOException {
60         wrappedWriter.write(cbuf);
61         String str = new String(cbuf);
62         notifyListeners(str);
63     }
64 
write(String str)65     public void write(String str) throws IOException {
66         wrappedWriter.write(str);
67         notifyListeners(str);
68     }
69 
write(String str, int off, int len)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     /**
77      * Notify that a new string has been written.
78      *
79      * @param str the written String to notify
80      */
notifyListeners(String str)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     /**
93      * Adds a writer listener to this writer that will be notified when
94      * new strings are sent.
95      *
96      * @param writerListener a writer listener.
97      */
addWriterListener(WriterListener writerListener)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     /**
110      * Removes a writer listener from this writer.
111      *
112      * @param writerListener a writer listener.
113      */
removeWriterListener(WriterListener writerListener)114     public void removeWriterListener(WriterListener writerListener) {
115         synchronized (listeners) {
116             listeners.remove(writerListener);
117         }
118     }
119 
120 }
121