001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, GeoTools Project Managment Committee (PMC)
005: * (C) 2004, Institut de Recherche pour le Développement
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation;
010: * version 2.1 of the License.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.io;
018:
019: // J2SE dependencies
020: import java.io.FilterWriter;
021: import java.io.IOException;
022: import java.io.Writer;
023:
024: /**
025: * A writer that copy all output to an other stream. This writer can be used for perfoming
026: * an exact copy of what is sent to an other writer. For example, it may be used for echoing
027: * to the standard output the content sent to a file. This writer is usefull for debugging
028: * purpose.
029: *
030: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/metadata/src/main/java/org/geotools/io/EchoWriter.java $
031: * @version $Id: EchoWriter.java 23632 2006-12-29 22:13:51Z desruisseaux $
032: * @author Martin Desruisseaux
033: *
034: * @since 2.1
035: */
036: public class EchoWriter extends FilterWriter {
037: /**
038: * The echo writer.
039: */
040: private final Writer echo;
041:
042: /**
043: * Creates a writer that will echo to the {@linkplain System#out standard output}.
044: * Each line to that standard output will be {@linkplain NumberedLineWriter numbered}.
045: *
046: * @param main The main stream.
047: */
048: public EchoWriter(final Writer main) {
049: super (main);
050: this .echo = NumberedLineWriter.OUT;
051: }
052:
053: /**
054: * Creates a copy writter for the specified stream.
055: *
056: * @param main The main stream.
057: * @param echo The echo stream.
058: */
059: public EchoWriter(final Writer main, final Writer echo) {
060: super (main);
061: this .echo = echo;
062: }
063:
064: /**
065: * Write a single character.
066: *
067: * @throws IOException If an I/O error occurs
068: */
069: public void write(final int c) throws IOException {
070: synchronized (lock) {
071: out.write(c);
072: echo.write(c);
073: }
074: }
075:
076: /**
077: * Write an array of characters.
078: *
079: * @param cbuf Buffer of characters to be written
080: * @throws IOException If an I/O error occurs
081: */
082: public void write(final char[] cbuf) throws IOException {
083: synchronized (lock) {
084: out.write(cbuf);
085: echo.write(cbuf);
086: }
087: }
088:
089: /**
090: * Write a portion of an array of characters.
091: *
092: * @param cbuf Buffer of characters to be written
093: * @param off Offset from which to start reading characters
094: * @param len Number of characters to be written
095: * @throws IOException If an I/O error occurs
096: */
097: public void write(final char[] cbuf, final int off, final int len)
098: throws IOException {
099: synchronized (lock) {
100: out.write(cbuf, off, len);
101: echo.write(cbuf, off, len);
102: }
103: }
104:
105: /**
106: * Write a string.
107: *
108: * @param str String to be written
109: * @throws IOException If an I/O error occurs
110: */
111: public void write(final String str) throws IOException {
112: synchronized (lock) {
113: out.write(str);
114: echo.write(str);
115: }
116: }
117:
118: /**
119: * Write a portion of a string.
120: *
121: * @param str A String
122: * @param off Offset from which to start writing characters
123: * @param len Number of characters to write
124: * @throws IOException If an I/O error occurs
125: */
126: public void write(final String str, final int off, final int len)
127: throws IOException {
128: synchronized (lock) {
129: out.write(str, off, len);
130: echo.write(str, off, len);
131: }
132: }
133:
134: /**
135: * Flush both streams.
136: *
137: * @throws IOException If an I/O error occurs
138: */
139: public void flush() throws IOException {
140: synchronized (lock) {
141: out.flush();
142: echo.flush();
143: }
144: }
145:
146: /**
147: * Close the main stream, If this object has been constructed with the
148: * {@linkplain #EchoWriter(Writer) one argument constructor} (i.e.
149: * if the echo stream is the {@linkplain System#out standard output}),
150: * then the echo stream will not be closed. Otherwise it will be closed too.
151: *
152: * @throws IOException If an I/O error occurs
153: */
154: public void close() throws IOException {
155: synchronized (lock) {
156: out.close();
157: echo.close(); // Overridden with an uncloseable version for System.out.
158: }
159: }
160: }
|