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 |
|
|
21 |
|
import java.io.BufferedInputStream; |
22 |
|
import java.io.File; |
23 |
|
import java.io.FileInputStream; |
24 |
|
import java.io.IOException; |
25 |
|
import java.io.InputStream; |
26 |
|
import java.nio.ByteBuffer; |
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
@Converter |
35 |
|
public class NIOConverter { |
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
0 |
private NIOConverter() { |
41 |
0 |
} |
42 |
|
|
43 |
|
@Converter |
44 |
|
public static byte[] toByteArray(ByteBuffer buffer) { |
45 |
0 |
return buffer.array(); |
46 |
|
} |
47 |
|
|
48 |
|
@Converter |
49 |
|
public static String toString(ByteBuffer buffer) { |
50 |
0 |
return IOConverter.toString(buffer.array()); |
51 |
|
} |
52 |
|
|
53 |
|
@Converter |
54 |
|
public static ByteBuffer toByteBuffer(byte[] data) { |
55 |
0 |
return ByteBuffer.wrap(data); |
56 |
|
} |
57 |
|
|
58 |
|
@Converter |
59 |
|
public static ByteBuffer toByteBuffer(File file) throws IOException { |
60 |
0 |
byte[] buf = new byte[(int) file.length()]; |
61 |
0 |
InputStream in = new BufferedInputStream(new FileInputStream(file)); |
62 |
0 |
in.read(buf); |
63 |
0 |
return ByteBuffer.wrap(buf); |
64 |
|
} |
65 |
|
|
66 |
|
@Converter |
67 |
|
public static ByteBuffer toByteBuffer(String value) { |
68 |
0 |
ByteBuffer buf = ByteBuffer.allocate(value.length()); |
69 |
0 |
byte[] bytes = value.getBytes(); |
70 |
0 |
buf.put(bytes); |
71 |
0 |
return buf; |
72 |
|
} |
73 |
|
@Converter |
74 |
|
public static ByteBuffer toByteBuffer(Short value) { |
75 |
0 |
ByteBuffer buf = ByteBuffer.allocate(2); |
76 |
0 |
buf.putShort(value); |
77 |
0 |
return buf; |
78 |
|
} |
79 |
|
@Converter |
80 |
|
public static ByteBuffer toByteBuffer(Integer value) { |
81 |
0 |
ByteBuffer buf = ByteBuffer.allocate(4); |
82 |
0 |
buf.putInt(value); |
83 |
0 |
return buf; |
84 |
|
} |
85 |
|
@Converter |
86 |
|
public static ByteBuffer toByteBuffer(Long value) { |
87 |
0 |
ByteBuffer buf = ByteBuffer.allocate(8); |
88 |
0 |
buf.putLong(value); |
89 |
0 |
return buf; |
90 |
|
} |
91 |
|
@Converter |
92 |
|
public static ByteBuffer toByteBuffer(Float value) { |
93 |
0 |
ByteBuffer buf = ByteBuffer.allocate(4); |
94 |
0 |
buf.putFloat(value); |
95 |
0 |
return buf; |
96 |
|
} |
97 |
|
@Converter |
98 |
|
public static ByteBuffer toByteBuffer(Double value) { |
99 |
0 |
ByteBuffer buf = ByteBuffer.allocate(8); |
100 |
0 |
buf.putDouble(value); |
101 |
0 |
return buf; |
102 |
|
} |
103 |
|
|
104 |
|
@Converter |
105 |
|
public static InputStream toInputStream(ByteBuffer bufferbuffer) { |
106 |
0 |
return IOConverter.toInputStream(toByteArray(bufferbuffer)); |
107 |
|
} |
108 |
|
} |