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.ByteArrayOutputStream;
022    import java.io.IOException;
023    import java.io.InputStream;
024    import java.io.OutputStream;
025    
026    /**
027     * Utility functions
028     * @Immutable
029     */
030    public final class IOUtils {
031    
032        /** Private constructor to prevent instantiation of this utility class. */
033        private IOUtils(){
034        }
035    
036        /**
037         * Copies the content of a InputStream into an OutputStream.
038         * Uses a default buffer size of 8024 bytes.
039         *
040         * @param input
041         *            the InputStream to copy
042         * @param output
043         *            the target Stream
044         * @throws IOException
045         *             if an error occurs
046         */
047        public static long copy(final InputStream input, final OutputStream output) throws IOException {
048            return copy(input, output, 8024);
049        }
050    
051        /**
052         * Copies the content of a InputStream into an OutputStream
053         *
054         * @param input
055         *            the InputStream to copy
056         * @param output
057         *            the target Stream
058         * @param buffersize
059         *            the buffer size to use
060         * @throws IOException
061         *             if an error occurs
062         */
063        public static long copy(final InputStream input, final OutputStream output, int buffersize) throws IOException {
064            final byte[] buffer = new byte[buffersize];
065            int n = 0;
066            long count=0;
067            while (-1 != (n = input.read(buffer))) {
068                output.write(buffer, 0, n);
069                count += n;
070            }
071            return count;
072        }
073    
074    
075        // toByteArray(InputStream) copied from:
076        // commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java?revision=1428941
077        // January 8th, 2013
078        //
079        // Assuming our copy() works just as well as theirs!  :-)
080    
081        /**
082         * Gets the contents of an <code>InputStream</code> as a <code>byte[]</code>.
083         * <p>
084         * This method buffers the input internally, so there is no need to use a
085         * <code>BufferedInputStream</code>.
086         *
087         * @param input  the <code>InputStream</code> to read from
088         * @return the requested byte array
089         * @throws NullPointerException if the input is null
090         * @throws IOException if an I/O error occurs
091         * @since 1.5
092         */
093        public static byte[] toByteArray(final InputStream input) throws IOException {
094            final ByteArrayOutputStream output = new ByteArrayOutputStream();
095            copy(input, output);
096            return output.toByteArray();
097        }
098    }