001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * 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, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.camel.converter;
019    
020    import org.apache.camel.Converter;
021    import org.apache.commons.logging.Log;
022    import org.apache.commons.logging.LogFactory;
023    
024    import java.io.*;
025    import java.nio.ByteBuffer;
026    
027    /**
028     * Some core java.io based
029     * <a href="http://activemq.apache.org/camel/type-converter.html">Type Converters</a>
030     *
031     * @version $Revision: 544115 $
032     */
033    @Converter
034    public class IOConverter {
035        private static final transient Log log = LogFactory.getLog(IOConverter.class);
036    
037        @Converter
038        public static InputStream toInputStream(File file) throws FileNotFoundException {
039            return new BufferedInputStream(new FileInputStream(file));
040        }
041    
042        @Converter
043        public static BufferedReader toReader(File file) throws FileNotFoundException {
044            return new BufferedReader(new FileReader(file));
045        }
046    
047        @Converter
048        public static OutputStream toOutputStream(File file) throws FileNotFoundException {
049            return new BufferedOutputStream(new FileOutputStream(file));
050        }
051    
052        @Converter
053        public static BufferedWriter toWriter(File file) throws IOException {
054            return new BufferedWriter(new FileWriter(file));
055        }
056    
057        @Converter
058        public static Reader toReader(InputStream in) throws FileNotFoundException {
059            return new InputStreamReader(in);
060        }
061    
062        @Converter
063        public static Writer toWriter(OutputStream out) throws FileNotFoundException {
064            return new OutputStreamWriter(out);
065        }
066    
067    
068        @Converter
069        public static StringReader toReader(String text) {
070            // TODO could we automatically find this?
071            return new StringReader(text);
072        }
073    
074        @Converter
075        public static InputStream toInputStream(String text) {
076            return toInputStream(text.getBytes());
077        }
078    
079        @Converter
080        public static byte[] toByteArray(String text) {
081            // TODO could we automatically find this?
082            return text.getBytes();
083        }
084    
085        @Converter
086        public static String toString(byte[] data) {
087            return new String(data);
088        }
089    
090        @Converter
091        public static String toString(Reader reader) throws IOException {
092            if (reader instanceof BufferedReader) {
093                return toString((BufferedReader) reader);
094            }
095            else {
096                return toString(new BufferedReader(reader));
097            }
098        }
099    
100        @Converter
101        public static String toString(BufferedReader reader) throws IOException {
102            if (reader == null) {
103                return null;
104            }
105            try {
106                StringBuilder builder = new StringBuilder();
107                boolean first = true;
108                while (true) {
109                    String line = reader.readLine();
110                    if (line == null) {
111                        return builder.toString();
112                    }
113                    if (first) {
114                        first = false;
115                    }
116                    else {
117                        builder.append("\n");
118                    }
119                    builder.append(line);
120                }
121            }
122            finally {
123                try {
124                    reader.close();
125                }
126                catch (IOException e) {
127                    log.warn("Failed to close stream: "+ e, e);
128                }
129            }
130        }
131        
132        @Converter
133        public static String toString(InputStream in) throws IOException {
134            return toString(toReader(in));
135        }
136    
137        @Converter
138        public static InputStream toInputStream(byte[] data) {
139            return new ByteArrayInputStream(data);
140        }
141    }