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.IOException;
030 import java.io.InputStream;
031 import java.io.InputStreamReader;
032 import java.io.ObjectInput;
033 import java.io.ObjectInputStream;
034 import java.io.ObjectOutput;
035 import java.io.ObjectOutputStream;
036 import java.io.OutputStream;
037 import java.io.OutputStreamWriter;
038 import java.io.Reader;
039 import java.io.StringReader;
040 import java.io.UnsupportedEncodingException;
041 import java.io.Writer;
042 import java.net.URL;
043
044 import org.apache.camel.Converter;
045 import org.apache.camel.Exchange;
046 import org.apache.camel.util.IOHelper;
047 import org.apache.camel.util.ObjectHelper;
048 import org.apache.commons.logging.Log;
049 import org.apache.commons.logging.LogFactory;
050
051 /**
052 * Some core java.io based <a
053 * href="http://camel.apache.org/type-converter.html">Type Converters</a>
054 *
055 * @version $Revision: 19930 $
056 */
057 @Converter
058 public final class IOConverter {
059 private static final transient Log LOG = LogFactory.getLog(IOConverter.class);
060
061 /**
062 * Utility classes should not have a public constructor.
063 */
064 private IOConverter() {
065 }
066
067 @Converter
068 public static InputStream toInputStream(URL url) throws IOException {
069 return url.openStream();
070 }
071
072 @Converter
073 public static InputStream toInputStream(File file) throws IOException {
074 return new BufferedInputStream(new FileInputStream(file));
075 }
076
077 @Deprecated
078 public static BufferedReader toReader(File file) throws IOException {
079 return toReader(file, null);
080 }
081
082 @Converter
083 public static BufferedReader toReader(File file, Exchange exchange) throws IOException {
084 return new BufferedReader(new EncodingFileReader(file, getCharsetName(exchange)));
085 }
086
087 @Converter
088 public static File toFile(String name) throws FileNotFoundException {
089 return new File(name);
090 }
091
092 @Converter
093 public static OutputStream toOutputStream(File file) throws FileNotFoundException {
094 return new BufferedOutputStream(new FileOutputStream(file));
095 }
096
097 @Deprecated
098 public static BufferedWriter toWriter(File file) throws IOException {
099 return toWriter(file, null);
100 }
101
102 @Converter
103 public static BufferedWriter toWriter(File file, Exchange exchange) throws IOException {
104 return new BufferedWriter(new EncodingFileWriter(file, getCharsetName(exchange)));
105 }
106
107 @Deprecated
108 public static Reader toReader(InputStream in) throws IOException {
109 return toReader(in, null);
110 }
111
112 @Converter
113 public static Reader toReader(InputStream in, Exchange exchange) throws IOException {
114 return new InputStreamReader(in, getCharsetName(exchange));
115 }
116
117 @Deprecated
118 public static Writer toWriter(OutputStream out) throws IOException {
119 return toWriter(out, null);
120 }
121
122 @Converter
123 @Deprecated
124 public static Writer toWriter(OutputStream out, Exchange exchange) throws IOException {
125 return new OutputStreamWriter(out, getCharsetName(exchange));
126 }
127
128 @Converter
129 public static StringReader toReader(String text) {
130 return new StringReader(text);
131 }
132
133 @Deprecated
134 public static InputStream toInputStream(String text) throws IOException {
135 return toInputStream(text, null);
136 }
137
138 @Converter
139 public static InputStream toInputStream(String text, Exchange exchange) throws IOException {
140 return toInputStream(text.getBytes(getCharsetName(exchange)));
141 }
142
143 @Deprecated
144 public static InputStream toInputStream(BufferedReader buffer) throws IOException {
145 return toInputStream(buffer, null);
146 }
147
148 @Converter
149 public static InputStream toInputStream(BufferedReader buffer, Exchange exchange) throws IOException {
150 return toInputStream(toString(buffer), exchange);
151 }
152
153 @Deprecated
154 public static String toString(byte[] data) throws IOException {
155 return toString(data, null);
156 }
157
158 @Converter
159 public static String toString(byte[] data, Exchange exchange) throws IOException {
160 return new String(data, getCharsetName(exchange));
161 }
162
163 @Deprecated
164 public static String toString(File file) throws IOException {
165 return toString(file, null);
166 }
167
168 @Converter
169 public static String toString(File file, Exchange exchange) throws IOException {
170 return toString(toReader(file, exchange));
171 }
172
173 @Converter
174 public static byte[] toByteArray(File file) throws IOException {
175 InputStream is = toInputStream(file);
176 try {
177 return toBytes(is);
178 } finally {
179 IOHelper.close(is, "file", LOG);
180 }
181 }
182
183 @Deprecated
184 public static byte[] toByteArray(Reader reader) throws IOException {
185 return toByteArray(reader, null);
186 }
187
188 @Converter
189 public static byte[] toByteArray(Reader reader, Exchange exchange) throws IOException {
190 if (reader instanceof BufferedReader) {
191 return toByteArray((BufferedReader)reader, exchange);
192 } else {
193 return toByteArray(new BufferedReader(reader), exchange);
194 }
195 }
196
197 @Deprecated
198 public static String toString(URL url) throws IOException {
199 return toString(url, null);
200 }
201
202 @Converter
203 public static String toString(URL url, Exchange exchange) throws IOException {
204 InputStream is = toInputStream(url);
205 try {
206 return toString(is, exchange);
207 } finally {
208 IOHelper.close(is, "url", LOG);
209 }
210 }
211
212 @Converter
213 public static String toString(Reader reader) throws IOException {
214 if (reader instanceof BufferedReader) {
215 return toString((BufferedReader)reader);
216 } else {
217 return toString(new BufferedReader(reader));
218 }
219 }
220
221 @Converter
222 public static String toString(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 = 0;
231 // read until we reach then end which is the -1 marker
232 while (len != -1) {
233 len = reader.read(buf);
234 if (len != -1) {
235 sb.append(buf, 0, len);
236 }
237 }
238 } finally {
239 IOHelper.close(reader, "reader", LOG);
240 }
241
242 return sb.toString();
243 }
244
245 @Deprecated
246 public static byte[] toByteArray(BufferedReader reader) throws IOException {
247 return toByteArray(reader, null);
248 }
249
250 @Converter
251 public static byte[] toByteArray(BufferedReader reader, Exchange exchange) throws IOException {
252 return toByteArray(toString(reader), exchange);
253 }
254
255 @Deprecated
256 public static byte[] toByteArray(String value) throws IOException {
257 return toByteArray(value, null);
258 }
259
260 @Converter
261 public static byte[] toByteArray(String value, Exchange exchange) throws IOException {
262 return value != null ? value.getBytes(getCharsetName(exchange)) : null;
263 }
264
265 @Deprecated
266 public static String toString(InputStream in) throws IOException {
267 return toString(in, null);
268 }
269
270 @Converter
271 public static String toString(InputStream in, Exchange exchange) throws IOException {
272 return toString(toReader(in, exchange));
273 }
274
275 @Converter
276 public static InputStream toInputStream(byte[] data) {
277 return new ByteArrayInputStream(data);
278 }
279
280 @Converter
281 public static ObjectOutput toObjectOutput(OutputStream stream) throws IOException {
282 if (stream instanceof ObjectOutput) {
283 return (ObjectOutput) stream;
284 } else {
285 return new ObjectOutputStream(stream);
286 }
287 }
288
289 @Converter
290 public static ObjectInput toObjectInput(InputStream stream) throws IOException {
291 if (stream instanceof ObjectInput) {
292 return (ObjectInput) stream;
293 } else {
294 return new ObjectInputStream(stream);
295 }
296 }
297
298 @Converter
299 public static byte[] toBytes(InputStream stream) throws IOException {
300 ByteArrayOutputStream bos = new ByteArrayOutputStream();
301 try {
302 IOHelper.copy(stream, bos);
303 return bos.toByteArray();
304 } finally {
305 IOHelper.close(bos, "stream", LOG);
306 }
307 }
308
309 @Converter
310 public static byte[] toByteArray(ByteArrayOutputStream os) {
311 return os.toByteArray();
312 }
313
314 @Deprecated
315 public static String toString(ByteArrayOutputStream os) throws IOException {
316 return toString(os, null);
317 }
318
319 @Converter
320 public static String toString(ByteArrayOutputStream os, Exchange exchange) throws IOException {
321 return os.toString(getCharsetName(exchange));
322 }
323
324 @Converter
325 public static InputStream toInputStream(ByteArrayOutputStream os) {
326 return new ByteArrayInputStream(os.toByteArray());
327 }
328
329 public static String getCharsetName(Exchange exchange) {
330 return getCharsetName(exchange, true);
331 }
332
333 /**
334 * Gets the charset name if set as property {@link Exchange#CHARSET_NAME}.
335 *
336 * @param exchange the exchange
337 * @param useDefault should we fallback and use JVM default charset if no property existed?
338 * @return the charset, or <tt>null</tt> if no found
339 */
340 public static String getCharsetName(Exchange exchange, boolean useDefault) {
341 if (exchange != null) {
342 String charsetName = exchange.getProperty(Exchange.CHARSET_NAME, String.class);
343 if (charsetName != null) {
344 return charsetName;
345 }
346 }
347 if (useDefault) {
348 return getDefaultCharsetName();
349 } else {
350 return null;
351 }
352 }
353
354 public static String getDefaultCharsetName() {
355 return ObjectHelper.getSystemProperty(Exchange.DEFAULT_CHARSET_PROPERTY, "UTF-8");
356 }
357
358 /**
359 * Encoding-aware file reader.
360 */
361 private static class EncodingFileReader extends InputStreamReader {
362
363 /**
364 * @param file file to read
365 * @param charset character set to use
366 */
367 public EncodingFileReader(File file, String charset)
368 throws FileNotFoundException, UnsupportedEncodingException {
369 super(new FileInputStream(file), charset);
370 }
371
372 }
373
374 /**
375 * Encoding-aware file writer.
376 */
377 private static class EncodingFileWriter extends OutputStreamWriter {
378
379 /**
380 * @param file file to write
381 * @param charset character set to use
382 */
383 public EncodingFileWriter(File file, String charset)
384 throws FileNotFoundException, UnsupportedEncodingException {
385 super(new FileOutputStream(file), charset);
386 }
387
388 }
389
390 }