001/** 002 * 003 * Copyright 2003-2007 Jive Software. 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smack.util; 018 019import java.io.IOException; 020import java.io.Writer; 021import java.util.ArrayList; 022import java.util.List; 023 024/** 025 * An ObservableWriter is a wrapper on a Writer that notifies to its listeners when 026 * writing to character streams. 027 * 028 * @author Gaston Dombiak 029 */ 030public class ObservableWriter extends Writer { 031 private static final int MAX_STRING_BUILDER_SIZE = 4096; 032 033 Writer wrappedWriter = null; 034 final List<WriterListener> listeners = new ArrayList<WriterListener>(); 035 private final StringBuilder stringBuilder = new StringBuilder(MAX_STRING_BUILDER_SIZE); 036 037 public ObservableWriter(Writer wrappedWriter) { 038 this.wrappedWriter = wrappedWriter; 039 } 040 041 @Override 042 public void write(char[] cbuf, int off, int len) throws IOException { 043 wrappedWriter.write(cbuf, off, len); 044 String str = new String(cbuf, off, len); 045 maybeNotifyListeners(str); 046 } 047 048 @Override 049 public void flush() throws IOException { 050 notifyListeners(); 051 wrappedWriter.flush(); 052 } 053 054 @Override 055 public void close() throws IOException { 056 wrappedWriter.close(); 057 } 058 059 @Override 060 public void write(int c) throws IOException { 061 wrappedWriter.write(c); 062 } 063 064 @Override 065 public void write(char[] cbuf) throws IOException { 066 wrappedWriter.write(cbuf); 067 String str = new String(cbuf); 068 maybeNotifyListeners(str); 069 } 070 071 @Override 072 public void write(String str) throws IOException { 073 wrappedWriter.write(str); 074 maybeNotifyListeners(str); 075 } 076 077 @Override 078 public void write(String str, int off, int len) throws IOException { 079 wrappedWriter.write(str, off, len); 080 str = str.substring(off, off + len); 081 maybeNotifyListeners(str); 082 } 083 084 private void maybeNotifyListeners(String s) { 085 stringBuilder.append(s); 086 if (stringBuilder.length() > MAX_STRING_BUILDER_SIZE) { 087 notifyListeners(); 088 } 089 } 090 091 /** 092 * Notify that a new string has been written. 093 * 094 * @param str the written String to notify 095 */ 096 private void notifyListeners() { 097 WriterListener[] writerListeners = null; 098 synchronized (listeners) { 099 writerListeners = new WriterListener[listeners.size()]; 100 listeners.toArray(writerListeners); 101 } 102 String str = stringBuilder.toString(); 103 stringBuilder.setLength(0); 104 for (int i = 0; i < writerListeners.length; i++) { 105 writerListeners[i].write(str); 106 } 107 } 108 109 /** 110 * Adds a writer listener to this writer that will be notified when 111 * new strings are sent. 112 * 113 * @param writerListener a writer listener. 114 */ 115 public void addWriterListener(WriterListener writerListener) { 116 if (writerListener == null) { 117 return; 118 } 119 synchronized (listeners) { 120 if (!listeners.contains(writerListener)) { 121 listeners.add(writerListener); 122 } 123 } 124 } 125 126 /** 127 * Removes a writer listener from this writer. 128 * 129 * @param writerListener a writer listener. 130 */ 131 public void removeWriterListener(WriterListener writerListener) { 132 synchronized (listeners) { 133 listeners.remove(writerListener); 134 } 135 } 136 137}