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