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 org.apache.camel.Converter;
020 import org.apache.commons.logging.Log;
021 import org.apache.commons.logging.LogFactory;
022
023 import java.io.*;
024 import java.net.URL;
025
026 /**
027 * Some core java.io based <a
028 * href="http://activemq.apache.org/camel/type-converter.html">Type Converters</a>
029 *
030 * @version $Revision: 564167 $
031 */
032 @Converter
033 public class IOConverter {
034 private static final transient Log LOG = LogFactory.getLog(IOConverter.class);
035
036 /**
037 * Utility classes should not have a public constructor.
038 */
039 private IOConverter() {
040 }
041
042 @Converter
043 public static InputStream toInputStream(URL url) throws IOException {
044 return url.openStream();
045 }
046
047 @Converter
048 public static InputStream toInputStream(File file) throws FileNotFoundException {
049 return new BufferedInputStream(new FileInputStream(file));
050 }
051
052 @Converter
053 public static BufferedReader toReader(File file) throws FileNotFoundException {
054 return new BufferedReader(new FileReader(file));
055 }
056
057 @Converter
058 public static File toFile(String name) throws FileNotFoundException {
059 return new File(name);
060 }
061
062 @Converter
063 public static OutputStream toOutputStream(File file) throws FileNotFoundException {
064 return new BufferedOutputStream(new FileOutputStream(file));
065 }
066
067 @Converter
068 public static BufferedWriter toWriter(File file) throws IOException {
069 return new BufferedWriter(new FileWriter(file));
070 }
071
072 @Converter
073 public static Reader toReader(InputStream in) throws FileNotFoundException {
074 return new InputStreamReader(in);
075 }
076
077 @Converter
078 public static Writer toWriter(OutputStream out) throws FileNotFoundException {
079 return new OutputStreamWriter(out);
080 }
081
082 @Converter
083 public static StringReader toReader(String text) {
084 // TODO could we automatically find this?
085 return new StringReader(text);
086 }
087
088 @Converter
089 public static InputStream toInputStream(String text) {
090 return toInputStream(text.getBytes());
091 }
092
093 @Converter
094 public static byte[] toByteArray(String text) {
095 // TODO could we automatically find this?
096 return text.getBytes();
097 }
098
099 @Converter
100 public static String toString(byte[] data) {
101 return new String(data);
102 }
103
104 @Converter
105 public static String toString(File file) throws IOException {
106 return toString(toReader(file));
107 }
108
109 @Converter
110 public static String toString(URL url) throws IOException {
111 return toString(toInputStream(url));
112 }
113
114 @Converter
115 public static String toString(Reader reader) throws IOException {
116 if (reader instanceof BufferedReader) {
117 return toString((BufferedReader)reader);
118 } else {
119 return toString(new BufferedReader(reader));
120 }
121 }
122
123 @Converter
124 public static String toString(BufferedReader reader) throws IOException {
125 if (reader == null) {
126 return null;
127 }
128 try {
129 StringBuilder builder = new StringBuilder();
130 boolean first = true;
131 while (true) {
132 String line = reader.readLine();
133 if (line == null) {
134 return builder.toString();
135 }
136 if (first) {
137 first = false;
138 } else {
139 builder.append("\n");
140 }
141 builder.append(line);
142 }
143 } finally {
144 try {
145 reader.close();
146 } catch (IOException e) {
147 LOG.warn("Failed to close stream: " + e, e);
148 }
149 }
150 }
151
152 @Converter
153 public static String toString(InputStream in) throws IOException {
154 return toString(toReader(in));
155 }
156
157 @Converter
158 public static InputStream toInputStream(byte[] data) {
159 return new ByteArrayInputStream(data);
160 }
161
162 }