/Users/lyon/j4p/src/classUtils/pack/util/TieWriter.java

1    package classUtils.pack.util; 
2     
3    import java.io.Writer; 
4    import java.io.PrintWriter; 
5    import java.io.IOException; 
6     
7    /** 
8     * An Writer which ties output to two underlying streams. Any character written to the stream 
9     * is replicated on both. 
10    * 
11    * @author C. Sadun 
12    * @version 1.2 
13    */ 
14   public class TieWriter extends Writer 
15   { 
16      private Writer w1; 
17      private Writer w2; 
18    
19      /** 
20       * Build a Writer that ties togheter the two given streams. 
21       * Every output operation on the Writer will be replicated on both. 
22       * @param w1 the first stream 
23       * @param w2 the second stream 
24       */ 
25      public TieWriter(Writer w1, Writer w2) 
26      { this.w1=w1; 
27        this.w2=w2; 
28      } 
29    
30      /** 
31       * Write a byte to the stream, and thus to the two underlying streams. 
32       * 
33       * @param b the byte to write 
34       * @exception IOException if something fails during writing 
35       */ 
36      public void write(char[] buf, int off, int len) throws IOException 
37      { 
38       w1.write(buf, off, len); 
39       doCheckError(w1); 
40       w2.write(buf, off, len); 
41       doCheckError(w2); 
42      } 
43    
44      /** 
45       * Close both underlying streams 
46       * 
47       * @param b the byte to write 
48       * @exception IOException if something fails during closing 
49       */ 
50      public void close() throws IOException { 
51       w1.close(); 
52       doCheckError(w1); 
53       w2.close(); 
54       doCheckError(w2); 
55      } 
56    
57      /** 
58       * Flush both underlying streams 
59       * 
60       * @param b the byte to write 
61       * @exception IOException if something fails during flushing 
62       */ 
63      public void flush() throws IOException { 
64       w1.flush(); 
65       doCheckError(w1); 
66       w2.flush(); 
67       doCheckError(w2); 
68      } 
69    
70      /** 
71       * Return the first of the two tied writers 
72       * @return the first of the two tied writers 
73       */ 
74      public Writer getFirstWriter() { return w1; } 
75    
76      /** 
77       * Return the second of the two tied writers 
78       * @return the second of the two tied writers 
79       */ 
80      public Writer getSecondWriter() { return w2; } 
81    
82      // If the writer's of PrintWriter flavor, do an explicit 
83      // error check and raise an IOException in case of error 
84      private void doCheckError(Writer w) throws IOException { 
85       if (w instanceof PrintWriter) 
86           if (((PrintWriter)w).checkError()) 
87               throw new IOException("Operation on printwriter failed"); 
88      } 
89    
90      public static void main(String args[]) throws Exception { 
91       String lineSep = System.getProperty("line.separator"); 
92       PrintWriter w = new PrintWriter(new TieWriter(new java.io.FileWriter("test.txt"), new classUtils.pack.util.AutoCRWriter(System.out))); 
93       w.print("Hello"); 
94       w.println(); 
95       w.print("World"); 
96       w.println(); 
97    
98       w.close(); 
99      } 
100   
101   
102  } 
103