1 23 24 package org.enhydra.kelp.common; 25 26 import org.enhydra.kelp.common.event.WriteEvent; 28 import org.enhydra.kelp.common.event.WriteListener; 29 30 import java.io.StringWriter ; 32 import java.util.ArrayList ; 33 import java.util.Arrays ; 34 35 36 37 41 public class Writer extends StringWriter { 42 private WriteListener[] writeListeners = new WriteListener[0]; 43 private boolean buffered = false; 44 private boolean newLine = false; 45 private StringBuffer lineBuffer = new StringBuffer (); 46 47 51 public Writer() {} 52 53 public void setLineBuffered(boolean b) { 54 buffered = b; 55 } 56 57 public boolean isLineBuffered() { 58 return buffered; 59 } 60 61 67 public synchronized void write(int c) { 68 char[] ca = new char[1]; 69 70 ca[0] = (char) c; 71 String cs = new String (ca); 72 73 write(cs); 74 } 75 76 82 public synchronized void write(String str) { 83 84 write(str, 0, str.length()); 90 91 } 93 94 100 public synchronized void writeln(String str) { 101 newLine = true; 102 write(str + '\n'); 103 newLine = false; 104 } 105 106 114 public synchronized void write(String str, int off, int len) { 115 String out = str.substring(off, (off + len)); 116 boolean writeLine = false; 117 118 if (isLineBuffered()) { 119 if (out.length() > 1) { 120 char end1 = out.charAt(out.length() - 2); 121 char end2 = out.charAt(out.length() - 1); 122 123 if ((int) end1 == 13 && (int) end2 == 10) { 124 if (out.length() < 3) { 125 out = new String (); 126 } else { 127 out = out.substring(0, out.length() - 3); 128 } 129 writeLine = true; 130 } 131 if ((out.length() > 0) 132 && (out.charAt(out.length() - 1) == '\n')) { 133 writeLine = true; 134 } 135 } 136 if (writeLine) { 137 notifyWriteListeners(lineBuffer.toString() + out); 138 lineBuffer = new StringBuffer (); 139 } else { 140 lineBuffer.append(out); 141 } 142 } else { 143 notifyWriteListeners(out); 144 } 145 } 146 147 149 155 public synchronized void addWriteListener(WriteListener l) { 156 ArrayList list = null; 157 list = new ArrayList (Arrays.asList(writeListeners)); 158 if (! list.contains(l)) { 159 list.add(l); 160 } 161 list.trimToSize(); 162 writeListeners = new WriteListener[list.size()]; 163 writeListeners = (WriteListener[]) list.toArray(writeListeners); 164 list.clear(); 165 } 166 167 173 public synchronized void removeWriteListener(WriteListener l) { 174 ArrayList list = null; 175 list = new ArrayList (Arrays.asList(writeListeners)); 176 if (list.contains(l)) { 177 list.remove(l); 178 } 179 list.trimToSize(); 180 writeListeners = new WriteListener[list.size()]; 181 writeListeners = (WriteListener[]) list.toArray(writeListeners); 182 list.clear(); 183 } 184 185 public synchronized WriteListener[] getWriteListeners() { 186 return writeListeners; 187 } 188 189 195 private synchronized void notifyWriteListeners(String s) { 196 WriteEvent event = new WriteEvent(this, WriteEvent.OUTPUT, s); 197 198 for (int i = 0; i < writeListeners.length; i++) { 199 writeListeners[i].onWrite(event); 200 } 201 } 202 203 } 204 | Popular Tags |