    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.io.output;
    018
    019import java.io.FilterWriter;
    020import java.io.IOException;
    021import java.io.Writer;
    022import java.util.Collection;
    023
    024import org.apache.commons.io.IOUtils;
    025
    026/**
    027 * A Proxy stream collection which acts as expected, that is it passes the method calls on to the proxied streams and
    028 * doesn't change which methods are being called. It is an alternative base class to {@link FilterWriter} and
    029 * {@link FilterCollectionWriter} to increase reusability, because FilterWriter changes the methods being called, such
    030 * as {@code write(char[])} to {@code write(char[], int, int)} and {@code write(String)} to
    031 * {@code write(String, int, int)}. This is in contrast to {@link ProxyWriter} which is backed by a single
    032 * {@link Writer}.
    033 *
    034 * @since 2.7
    035 */
    036public class ProxyCollectionWriter extends FilterCollectionWriter {
    037
    038    /**
    039     * Creates a new proxy collection writer.
    040     *
    041     * @param writers Writers object to provide the underlying targets.
    042     */
    043    public ProxyCollectionWriter(final Collection<Writer> writers) {
    044        super(writers);
    045    }
    046
    047    /**
    048     * Creates a new proxy collection writer.
    049     *
    050     * @param writers Writers to provide the underlying targets.
    051     */
    052    public ProxyCollectionWriter(final Writer... writers) {
    053        super(writers);
    054    }
    055
    056    /**
    057     * Invoked by the write methods after the proxied call has returned successfully. The number of chars written (1 for
    058     * the {@link #write(int)} method, buffer length for {@link #write(char[])}, etc.) is given as an argument.
    059     * <p>
    060     * Subclasses can override this method to add common post-processing functionality without having to override all
    061     * the write methods. The default implementation does nothing.
    062     * </p>
    063     *
    064     * @param n number of chars written
    065     * @throws IOException if the post-processing fails
    066     */
    067    @SuppressWarnings("unused") // Possibly thrown from subclasses.
    068    protected void afterWrite(final int n) throws IOException {
    069        // noop
    070    }
    071
    072    /**
    073     * Invokes the delegates' {@code append(char)} methods.
    074     *
    075     * @param c The character to write
    076     * @return this writer
    077     * @throws IOException if an I/O error occurs.
    078     * @since 2.0
    079     */
    080    @Override
    081    public Writer append(final char c) throws IOException {
    082        try {
    083            beforeWrite(1);
    084            super.append(c);
    085            afterWrite(1);
    086        } catch (final IOException e) {
    087            handleIOException(e);
    088        }
    089        return this;
    090    }
    091
    092    /**
    093     * Invokes the delegates' {@code append(CharSequence)} methods.
    094     *
    095     * @param csq The character sequence to write
    096     * @return this writer
    097     * @throws IOException if an I/O error occurs.
    098     */
    099    @Override
    100    public Writer append(final CharSequence csq) throws IOException {
    101        try {
    102            final int len = IOUtils.length(csq);
    103            beforeWrite(len);
    104            super.append(csq);
    105            afterWrite(len);
    106        } catch (final IOException e) {
    107            handleIOException(e);
    108        }
    109        return this;
    110    }
    111
    112    /**
    113     * Invokes the delegates' {@code append(CharSequence, int, int)} methods.
    114     *
    115     * @param csq   The character sequence to write
    116     * @param start The index of the first character to write
    117     * @param end   The index of the first character to write (exclusive)
    118     * @return this writer
    119     * @throws IOException if an I/O error occurs.
    120     */
    121    @Override
    122    public Writer append(final CharSequence csq, final int start, final int end) throws IOException {
    123        try {
    124            beforeWrite(end - start);
    125            super.append(csq, start, end);
    126            afterWrite(end - start);
    127        } catch (final IOException e) {
    128            handleIOException(e);
    129        }
    130        return this;
    131    }
    132
    133    /**
    134     * Invoked by the write methods before the call is proxied. The number of chars to be written (1 for the
    135     * {@link #write(int)} method, buffer length for {@link #write(char[])}, etc.) is given as an argument.
    136     * <p>
    137     * Subclasses can override this method to add common pre-processing functionality without having to override all the
    138     * write methods. The default implementation does nothing.
    139     * </p>
    140     *
    141     * @param n number of chars to be written
    142     * @throws IOException if the pre-processing fails
    143     */
    144    @SuppressWarnings("unused") // Possibly thrown from subclasses.
    145    protected void beforeWrite(final int n) throws IOException {
    146        // noop
    147    }
    148
    149    /**
    150     * Invokes the delegate's {@code close()} method.
    151     *
    152     * @throws IOException if an I/O error occurs.
    153     */
    154    @Override
    155    public void close() throws IOException {
    156        try {
    157            super.close();
    158        } catch (final IOException e) {
    159            handleIOException(e);
    160        }
    161    }
    162
    163    /**
    164     * Invokes the delegate's {@code flush()} method.
    165     *
    166     * @throws IOException if an I/O error occurs.
    167     */
    168    @Override
    169    public void flush() throws IOException {
    170        try {
    171            super.flush();
    172        } catch (final IOException e) {
    173            handleIOException(e);
    174        }
    175    }
    176
    177    /**
    178     * Handle any IOExceptions thrown.
    179     * <p>
    180     * This method provides a point to implement custom exception handling. The default behavior is to re-throw the
    181     * exception.
    182     * </p>
    183     *
    184     * @param e The IOException thrown
    185     * @throws IOException if an I/O error occurs.
    186     */
    187    protected void handleIOException(final IOException e) throws IOException {
    188        throw e;
    189    }
    190
    191    /**
    192     * Invokes the delegate's {@code write(char[])} method.
    193     *
    194     * @param cbuf the characters to write
    195     * @throws IOException if an I/O error occurs.
    196     */
    197    @Override
    198    public void write(final char[] cbuf) throws IOException {
    199        try {
    200            final int len = IOUtils.length(cbuf);
    201            beforeWrite(len);
    202            super.write(cbuf);
    203            afterWrite(len);
    204        } catch (final IOException e) {
    205            handleIOException(e);
    206        }
    207    }
    208
    209    /**
    210     * Invokes the delegate's {@code write(char[], int, int)} method.
    211     *
    212     * @param cbuf the characters to write
    213     * @param off  The start offset
    214     * @param len  The number of characters to write
    215     * @throws IOException if an I/O error occurs.
    216     */
    217    @Override
    218    public void write(final char[] cbuf, final int off, final int len) throws IOException {
    219        try {
    220            beforeWrite(len);
    221            super.write(cbuf, off, len);
    222            afterWrite(len);
    223        } catch (final IOException e) {
    224            handleIOException(e);
    225        }
    226    }
    227
    228    /**
    229     * Invokes the delegate's {@code write(int)} method.
    230     *
    231     * @param c the character to write
    232     * @throws IOException if an I/O error occurs.
    233     */
    234    @Override
    235    public void write(final int c) throws IOException {
    236        try {
    237            beforeWrite(1);
    238            super.write(c);
    239            afterWrite(1);
    240        } catch (final IOException e) {
    241            handleIOException(e);
    242        }
    243    }
    244
    245    /**
    246     * Invokes the delegate's {@code write(String)} method.
    247     *
    248     * @param str the string to write
    249     * @throws IOException if an I/O error occurs.
    250     */
    251    @Override
    252    public void write(final String str) throws IOException {
    253        try {
    254            final int len = IOUtils.length(str);
    255            beforeWrite(len);
    256            super.write(str);
    257            afterWrite(len);
    258        } catch (final IOException e) {
    259            handleIOException(e);
    260        }
    261    }
    262
    263    /**
    264     * Invokes the delegate's {@code write(String)} method.
    265     *
    266     * @param str the string to write
    267     * @param off The start offset
    268     * @param len The number of characters to write
    269     * @throws IOException if an I/O error occurs.
    270     */
    271    @Override
    272    public void write(final String str, final int off, final int len) throws IOException {
    273        try {
    274            beforeWrite(len);
    275            super.write(str, off, len);
    276            afterWrite(len);
    277        } catch (final IOException e) {
    278            handleIOException(e);
    279        }
    280    }
    281
    282}