001 package com.croftsoft.core.io; 002 003 import java.io.*; 004 005 import com.croftsoft.core.lang.NullArgumentException; 006 007 /********************************************************************* 008 * A FilterReader that echoes characters read to a Writer. 009 * 010 * <p /> 011 * 012 * @version 013 * 2002-09-19 014 * @since 015 * 2002-09-19 016 * @author 017 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 018 *********************************************************************/ 019 020 public final class EchoReader 021 extends FilterReader 022 ////////////////////////////////////////////////////////////////////// 023 ////////////////////////////////////////////////////////////////////// 024 { 025 026 private final Writer echoWriter; 027 028 ////////////////////////////////////////////////////////////////////// 029 ////////////////////////////////////////////////////////////////////// 030 031 /********************************************************************* 032 * Main constructor. 033 *********************************************************************/ 034 public EchoReader ( 035 Reader in, 036 Writer echoWriter ) 037 ////////////////////////////////////////////////////////////////////// 038 { 039 super ( in ); 040 041 NullArgumentException.check ( this.echoWriter = echoWriter ); 042 } 043 044 /********************************************************************* 045 * Echoes to the standard output. 046 * <code><pre>this ( in, new PrintWriter ( System.out ) );</pre></code> 047 *********************************************************************/ 048 public EchoReader ( Reader in ) 049 ////////////////////////////////////////////////////////////////////// 050 { 051 this ( in, new PrintWriter ( System.out ) ); 052 } 053 054 ////////////////////////////////////////////////////////////////////// 055 ////////////////////////////////////////////////////////////////////// 056 057 public int read ( ) 058 throws IOException 059 ////////////////////////////////////////////////////////////////////// 060 { 061 int ch = super.read ( ); 062 063 if ( ch > -1 ) 064 { 065 echoWriter.write ( ch ); 066 } 067 068 return ch; 069 } 070 071 public int read ( 072 char [ ] cbuf, 073 int off, 074 int len ) 075 throws IOException 076 ////////////////////////////////////////////////////////////////////// 077 { 078 int count = super.read ( cbuf, off, len ); 079 080 if ( count > -1 ) 081 { 082 echoWriter.write ( cbuf, off, count ); 083 } 084 085 return count; 086 } 087 088 public int read ( char [ ] cbuf ) 089 throws IOException 090 ////////////////////////////////////////////////////////////////////// 091 { 092 int count = super.read ( cbuf ); 093 094 if ( count > -1 ) 095 { 096 echoWriter.write ( cbuf, 0, count ); 097 } 098 099 return count; 100 } 101 102 /********************************************************************* 103 * Flushes the echoWriter in addition to closing the input. 104 *********************************************************************/ 105 public void close ( ) 106 throws IOException 107 ////////////////////////////////////////////////////////////////////// 108 { 109 super.close ( ); 110 111 echoWriter.flush ( ); 112 } 113 114 ////////////////////////////////////////////////////////////////////// 115 ////////////////////////////////////////////////////////////////////// 116 }