Quadcap Embeddable Server

com/quadcap/io/TeeWriter.java

Go to the documentation of this file.
00001 package com.quadcap.io; 00002 00003 /* Copyright 2002 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.IOException; 00042 import java.io.Writer; 00043 00044 import java.util.HashMap; 00045 import java.util.Iterator; 00046 00047 /** 00048 * 00049 * @author Stan Bailes 00050 */ 00051 public class TeeWriter extends Writer { 00052 class WriterBinding { 00053 Writer w; 00054 boolean passFlush = true; 00055 boolean passClose = true; 00056 boolean autoFlush = false; 00057 boolean enabled = true; 00058 WriterBinding(Writer w) { this.w = w; } 00059 } 00060 00061 HashMap writers = new HashMap(); 00062 00063 /** 00064 * Public constructor 00065 */ 00066 public TeeWriter() {} 00067 00068 public TeeWriter(Writer w) { addWriter("", w); } 00069 00070 /** 00071 * Write some characters to the buffer and the downstream writer 00072 */ 00073 public void write(char[] buf, int off, int len) throws IOException { 00074 Iterator iter = writers.values().iterator(); 00075 while (iter.hasNext()) { 00076 WriterBinding wb = (WriterBinding)iter.next(); 00077 if (wb.enabled) { 00078 wb.w.write(buf, off, len); 00079 if (wb.autoFlush) { 00080 wb.w.flush(); 00081 } 00082 } 00083 } 00084 } 00085 00086 /** 00087 * Flush (the downstream writer) 00088 */ 00089 public void flush() throws IOException { 00090 Iterator iter = writers.values().iterator(); 00091 while (iter.hasNext()) { 00092 WriterBinding wb = (WriterBinding)iter.next(); 00093 if (wb.enabled && wb.passFlush) wb.w.flush(); 00094 } 00095 } 00096 00097 /** 00098 * Close (the downstream writer) 00099 */ 00100 public void close() throws IOException { 00101 Iterator iter = writers.values().iterator(); 00102 while (iter.hasNext()) { 00103 WriterBinding wb = (WriterBinding)iter.next(); 00104 if (wb.enabled && wb.passClose) wb.w.close(); 00105 } 00106 } 00107 00108 /** 00109 * Add a new writer 00110 */ 00111 public void addWriter(String name, Writer w) { 00112 WriterBinding wb = new WriterBinding(w); 00113 writers.put(name, wb); 00114 } 00115 00116 /** 00117 * Remove a writer 00118 */ 00119 public void removeWriter(String name) { 00120 writers.remove(name); 00121 } 00122 00123 public void setPassClose(String w, boolean b) throws IOException { 00124 getBinding(w).passClose = b; 00125 } 00126 00127 public void setPassFlush(String w, boolean b) throws IOException { 00128 getBinding(w).passFlush = b; 00129 } 00130 00131 public void setAutoFlush(String w, boolean b) throws IOException { 00132 getBinding(w).autoFlush = b; 00133 } 00134 00135 public void setEnabled(String w, boolean b) throws IOException { 00136 getBinding(w).enabled = b; 00137 } 00138 00139 final private WriterBinding getBinding(String w) throws IOException { 00140 WriterBinding wb = (WriterBinding)writers.get(w); 00141 if (wb == null) { 00142 throw new IOException("No writer: " + w); 00143 } 00144 return wb; 00145 } 00146 00147 }