use of org.apache.mina.common.ByteBuffer in project camel by apache.
the class MinaConverter method toByteBuffer.
@Converter
public static ByteBuffer toByteBuffer(byte[] bytes) {
ByteBuffer buf = ByteBuffer.allocate(bytes.length);
buf.put(bytes);
return buf;
}
use of org.apache.mina.common.ByteBuffer in project camel by apache.
the class MinaUdpProtocolCodecFactory method toByteBuffer.
private ByteBuffer toByteBuffer(Object message) throws CharacterCodingException, NoTypeConversionAvailableException {
// try to convert it to a byte array
byte[] value = context.getTypeConverter().tryConvertTo(byte[].class, message);
if (value != null) {
ByteBuffer answer = ByteBuffer.allocate(value.length).setAutoExpand(false);
answer.put(value);
return answer;
}
// fallback to use a byte buffer converter
return context.getTypeConverter().mandatoryConvertTo(ByteBuffer.class, message);
}
use of org.apache.mina.common.ByteBuffer in project camel by apache.
the class MinaConverterTest method testToByteArray.
public void testToByteArray() {
byte[] in = "Hello World".getBytes();
ByteBuffer bb = ByteBuffer.wrap(in);
byte[] out = MinaConverter.toByteArray(bb);
for (int i = 0; i < out.length; i++) {
assertEquals(in[i], out[i]);
}
}
use of org.apache.mina.common.ByteBuffer in project camel by apache.
the class MinaConverterTest method testToInputStream.
public void testToInputStream() throws Exception {
byte[] in = "Hello World".getBytes();
ByteBuffer bb = ByteBuffer.wrap(in);
InputStream is = MinaConverter.toInputStream(bb);
for (byte b : in) {
int out = is.read();
assertEquals(b, out);
}
}
use of org.apache.mina.common.ByteBuffer in project camel by apache.
the class MinaConverterTest method testToStringTwoTimes.
public void testToStringTwoTimes() throws UnsupportedEncodingException {
String in = "Hello World 你好";
ByteBuffer bb = ByteBuffer.wrap(in.getBytes("UTF-8"));
Exchange exchange = new DefaultExchange(new DefaultCamelContext());
exchange.setProperty(Exchange.CHARSET_NAME, "UTF-8");
String out = MinaConverter.toString(bb, exchange);
assertEquals("Hello World 你好", out);
// should be possible to convert to string without affecting the ByteBuffer
out = MinaConverter.toString(bb, exchange);
assertEquals("Hello World 你好", out);
}
Aggregations