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