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