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.converter;
018    
019    import java.io.BufferedInputStream;
020    import java.io.BufferedOutputStream;
021    import java.io.BufferedReader;
022    import java.io.BufferedWriter;
023    import java.io.ByteArrayInputStream;
024    import java.io.ByteArrayOutputStream;
025    import java.io.File;
026    import java.io.FileInputStream;
027    import java.io.FileNotFoundException;
028    import java.io.FileOutputStream;
029    import java.io.FileReader;
030    import java.io.FileWriter;
031    import java.io.IOException;
032    import java.io.InputStream;
033    import java.io.InputStreamReader;
034    import java.io.ObjectInput;
035    import java.io.ObjectInputStream;
036    import java.io.ObjectOutput;
037    import java.io.ObjectOutputStream;
038    import java.io.OutputStream;
039    import java.io.OutputStreamWriter;
040    import java.io.Reader;
041    import java.io.StringReader;
042    import java.io.UnsupportedEncodingException;
043    import java.io.Writer;
044    import java.net.URL;
045    import javax.xml.transform.TransformerException;
046    import javax.xml.transform.dom.DOMSource;
047    
048    import org.apache.camel.Converter;
049    import org.apache.camel.Exchange;
050    import org.apache.camel.converter.jaxp.XmlConverter;
051    import org.apache.camel.util.CollectionStringBuffer;
052    import org.apache.camel.util.IOHelper;
053    import org.apache.camel.util.ObjectHelper;
054    import org.apache.commons.logging.Log;
055    import org.apache.commons.logging.LogFactory;
056    
057    /**
058     * Some core java.io based <a
059     * href="http://camel.apache.org/type-converter.html">Type Converters</a>
060     *
061     * @version $Revision: 749236 $
062     */
063    @Converter
064    public final class IOConverter {
065        private static final transient Log LOG = LogFactory.getLog(IOConverter.class);
066        private static XmlConverter xmlConverter;
067    
068        /**
069         * Utility classes should not have a public constructor.
070         */
071        private IOConverter() {
072        }
073    
074        @Converter
075        public static InputStream toInputStream(URL url) throws IOException {
076            return url.openStream();
077        }
078    
079        @Converter
080        public static InputStream toInputStream(File file) throws FileNotFoundException {
081            return new BufferedInputStream(new FileInputStream(file));
082        }
083    
084        @Converter
085        public static BufferedReader toReader(File file) throws FileNotFoundException {
086            return new BufferedReader(new FileReader(file));
087        }
088    
089        @Converter
090        public static File toFile(String name) throws FileNotFoundException {
091            return new File(name);
092        }
093    
094        @Converter
095        public static OutputStream toOutputStream(File file) throws FileNotFoundException {
096            return new BufferedOutputStream(new FileOutputStream(file));
097        }
098    
099        @Converter
100        public static BufferedWriter toWriter(File file) throws IOException {
101            return new BufferedWriter(new FileWriter(file));
102        }
103    
104        @Converter
105        public static Reader toReader(InputStream in) {
106            return new InputStreamReader(in);
107        }
108    
109        @Converter
110        public static Writer toWriter(OutputStream out) {
111            return new OutputStreamWriter(out);
112        }
113    
114        @Converter
115        public static StringReader toReader(String text) {
116            return new StringReader(text);
117        }
118    
119        @Converter
120        public static InputStream toInputStream(String text, Exchange exchange) {
121            if (exchange != null) {
122                String charsetName = exchange.getProperty(Exchange.CHARSET_NAME, String.class);
123                if (charsetName != null) {
124                    try {
125                        return toInputStream(text.getBytes(charsetName));
126                    } catch (UnsupportedEncodingException e) {
127                        LOG.warn("Cannot convert the String into byte[] with the charset: " + charsetName, e);
128                    }
129                }
130            }
131            return toInputStream(text.getBytes());
132        }
133        
134        @Converter
135        public static InputStream toInputStream(BufferedReader buffer, Exchange exchange) throws IOException {
136            return toInputStream(toString(buffer), exchange);
137        }
138    
139        @Converter
140        public static InputStream toInputStrean(DOMSource source) throws TransformerException, IOException {
141            XmlConverter xmlConverter = createXmlConverter();
142            ByteArrayInputStream bais = new ByteArrayInputStream(xmlConverter.toString(source).getBytes());
143            return bais;
144        }
145    
146        private static XmlConverter createXmlConverter() {
147            if (xmlConverter == null) {
148                xmlConverter = new XmlConverter();
149            }
150            return xmlConverter;
151        }
152    
153        @Converter
154        public static String toString(byte[] data, Exchange exchange) {
155            if (exchange != null) {
156                String charsetName = exchange.getProperty(Exchange.CHARSET_NAME, String.class);
157                if (charsetName != null) {
158                    try {
159                        return new String(data, charsetName);
160                    } catch (UnsupportedEncodingException e) {
161                        LOG.warn("Cannot convert the byte[] to String with the charset: " + charsetName, e);
162                    }
163                }
164            }
165            return new String(data);
166        }
167    
168    
169        @Converter
170        public static String toString(File file) throws IOException {
171            return toString(toReader(file));
172        }
173    
174        @Converter
175        public static byte[] toByteArray(File file) throws IOException {
176            return toBytes(toInputStream(file));
177        }
178    
179        @Converter
180        public static byte[] toByteArray(Reader reader) throws IOException {
181            if (reader instanceof BufferedReader) {
182                return toByteArray((BufferedReader)reader);
183            } else {
184                return toByteArray(new BufferedReader(reader));
185            }
186        }
187    
188        @Converter
189        public static String toString(URL url) throws IOException {
190            return toString(toInputStream(url));
191        }
192    
193        @Converter
194        public static String toString(Reader reader) throws IOException {
195            if (reader instanceof BufferedReader) {
196                return toString((BufferedReader)reader);
197            } else {
198                return toString(new BufferedReader(reader));
199            }
200        }
201    
202        @Converter
203        public static String toString(BufferedReader reader) throws IOException {
204            if (reader == null) {
205                return null;
206            }
207            try {
208                CollectionStringBuffer builder = new CollectionStringBuffer("\n");
209                while (true) {
210                    String line = reader.readLine();
211                    if (line == null) {
212                        return builder.toString();
213                    }
214                    builder.append(line);
215                }
216            } finally {
217                ObjectHelper.close(reader, "reader", LOG);
218            }
219        }
220    
221        @Converter
222        public static byte[] toByteArray(BufferedReader reader) throws IOException {
223            if (reader == null) {
224                return null;
225            }
226    
227            StringBuilder sb = new StringBuilder(1024);
228            char[] buf = new char[1024];
229            try {
230                int len = reader.read(buf);
231                if (len != -1) {
232                    sb.append(buf, 0, len);
233                }
234            } finally {
235                ObjectHelper.close(reader, "reader", LOG);
236            }
237    
238            return sb.toString().getBytes();
239        }
240    
241        @Converter
242        public static String toString(InputStream in) throws IOException {
243            return toString(toReader(in));
244        }
245    
246        @Converter
247        public static InputStream toInputStream(byte[] data) {
248            return new ByteArrayInputStream(data);
249        }
250    
251        @Converter
252        public static ObjectOutput toObjectOutput(OutputStream stream) throws IOException {
253            if (stream instanceof ObjectOutput) {
254                return (ObjectOutput) stream;
255            } else {
256                return new ObjectOutputStream(stream);
257            }
258        }
259    
260        @Converter
261        public static ObjectInput toObjectInput(InputStream stream) throws IOException {
262            if (stream instanceof ObjectInput) {
263                return (ObjectInput) stream;
264            } else {
265                return new ObjectInputStream(stream);
266            }
267        }
268    
269        @Converter
270        public static byte[] toBytes(InputStream stream) throws IOException {
271            ByteArrayOutputStream bos = new ByteArrayOutputStream();
272            try {
273                IOHelper.copy(stream, bos);
274                return bos.toByteArray();
275            } finally {
276                ObjectHelper.close(bos, "stream", LOG);
277            }
278        }
279    
280        @Converter
281        public static byte[] toByteArray(ByteArrayOutputStream os) {
282            return os.toByteArray();
283        }
284    
285        @Converter
286        public static String toString(ByteArrayOutputStream os) {
287            return os.toString();
288        }
289    
290        @Converter
291        public static InputStream toInputStream(ByteArrayOutputStream os) {
292            return new ByteArrayInputStream(os.toByteArray());
293        }
294    
295    }