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 private void notifyListeners() { 095 WriterListener[] writerListeners; 096 synchronized (listeners) { 097 writerListeners = new WriterListener[listeners.size()]; 098 listeners.toArray(writerListeners); 099 } 100 String str = stringBuilder.toString(); 101 stringBuilder.setLength(0); 102 for (WriterListener writerListener : writerListeners) { 103 writerListener.write(str); 104 } 105 } 106 107 /** 108 * Adds a writer listener to this writer that will be notified when 109 * new strings are sent. 110 * 111 * @param writerListener a writer listener. 112 */ 113 public void addWriterListener(WriterListener writerListener) { 114 if (writerListener == null) { 115 return; 116 } 117 synchronized (listeners) { 118 if (!listeners.contains(writerListener)) { 119 listeners.add(writerListener); 120 } 121 } 122 } 123 124 /** 125 * Removes a writer listener from this writer. 126 * 127 * @param writerListener a writer listener. 128 */ 129 public void removeWriterListener(WriterListener writerListener) { 130 synchronized (listeners) { 131 listeners.remove(writerListener); 132 } 133 } 134 135}