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: 65759 $ 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) { 120 return toInputStream(text, null); 121 } 122 123 @Converter 124 public static InputStream toInputStream(String text, Exchange exchange) { 125 if (exchange != null) { 126 String charsetName = exchange.getProperty(Exchange.CHARSET_NAME, String.class); 127 if (charsetName != null) { 128 try { 129 return toInputStream(text.getBytes(charsetName)); 130 } catch (UnsupportedEncodingException e) { 131 LOG.warn("Can't convert the String into the bytes with the charset " + charsetName, e); 132 } 133 } 134 } 135 return toInputStream(text.getBytes()); 136 } 137 138 @Converter 139 public static InputStream toInputStream(BufferedReader buffer) throws IOException { 140 return toInputStream(toString(buffer), null); 141 } 142 143 @Converter 144 public static InputStream toInputStream(BufferedReader buffer, Exchange exchange) throws IOException { 145 return toInputStream(toString(buffer), exchange); 146 } 147 148 @Converter 149 public static InputStream toInputStrean(DOMSource source) throws TransformerException, IOException { 150 XmlConverter xmlConverter = createXmlConverter(); 151 ByteArrayInputStream bais = new ByteArrayInputStream(xmlConverter.toString(source).getBytes()); 152 return bais; 153 } 154 155 private static XmlConverter createXmlConverter() { 156 if (xmlConverter == null) { 157 xmlConverter = new XmlConverter(); 158 } 159 return xmlConverter; 160 } 161 162 @Converter 163 public static String toString(byte[] data, Exchange exchange) { 164 if (exchange != null) { 165 String charsetName = exchange.getProperty(Exchange.CHARSET_NAME, String.class); 166 if (charsetName != null) { 167 try { 168 return new String(data, charsetName); 169 } catch (UnsupportedEncodingException e) { 170 LOG.warn("Can't convert the byte to String with the charset " + charsetName, e); 171 } 172 } 173 } 174 return new String(data); 175 } 176 177 178 @Converter 179 public static String toString(File file) throws IOException { 180 return toString(toReader(file)); 181 } 182 183 @Converter 184 public static byte[] toByteArray(File file) throws IOException { 185 return toBytes(toInputStream(file)); 186 } 187 188 @Converter 189 public static byte[] toByteArray(Reader reader) throws IOException { 190 if (reader instanceof BufferedReader) { 191 return toByteArray((BufferedReader)reader); 192 } else { 193 return toByteArray(new BufferedReader(reader)); 194 } 195 } 196 197 @Converter 198 public static String toString(URL url) throws IOException { 199 return toString(toInputStream(url)); 200 } 201 202 @Converter 203 public static String toString(Reader reader) throws IOException { 204 if (reader instanceof BufferedReader) { 205 return toString((BufferedReader)reader); 206 } else { 207 return toString(new BufferedReader(reader)); 208 } 209 } 210 211 @Converter 212 public static String toString(BufferedReader reader) throws IOException { 213 if (reader == null) { 214 return null; 215 } 216 try { 217 CollectionStringBuffer builder = new CollectionStringBuffer("\n"); 218 while (true) { 219 String line = reader.readLine(); 220 if (line == null) { 221 return builder.toString(); 222 } 223 builder.append(line); 224 } 225 } finally { 226 try { 227 reader.close(); 228 } catch (IOException e) { 229 LOG.warn("Failed to close stream: " + e, e); 230 } 231 } 232 } 233 234 @Converter 235 public static byte[] toByteArray(BufferedReader reader) throws IOException { 236 if (reader == null) { 237 return null; 238 } 239 240 StringBuilder sb = new StringBuilder(1024); 241 char[] buf = new char[1024]; 242 try { 243 int len = reader.read(buf); 244 if (len != -1) { 245 sb.append(buf, 0, len); 246 } 247 } finally { 248 try { 249 reader.close(); 250 } catch (IOException e) { 251 LOG.warn("Failed to close stream: " + e, e); 252 } 253 } 254 return sb.toString().getBytes(); 255 } 256 257 @Converter 258 public static String toString(InputStream in) throws IOException { 259 return toString(toReader(in)); 260 } 261 262 @Converter 263 public static InputStream toInputStream(byte[] data) { 264 return new ByteArrayInputStream(data); 265 } 266 267 @Converter 268 public static ObjectOutput toObjectOutput(OutputStream stream) throws IOException { 269 if (stream instanceof ObjectOutput) { 270 return (ObjectOutput) stream; 271 } else { 272 return new ObjectOutputStream(stream); 273 } 274 } 275 276 @Converter 277 public static ObjectInput toObjectInput(InputStream stream) throws IOException { 278 if (stream instanceof ObjectInput) { 279 return (ObjectInput) stream; 280 } else { 281 return new ObjectInputStream(stream); 282 } 283 } 284 285 @Converter 286 public static byte[] toBytes(InputStream stream) throws IOException { 287 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 288 copy(stream, bos); 289 return bos.toByteArray(); 290 } 291 292 public static void copy(InputStream stream, OutputStream os) throws IOException { 293 byte[] data = new byte[4096]; 294 int read = stream.read(data); 295 while (read != -1) { 296 os.write(data, 0, read); 297 read = stream.read(data); 298 } 299 os.flush(); 300 } 301 }