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     */
017    package org.apache.camel.util;
018    
019    import java.io.IOException;
020    import java.io.InputStream;
021    import java.io.OutputStream;
022    import java.io.UnsupportedEncodingException;
023    import java.nio.charset.Charset;
024    
025    /**
026     * IO helper class.
027     *
028     * @version $Revision: 749936 $
029     */
030    public final class IOHelper {
031        
032        private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
033        private static final Charset UTF8_CHARSET = Charset.forName("utf-8");
034    
035        private IOHelper() {
036            //Utility Class
037        }
038        
039        /**
040         * Use this function instead of new String(byte[]) to avoid surprises from non-standard default encodings.
041         * @param bytes
042         * @return
043         */
044        public static String newStringFromBytes(byte[] bytes) {
045            try {
046                return new String(bytes, UTF8_CHARSET.name());
047            } catch (UnsupportedEncodingException e) {
048                throw 
049                    new RuntimeException("Impossible failure: Charset.forName(\"utf-8\") returns invalid name.");
050    
051            }
052        }
053    
054        /**
055         * Use this function instead of new String(byte[], int, int) 
056         * to avoid surprises from non-standard default encodings.
057         * @param bytes
058         * @param start
059         * @param length
060         * @return
061         */
062        public static String newStringFromBytes(byte[] bytes, int start, int length) {
063            try {
064                return new String(bytes, start, length, UTF8_CHARSET.name());
065            } catch (UnsupportedEncodingException e) {
066                throw 
067                    new RuntimeException("Impossible failure: Charset.forName(\"utf-8\") returns invalid name.");
068    
069            }
070        }
071    
072        /**
073         * A factory method which creates an {@link IOException} from the given
074         * exception and message
075         */
076        public static IOException createIOException(Throwable cause) {
077            return createIOException(cause.getMessage(), cause);
078        }
079    
080        /**
081         * A factory method which creates an {@link IOException} from the given
082         * exception and message
083         */
084        public static IOException createIOException(String message, Throwable cause) {
085            IOException answer = new IOException(message);
086            answer.initCause(cause);
087            return answer;
088        }
089    
090        public static int copy(InputStream input, OutputStream output) throws IOException {
091            return copy(input, output, DEFAULT_BUFFER_SIZE);
092        }
093        
094        public static int copy(final InputStream input, final OutputStream output, int bufferSize)
095            throws IOException {
096            int avail = input.available();
097            if (avail > 262144) {
098                avail = 262144;
099            }
100            if (avail > bufferSize) {
101                bufferSize = avail;
102            }
103            final byte[] buffer = new byte[bufferSize];
104            int n = 0;
105            n = input.read(buffer);
106            int total = 0;
107            while (-1 != n) {
108                output.write(buffer, 0, n);
109                total += n;
110                n = input.read(buffer);
111            }
112            output.flush();
113            return total;
114        }
115        
116        public static void copyAndCloseInput(InputStream input, OutputStream output) throws IOException {
117            copy(input, output);
118            input.close();
119        }
120        
121        public static void copyAndCloseInput(InputStream input, OutputStream output, int bufferSize) throws IOException {
122            copy(input, output, bufferSize);
123        }
124    }