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