| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
package org.apache.camel.converter; |
| 18 |
|
|
| 19 |
|
import org.apache.camel.Converter; |
| 20 |
|
import org.apache.commons.logging.Log; |
| 21 |
|
import org.apache.commons.logging.LogFactory; |
| 22 |
|
|
| 23 |
|
import java.io.*; |
| 24 |
|
import java.net.URL; |
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
@Converter |
| 33 |
|
public class IOConverter { |
| 34 |
3 |
private static final transient Log LOG = LogFactory.getLog(IOConverter.class); |
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
0 |
private IOConverter() { |
| 40 |
0 |
} |
| 41 |
|
|
| 42 |
|
@Converter |
| 43 |
|
public static InputStream toInputStream(URL url) throws IOException { |
| 44 |
0 |
return url.openStream(); |
| 45 |
|
} |
| 46 |
|
|
| 47 |
|
@Converter |
| 48 |
|
public static InputStream toInputStream(File file) throws FileNotFoundException { |
| 49 |
6 |
return new BufferedInputStream(new FileInputStream(file)); |
| 50 |
|
} |
| 51 |
|
|
| 52 |
|
@Converter |
| 53 |
|
public static BufferedReader toReader(File file) throws FileNotFoundException { |
| 54 |
15 |
return new BufferedReader(new FileReader(file)); |
| 55 |
|
} |
| 56 |
|
|
| 57 |
|
@Converter |
| 58 |
|
public static File toFile(String name) throws FileNotFoundException { |
| 59 |
3 |
return new File(name); |
| 60 |
|
} |
| 61 |
|
|
| 62 |
|
@Converter |
| 63 |
|
public static OutputStream toOutputStream(File file) throws FileNotFoundException { |
| 64 |
0 |
return new BufferedOutputStream(new FileOutputStream(file)); |
| 65 |
|
} |
| 66 |
|
|
| 67 |
|
@Converter |
| 68 |
|
public static BufferedWriter toWriter(File file) throws IOException { |
| 69 |
0 |
return new BufferedWriter(new FileWriter(file)); |
| 70 |
|
} |
| 71 |
|
|
| 72 |
|
@Converter |
| 73 |
|
public static Reader toReader(InputStream in) throws FileNotFoundException { |
| 74 |
3 |
return new InputStreamReader(in); |
| 75 |
|
} |
| 76 |
|
|
| 77 |
|
@Converter |
| 78 |
|
public static Writer toWriter(OutputStream out) throws FileNotFoundException { |
| 79 |
0 |
return new OutputStreamWriter(out); |
| 80 |
|
} |
| 81 |
|
|
| 82 |
|
@Converter |
| 83 |
|
public static StringReader toReader(String text) { |
| 84 |
|
|
| 85 |
0 |
return new StringReader(text); |
| 86 |
|
} |
| 87 |
|
|
| 88 |
|
@Converter |
| 89 |
|
public static InputStream toInputStream(String text) { |
| 90 |
15 |
return toInputStream(text.getBytes()); |
| 91 |
|
} |
| 92 |
|
|
| 93 |
|
@Converter |
| 94 |
|
public static byte[] toByteArray(String text) { |
| 95 |
|
|
| 96 |
3 |
return text.getBytes(); |
| 97 |
|
} |
| 98 |
|
|
| 99 |
|
@Converter |
| 100 |
|
public static String toString(byte[] data) { |
| 101 |
3 |
return new String(data); |
| 102 |
|
} |
| 103 |
|
|
| 104 |
|
@Converter |
| 105 |
|
public static String toString(File file) throws IOException { |
| 106 |
15 |
return toString(toReader(file)); |
| 107 |
|
} |
| 108 |
|
|
| 109 |
|
@Converter |
| 110 |
|
public static String toString(URL url) throws IOException { |
| 111 |
0 |
return toString(toInputStream(url)); |
| 112 |
|
} |
| 113 |
|
|
| 114 |
|
@Converter |
| 115 |
|
public static String toString(Reader reader) throws IOException { |
| 116 |
3 |
if (reader instanceof BufferedReader) { |
| 117 |
0 |
return toString((BufferedReader)reader); |
| 118 |
|
} else { |
| 119 |
3 |
return toString(new BufferedReader(reader)); |
| 120 |
|
} |
| 121 |
|
} |
| 122 |
|
|
| 123 |
|
@Converter |
| 124 |
|
public static String toString(BufferedReader reader) throws IOException { |
| 125 |
18 |
if (reader == null) { |
| 126 |
0 |
return null; |
| 127 |
|
} |
| 128 |
|
try { |
| 129 |
18 |
StringBuilder builder = new StringBuilder(); |
| 130 |
18 |
boolean first = true; |
| 131 |
|
while (true) { |
| 132 |
87 |
String line = reader.readLine(); |
| 133 |
87 |
if (line == null) { |
| 134 |
18 |
return builder.toString(); |
| 135 |
|
} |
| 136 |
69 |
if (first) { |
| 137 |
18 |
first = false; |
| 138 |
18 |
} else { |
| 139 |
51 |
builder.append("\n"); |
| 140 |
|
} |
| 141 |
69 |
builder.append(line); |
| 142 |
69 |
} |
| 143 |
|
} finally { |
| 144 |
0 |
try { |
| 145 |
18 |
reader.close(); |
| 146 |
0 |
} catch (IOException e) { |
| 147 |
0 |
LOG.warn("Failed to close stream: " + e, e); |
| 148 |
18 |
} |
| 149 |
18 |
} |
| 150 |
|
} |
| 151 |
|
|
| 152 |
|
@Converter |
| 153 |
|
public static String toString(InputStream in) throws IOException { |
| 154 |
3 |
return toString(toReader(in)); |
| 155 |
|
} |
| 156 |
|
|
| 157 |
|
@Converter |
| 158 |
|
public static InputStream toInputStream(byte[] data) { |
| 159 |
15 |
return new ByteArrayInputStream(data); |
| 160 |
|
} |
| 161 |
|
|
| 162 |
|
} |