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.CollectionStringBuffer; 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://activemq.apache.org/camel/type-converter.html">Type Converters</a> 059 * 060 * @version $Revision: 69549 $ 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("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 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("Cannot 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 ObjectHelper.close(reader, "reader", LOG); 217 } 218 } 219 220 @Converter 221 public static byte[] toByteArray(BufferedReader reader) throws IOException { 222 if (reader == null) { 223 return null; 224 } 225 226 StringBuilder sb = new StringBuilder(1024); 227 char[] buf = new char[1024]; 228 try { 229 int len = reader.read(buf); 230 if (len != -1) { 231 sb.append(buf, 0, len); 232 } 233 } finally { 234 ObjectHelper.close(reader, "reader", LOG); 235 } 236 237 return sb.toString().getBytes(); 238 } 239 240 @Converter 241 public static String toString(InputStream in) throws IOException { 242 return toString(toReader(in)); 243 } 244 245 @Converter 246 public static InputStream toInputStream(byte[] data) { 247 return new ByteArrayInputStream(data); 248 } 249 250 @Converter 251 public static ObjectOutput toObjectOutput(OutputStream stream) throws IOException { 252 if (stream instanceof ObjectOutput) { 253 return (ObjectOutput) stream; 254 } else { 255 return new ObjectOutputStream(stream); 256 } 257 } 258 259 @Converter 260 public static ObjectInput toObjectInput(InputStream stream) throws IOException { 261 if (stream instanceof ObjectInput) { 262 return (ObjectInput) stream; 263 } else { 264 return new ObjectInputStream(stream); 265 } 266 } 267 268 @Converter 269 public static byte[] toBytes(InputStream stream) throws IOException { 270 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 271 try { 272 copy(stream, bos); 273 return bos.toByteArray(); 274 } finally { 275 ObjectHelper.close(bos, "stream", LOG); 276 } 277 } 278 279 public static void copy(InputStream stream, OutputStream os) throws IOException { 280 byte[] data = new byte[4096]; 281 int read = stream.read(data); 282 while (read != -1) { 283 os.write(data, 0, read); 284 read = stream.read(data); 285 } 286 os.flush(); 287 } 288 }