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
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.commons.logging.Log;
054 import org.apache.commons.logging.LogFactory;
055
056 /**
057 * Some core java.io based <a
058 * href="http://activemq.apache.org/camel/type-converter.html">Type Converters</a>
059 *
060 * @version $Revision: 735741 $
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) throws FileNotFoundException {
105 return new InputStreamReader(in);
106 }
107
108 @Converter
109 public static Writer toWriter(OutputStream out) throws FileNotFoundException {
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("Can't convert the String into the bytes 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 ByteArrayInputStream bais = new ByteArrayInputStream(xmlConverter.toString(source).getBytes());
142 return bais;
143 }
144
145 private static XmlConverter createXmlConverter() {
146 if (xmlConverter == null) {
147 xmlConverter = new XmlConverter();
148 }
149 return xmlConverter;
150 }
151
152 @Converter
153 public static String toString(byte[] data, Exchange exchange) {
154 if (exchange != null) {
155 String charsetName = exchange.getProperty(Exchange.CHARSET_NAME, String.class);
156 if (charsetName != null) {
157 try {
158 return new String(data, charsetName);
159 } catch (UnsupportedEncodingException e) {
160 LOG.warn("Can't convert the byte to String with the charset " + charsetName, e);
161 }
162 }
163 }
164 return new String(data);
165 }
166
167
168 @Converter
169 public static String toString(File file) throws IOException {
170 return toString(toReader(file));
171 }
172
173 @Converter
174 public static byte[] toByteArray(File file) throws IOException {
175 return toBytes(toInputStream(file));
176 }
177
178 @Converter
179 public static byte[] toByteArray(Reader reader) throws IOException {
180 if (reader instanceof BufferedReader) {
181 return toByteArray((BufferedReader)reader);
182 } else {
183 return toByteArray(new BufferedReader(reader));
184 }
185 }
186
187 @Converter
188 public static String toString(URL url) throws IOException {
189 return toString(toInputStream(url));
190 }
191
192 @Converter
193 public static String toString(Reader reader) throws IOException {
194 if (reader instanceof BufferedReader) {
195 return toString((BufferedReader)reader);
196 } else {
197 return toString(new BufferedReader(reader));
198 }
199 }
200
201 @Converter
202 public static String toString(BufferedReader reader) throws IOException {
203 if (reader == null) {
204 return null;
205 }
206 try {
207 CollectionStringBuffer builder = new CollectionStringBuffer("\n");
208 while (true) {
209 String line = reader.readLine();
210 if (line == null) {
211 return builder.toString();
212 }
213 builder.append(line);
214 }
215 } finally {
216 try {
217 reader.close();
218 } catch (IOException e) {
219 LOG.warn("Failed to close stream: " + e, e);
220 }
221 }
222 }
223
224 @Converter
225 public static byte[] toByteArray(BufferedReader reader) throws IOException {
226 if (reader == null) {
227 return null;
228 }
229
230 StringBuilder sb = new StringBuilder(1024);
231 char[] buf = new char[1024];
232 try {
233 int len = reader.read(buf);
234 if (len != -1) {
235 sb.append(buf, 0, len);
236 }
237 } finally {
238 try {
239 reader.close();
240 } catch (IOException e) {
241 LOG.warn("Failed to close stream: " + e, e);
242 }
243 }
244 return sb.toString().getBytes();
245 }
246
247 @Converter
248 public static String toString(InputStream in) throws IOException {
249 return toString(toReader(in));
250 }
251
252 @Converter
253 public static InputStream toInputStream(byte[] data) {
254 return new ByteArrayInputStream(data);
255 }
256
257 @Converter
258 public static ObjectOutput toObjectOutput(OutputStream stream) throws IOException {
259 if (stream instanceof ObjectOutput) {
260 return (ObjectOutput) stream;
261 } else {
262 return new ObjectOutputStream(stream);
263 }
264 }
265
266 @Converter
267 public static ObjectInput toObjectInput(InputStream stream) throws IOException {
268 if (stream instanceof ObjectInput) {
269 return (ObjectInput) stream;
270 } else {
271 return new ObjectInputStream(stream);
272 }
273 }
274
275 @Converter
276 public static byte[] toBytes(InputStream stream) throws IOException {
277 ByteArrayOutputStream bos = new ByteArrayOutputStream();
278 copy(stream, bos);
279 return bos.toByteArray();
280 }
281
282 public static void copy(InputStream stream, OutputStream os) throws IOException {
283 byte[] data = new byte[4096];
284 int read = stream.read(data);
285 while (read != -1) {
286 os.write(data, 0, read);
287 read = stream.read(data);
288 }
289 os.flush();
290 }
291 }