    001    /*
    002     * Licensed to the Apache Software Foundation (ASF) under one
    003     * or more contributor license agreements.  See the NOTICE file
    004     * distributed with this work for additional information
    005     * regarding copyright ownership.  The ASF licenses this file
    006     * to you under the Apache License, Version 2.0 (the
    007     * "License"); you may not use this file except in compliance
    008     * with the License.  You may obtain a copy of the License at
    009     *
    010     * http://www.apache.org/licenses/LICENSE-2.0
    011     *
    012     * Unless required by applicable law or agreed to in writing,
    013     * software distributed under the License is distributed on an
    014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    015     * KIND, either express or implied.  See the License for the
    016     * specific language governing permissions and limitations
    017     * under the License.
    018     */
    019    package org.apache.commons.compress.utils;
    020
    021    import java.io.IOException;
    022    import java.io.InputStream;
    023    import java.io.OutputStream;
    024
    025    /**
    026     * Utility functions
    027     * @Immutable
    028     */
    029    public final class IOUtils {
    030
    031        /** Private constructor to prevent instantiation of this utility class. */
    032        private IOUtils(){
    033        }
    034
    035        /**
    036         * Copies the content of a InputStream into an OutputStream.
    037         * Uses a default buffer size of 8024 bytes.
    038         *
    039         * @param input
    040         *            the InputStream to copy
    041         * @param output
    042         *            the target Stream
    043         * @throws IOException
    044         *             if an error occurs
    045         */
    046        public static long copy(final InputStream input, final OutputStream output) throws IOException {
    047            return copy(input, output, 8024);
    048        }
    049
    050        /**
    051         * Copies the content of a InputStream into an OutputStream
    052         *
    053         * @param input
    054         *            the InputStream to copy
    055         * @param output
    056         *            the target Stream
    057         * @param buffersize
    058         *            the buffer size to use
    059         * @throws IOException
    060         *             if an error occurs
    061         */
    062        public static long copy(final InputStream input, final OutputStream output, int buffersize) throws IOException {
    063            final byte[] buffer = new byte[buffersize];
    064            int n = 0;
    065            long count=0;
    066            while (-1 != (n = input.read(buffer))) {
    067                output.write(buffer, 0, n);
    068                count += n;
    069            }
    070            return count;
    071        }
    072    }