    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;
    018
    019import static org.apache.commons.io.IOUtils.EOF;
    020
    021import java.io.ByteArrayInputStream;
    022import java.io.IOException;
    023import java.io.InputStream;
    024import java.io.InputStreamReader;
    025import java.io.OutputStream;
    026import java.io.OutputStreamWriter;
    027import java.io.Reader;
    028import java.io.StringReader;
    029import java.io.Writer;
    030import java.nio.charset.Charset;
    031
    032/**
    033 * This class provides static utility methods for buffered
    034 * copying between sources ({@link InputStream}, {@link Reader},
    035 * {@link String} and {@code byte[]}) and destinations
    036 * ({@link OutputStream}, {@link Writer}, {@link String} and
    037 * {@code byte[]}).
    038 * <p>
    039 * Unless otherwise noted, these {@code copy} methods do <em>not</em>
    040 * flush or close the streams. Often doing so would require making non-portable
    041 * assumptions about the streams' origin and further use. This means that both
    042 * streams' {@code close()} methods must be called after copying. if one
    043 * omits this step, then the stream resources (sockets, file descriptors) are
    044 * released when the associated Stream is garbage-collected. It is not a good
    045 * idea to rely on this mechanism. For a good overview of the distinction
    046 * between "memory management" and "resource management", see
    047 * <a href="http://www.unixreview.com/articles/1998/9804/9804ja/ja.htm">this
    048 * UnixReview article</a>.
    049 * <p>
    050 * For byte-to-char methods, a {@code copy} variant allows the encoding
    051 * to be selected (otherwise the platform default is used). We would like to
    052 * encourage you to always specify the encoding because relying on the platform
    053 * default can lead to unexpected results.
    054 * <p>
    055 * We don't provide special variants for the {@code copy} methods that
    056 * let you specify the buffer size because in modern VMs the impact on speed
    057 * seems to be minimal. We're using a default buffer size of 4 KB.
    058 * <p>
    059 * The {@code copy} methods use an internal buffer when copying. It is
    060 * therefore advisable <em>not</em> to deliberately wrap the stream arguments
    061 * to the {@code copy} methods in {@code Buffered*} streams. For
    062 * example, don't do the following:
    063 * <pre>
    064 *  copy( new BufferedInputStream( in ), new BufferedOutputStream( out ) );
    065 *  </pre>
    066 * The rationale is as follows:
    067 * <p>
    068 * Imagine that an InputStream's read() is a very expensive operation, which
    069 * would usually suggest wrapping in a BufferedInputStream. The
    070 * BufferedInputStream works by issuing infrequent
    071 * {@link java.io.InputStream#read(byte[] b, int off, int len)} requests on the
    072 * underlying InputStream, to fill an internal buffer, from which further
    073 * {@code read} requests can inexpensively get their data (until the buffer
    074 * runs out).
    075 * <p>
    076 * However, the {@code copy} methods do the same thing, keeping an
    077 * internal buffer, populated by
    078 * {@link InputStream#read(byte[] b, int off, int len)} requests. Having two
    079 * buffers (or three if the destination stream is also buffered) is pointless,
    080 * and the unnecessary buffer management hurts performance slightly (about 3%,
    081 * according to some simple experiments).
    082 * <p>
    083 * Behold, intrepid explorers; a map of this class:
    084 * <pre>
    085 *       Method      Input               Output          Dependency
    086 *       ------      -----               ------          -------
    087 * 1     copy        InputStream         OutputStream    (primitive)
    088 * 2     copy        Reader              Writer          (primitive)
    089 *
    090 * 3     copy        InputStream         Writer          2
    091 *
    092 * 4     copy        Reader              OutputStream    2
    093 *
    094 * 5     copy        String              OutputStream    2
    095 * 6     copy        String              Writer          (trivial)
    096 *
    097 * 7     copy        byte[]              Writer          3
    098 * 8     copy        byte[]              OutputStream    (trivial)
    099 * </pre>
    100 * <p>
    101 * Note that only the first two methods shuffle bytes; the rest use these
    102 * two, or (if possible) copy using native Java copy methods. As there are
    103 * method variants to specify the encoding, each row may
    104 * correspond to up to 2 methods.
    105 * <p>
    106 * Provenance: Excalibur.
    107 *
    108 * @deprecated Use IOUtils. Will be removed in 3.0.
    109 *  Methods renamed to IOUtils.write() or IOUtils.copy().
    110 *  Null handling behavior changed in IOUtils (null data does not
    111 *  throw NullPointerException).
    112 */
    113@Deprecated
    114public class CopyUtils {
    115
    116    /**
    117     * Copies bytes from a {@code byte[]} to an {@link OutputStream}.
    118     * @param input the byte array to read from
    119     * @param output the {@link OutputStream} to write to
    120     * @throws IOException In case of an I/O problem
    121     */
    122    public static void copy(final byte[] input, final OutputStream output) throws IOException {
    123        output.write(input);
    124    }
    125
    126    /**
    127     * Copies and convert bytes from a {@code byte[]} to chars on a
    128     * {@link Writer}.
    129     * The platform's default encoding is used for the byte-to-char conversion.
    130     *
    131     * @param input the byte array to read from
    132     * @param output the {@link Writer} to write to
    133     * @throws IOException In case of an I/O problem
    134     * @deprecated Use {@link #copy(byte[], Writer, String)} instead
    135     */
    136    @Deprecated
    137    public static void copy(final byte[] input, final Writer output) throws IOException {
    138        final ByteArrayInputStream inputStream = new ByteArrayInputStream(input);
    139        copy(inputStream, output);
    140    }
    141
    142    /**
    143     * Copies and convert bytes from a {@code byte[]} to chars on a
    144     * {@link Writer}, using the specified encoding.
    145     *
    146     * @param input the byte array to read from
    147     * @param output the {@link Writer} to write to
    148     * @param encoding The name of a supported character encoding. See the
    149     * <a href="http://www.iana.org/assignments/character-sets">IANA
    150     * Charset Registry</a> for a list of valid encoding types.
    151     * @throws IOException In case of an I/O problem
    152     */
    153    public static void copy(final byte[] input, final Writer output, final String encoding) throws IOException {
    154        final ByteArrayInputStream inputStream = new ByteArrayInputStream(input);
    155        copy(inputStream, output, encoding);
    156    }
    157
    158    /**
    159     * Copies bytes from an {@link InputStream} to an
    160     * {@link OutputStream}.
    161     *
    162     * @param input the {@link InputStream} to read from
    163     * @param output the {@link OutputStream} to write to
    164     * @return the number of bytes copied
    165     * @throws IOException In case of an I/O problem
    166     */
    167    public static int copy(final InputStream input, final OutputStream output) throws IOException {
    168        final byte[] buffer = IOUtils.byteArray();
    169        int count = 0;
    170        int n;
    171        while (EOF != (n = input.read(buffer))) {
    172            output.write(buffer, 0, n);
    173            count += n;
    174        }
    175        return count;
    176    }
    177
    178    /**
    179     * Copies and convert bytes from an {@link InputStream} to chars on a
    180     * {@link Writer}.
    181     * The platform's default encoding is used for the byte-to-char conversion.
    182     *
    183     * @param input the {@link InputStream} to read from
    184     * @param output the {@link Writer} to write to
    185     * @throws IOException In case of an I/O problem
    186     * @deprecated Use {@link #copy(InputStream, Writer, String)} instead
    187     */
    188    @Deprecated
    189    public static void copy(
    190            final InputStream input,
    191            final Writer output)
    192                throws IOException {
    193        // make explicit the dependency on the default encoding
    194        final InputStreamReader in = new InputStreamReader(input, Charset.defaultCharset());
    195        copy(in, output);
    196    }
    197
    198    /**
    199     * Copies and convert bytes from an {@link InputStream} to chars on a
    200     * {@link Writer}, using the specified encoding.
    201     *
    202     * @param input the {@link InputStream} to read from
    203     * @param output the {@link Writer} to write to
    204     * @param encoding The name of a supported character encoding. See the
    205     * <a href="http://www.iana.org/assignments/character-sets">IANA
    206     * Charset Registry</a> for a list of valid encoding types.
    207     * @throws IOException In case of an I/O problem
    208     */
    209    public static void copy(
    210            final InputStream input,
    211            final Writer output,
    212            final String encoding)
    213                throws IOException {
    214        final InputStreamReader in = new InputStreamReader(input, encoding);
    215        copy(in, output);
    216    }
    217
    218    /**
    219     * Serialize chars from a {@link Reader} to bytes on an
    220     * {@link OutputStream}, and flush the {@link OutputStream}.
    221     * Uses the default platform encoding.
    222     *
    223     * @param input the {@link Reader} to read from
    224     * @param output the {@link OutputStream} to write to
    225     * @throws IOException In case of an I/O problem
    226     * @deprecated Use {@link #copy(Reader, OutputStream, String)} instead
    227     */
    228    @Deprecated
    229    public static void copy(
    230            final Reader input,
    231            final OutputStream output)
    232                throws IOException {
    233        // make explicit the dependency on the default encoding
    234        final OutputStreamWriter out = new OutputStreamWriter(output, Charset.defaultCharset());
    235        copy(input, out);
    236        // XXX Unless anyone is planning on rewriting OutputStreamWriter, we
    237        // have to flush here.
    238        out.flush();
    239    }
    240
    241    /**
    242     * Serialize chars from a {@link Reader} to bytes on an
    243     * {@link OutputStream}, and flush the {@link OutputStream}.
    244     *
    245     * @param input the {@link Reader} to read from
    246     * @param output the {@link OutputStream} to write to
    247     * @param encoding The name of a supported character encoding. See the
    248     * <a href="http://www.iana.org/assignments/character-sets">IANA
    249     * Charset Registry</a> for a list of valid encoding types.
    250     * @throws IOException In case of an I/O problem
    251     * @since 2.5
    252     */
    253    public static void copy(
    254            final Reader input,
    255            final OutputStream output,
    256            final String encoding)
    257                throws IOException {
    258        final OutputStreamWriter out = new OutputStreamWriter(output, encoding);
    259        copy(input, out);
    260        // XXX Unless anyone is planning on rewriting OutputStreamWriter, we
    261        // have to flush here.
    262        out.flush();
    263    }
    264
    265    /**
    266     * Copies chars from a {@link Reader} to a {@link Writer}.
    267     *
    268     * @param input the {@link Reader} to read from
    269     * @param output the {@link Writer} to write to
    270     * @return the number of characters copied
    271     * @throws IOException In case of an I/O problem
    272     */
    273    public static int copy(
    274            final Reader input,
    275            final Writer output)
    276                throws IOException {
    277        final char[] buffer = IOUtils.getScratchCharArray();
    278        int count = 0;
    279        int n;
    280        while (EOF != (n = input.read(buffer))) {
    281            output.write(buffer, 0, n);
    282            count += n;
    283        }
    284        return count;
    285    }
    286
    287    /**
    288     * Serialize chars from a {@link String} to bytes on an
    289     * {@link OutputStream}, and
    290     * flush the {@link OutputStream}.
    291     * Uses the platform default encoding.
    292     *
    293     * @param input the {@link String} to read from
    294     * @param output the {@link OutputStream} to write to
    295     * @throws IOException In case of an I/O problem
    296     * @deprecated Use {@link #copy(String, OutputStream, String)} instead
    297     */
    298    @Deprecated
    299    public static void copy(
    300            final String input,
    301            final OutputStream output)
    302                throws IOException {
    303        final StringReader in = new StringReader(input);
    304        // make explicit the dependency on the default encoding
    305        final OutputStreamWriter out = new OutputStreamWriter(output, Charset.defaultCharset());
    306        copy(in, out);
    307        // XXX Unless anyone is planning on rewriting OutputStreamWriter, we
    308        // have to flush here.
    309        out.flush();
    310    }
    311
    312    /**
    313     * Serialize chars from a {@link String} to bytes on an
    314     * {@link OutputStream}, and
    315     * flush the {@link OutputStream}.
    316     *
    317     * @param input the {@link String} to read from
    318     * @param output the {@link OutputStream} to write to
    319     * @param encoding The name of a supported character encoding. See the
    320     * <a href="http://www.iana.org/assignments/character-sets">IANA
    321     * Charset Registry</a> for a list of valid encoding types.
    322     * @throws IOException In case of an I/O problem
    323     * @since 2.5
    324     */
    325    public static void copy(
    326            final String input,
    327            final OutputStream output,
    328            final String encoding)
    329                throws IOException {
    330        final StringReader in = new StringReader(input);
    331        final OutputStreamWriter out = new OutputStreamWriter(output, encoding);
    332        copy(in, out);
    333        // XXX Unless anyone is planning on rewriting OutputStreamWriter, we
    334        // have to flush here.
    335        out.flush();
    336    }
    337
    338    /**
    339     * Copies chars from a {@link String} to a {@link Writer}.
    340     *
    341     * @param input the {@link String} to read from
    342     * @param output the {@link Writer} to write to
    343     * @throws IOException In case of an I/O problem
    344     */
    345    public static void copy(final String input, final Writer output)
    346                throws IOException {
    347        output.write(input);
    348    }
    349
    350    /**
    351     * Instances should NOT be constructed in standard programming.
    352     *
    353     * @deprecated TODO Make private in 3.0.
    354     */
    355    @Deprecated
    356    public CopyUtils() {
    357        // empty
    358    }
    359
    360}