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.Writer; 043 import java.net.URL; 044 import java.util.Properties; 045 046 import javax.xml.transform.OutputKeys; 047 import javax.xml.transform.Source; 048 import javax.xml.transform.Transformer; 049 import javax.xml.transform.TransformerException; 050 import javax.xml.transform.TransformerFactory; 051 import javax.xml.transform.dom.DOMSource; 052 import javax.xml.transform.stream.StreamResult; 053 054 import org.apache.camel.Converter; 055 import org.apache.camel.util.CollectionStringBuffer; 056 import org.apache.commons.logging.Log; 057 import org.apache.commons.logging.LogFactory; 058 059 /** 060 * Some core java.io based <a 061 * href="http://activemq.apache.org/camel/type-converter.html">Type Converters</a> 062 * 063 * @version $Revision: 36565 $ 064 */ 065 @Converter 066 public final class IOConverter { 067 private static final transient Log LOG = LogFactory.getLog(IOConverter.class); 068 069 /** 070 * Utility classes should not have a public constructor. 071 */ 072 private IOConverter() { 073 } 074 075 @Converter 076 public static InputStream toInputStream(URL url) throws IOException { 077 return url.openStream(); 078 } 079 080 @Converter 081 public static InputStream toInputStream(File file) throws FileNotFoundException { 082 return new BufferedInputStream(new FileInputStream(file)); 083 } 084 085 @Converter 086 public static BufferedReader toReader(File file) throws FileNotFoundException { 087 return new BufferedReader(new FileReader(file)); 088 } 089 090 @Converter 091 public static File toFile(String name) throws FileNotFoundException { 092 return new File(name); 093 } 094 095 @Converter 096 public static OutputStream toOutputStream(File file) throws FileNotFoundException { 097 return new BufferedOutputStream(new FileOutputStream(file)); 098 } 099 100 @Converter 101 public static BufferedWriter toWriter(File file) throws IOException { 102 return new BufferedWriter(new FileWriter(file)); 103 } 104 105 @Converter 106 public static Reader toReader(InputStream in) throws FileNotFoundException { 107 return new InputStreamReader(in); 108 } 109 110 @Converter 111 public static Writer toWriter(OutputStream out) throws FileNotFoundException { 112 return new OutputStreamWriter(out); 113 } 114 115 @Converter 116 public static StringReader toReader(String text) { 117 // TODO could we automatically find this? 118 return new StringReader(text); 119 } 120 121 @Converter 122 public static InputStream toInputStream(String text) { 123 return toInputStream(text.getBytes()); 124 } 125 126 @Converter 127 public static InputStream toInputStream(BufferedReader buffer) throws IOException { 128 return toInputStream(toString(buffer)); 129 } 130 131 @Converter 132 public static InputStream toInputStrean(DOMSource source) throws TransformerException, IOException { 133 ByteArrayInputStream bais = new ByteArrayInputStream(toString(source).getBytes()); 134 return bais; 135 } 136 137 @Converter 138 public static String toString(byte[] data) { 139 return new String(data); 140 } 141 142 @Converter 143 public static String toString(File file) throws IOException { 144 return toString(toReader(file)); 145 } 146 147 @Converter 148 public static String toString(URL url) throws IOException { 149 return toString(toInputStream(url)); 150 } 151 152 @Converter 153 public static String toString(Reader reader) throws IOException { 154 if (reader instanceof BufferedReader) { 155 return toString((BufferedReader)reader); 156 } else { 157 return toString(new BufferedReader(reader)); 158 } 159 } 160 161 @Converter 162 public static String toString(BufferedReader reader) throws IOException { 163 if (reader == null) { 164 return null; 165 } 166 try { 167 CollectionStringBuffer builder = new CollectionStringBuffer("\n"); 168 while (true) { 169 String line = reader.readLine(); 170 if (line == null) { 171 return builder.toString(); 172 } 173 builder.append(line); 174 } 175 } finally { 176 try { 177 reader.close(); 178 } catch (IOException e) { 179 LOG.warn("Failed to close stream: " + e, e); 180 } 181 } 182 } 183 184 @Converter 185 public static String toString(InputStream in) throws IOException { 186 return toString(toReader(in)); 187 } 188 189 public static String toString(Source source) throws TransformerException, IOException { 190 return toString(source, null); 191 } 192 193 public static String toString(Source source, Properties props) throws TransformerException, IOException { 194 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 195 StreamResult sr = new StreamResult(bos); 196 Transformer trans = TransformerFactory.newInstance().newTransformer(); 197 if (props == null) { 198 props = new Properties(); 199 props.put(OutputKeys.OMIT_XML_DECLARATION, "yes"); 200 } 201 trans.setOutputProperties(props); 202 trans.transform(source, sr); 203 bos.close(); 204 return bos.toString(); 205 } 206 207 @Converter 208 public static InputStream toInputStream(byte[] data) { 209 return new ByteArrayInputStream(data); 210 } 211 212 @Converter 213 public static ObjectOutput toObjectOutput(OutputStream stream) throws IOException { 214 if (stream instanceof ObjectOutput) { 215 return (ObjectOutput) stream; 216 } else { 217 return new ObjectOutputStream(stream); 218 } 219 } 220 221 @Converter 222 public static ObjectInput toObjectInput(InputStream stream) throws IOException { 223 if (stream instanceof ObjectInput) { 224 return (ObjectInput) stream; 225 } else { 226 return new ObjectInputStream(stream); 227 } 228 } 229 230 @Converter 231 public static byte[] toBytes(InputStream stream) throws IOException { 232 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 233 copy(stream, bos); 234 return bos.toByteArray(); 235 } 236 237 protected static void copy(InputStream stream, ByteArrayOutputStream bos) throws IOException { 238 byte[] data = new byte[4096]; 239 int read = stream.read(data); 240 while (read != -1) { 241 bos.write(data, 0, read); 242 read = stream.read(data); 243 } 244 bos.flush(); 245 } 246 }