use of java.nio.ByteBuffer in project camel by apache.
the class NIOConverter method toByteBuffer.
@Converter
public static ByteBuffer toByteBuffer(String value, Exchange exchange) {
ByteBuffer buf = ByteBuffer.allocate(value.length());
byte[] bytes = null;
if (exchange != null) {
String charsetName = exchange.getProperty(Exchange.CHARSET_NAME, String.class);
if (charsetName != null) {
try {
bytes = value.getBytes(charsetName);
} catch (UnsupportedEncodingException e) {
LOG.warn("Cannot convert the byte to String with the charset " + charsetName, e);
}
}
}
if (bytes == null) {
bytes = value.getBytes();
}
buf.put(bytes);
buf.flip();
return buf;
}
use of java.nio.ByteBuffer in project camel by apache.
the class NIOConverterTest method testToByteArrayBigBuffer.
/**
* Test if returned array size is only to limit of ByteBuffer.
*
* If byteBuffer capacity is bigger that limit, we MUST return data only to the limit.
*/
public void testToByteArrayBigBuffer() {
ByteBuffer bb = ByteBuffer.allocate(100);
bb.put("Hello".getBytes());
bb.flip();
byte[] out = NIOConverter.toByteArray(bb);
assertNotNull(out);
assertEquals(5, out.length);
}
use of java.nio.ByteBuffer in project camel by apache.
the class NIOConverterTest method testToString.
public void testToString() throws Exception {
ByteBuffer bb = ByteBuffer.wrap("Hello".getBytes());
String out = NIOConverter.toString(bb, null);
assertNotNull(out);
assertEquals("Hello", out);
}
use of java.nio.ByteBuffer in project camel by apache.
the class NIOConverterTest method testToByteBufferInteger.
public void testToByteBufferInteger() {
ByteBuffer bb = NIOConverter.toByteBuffer(Integer.valueOf("2"));
assertNotNull(bb);
assertEquals(2, bb.getInt());
}
use of java.nio.ByteBuffer in project camel by apache.
the class XmlConverterTest method testToStreamSourceByByteBuffer.
public void testToStreamSourceByByteBuffer() throws Exception {
Exchange exchange = new DefaultExchange(context);
XmlConverter conv = new XmlConverter();
ByteBuffer bytes = context.getTypeConverter().convertTo(ByteBuffer.class, "<foo>bar</foo>");
StreamSource out = conv.toStreamSource(bytes, exchange);
assertNotNull(out);
assertEquals("<foo>bar</foo>", conv.toString(out, null));
}
Aggregations