Search in sources :

Example 1 with ByteBuffer

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;
}
Also used : ByteBuffer(org.apache.mina.common.ByteBuffer) Converter(org.apache.camel.Converter)

Example 2 with ByteBuffer

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);
}
Also used : ByteBuffer(org.apache.mina.common.ByteBuffer)

Example 3 with ByteBuffer

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]);
    }
}
Also used : ByteBuffer(org.apache.mina.common.ByteBuffer)

Example 4 with ByteBuffer

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);
    }
}
Also used : InputStream(java.io.InputStream) ByteBuffer(org.apache.mina.common.ByteBuffer)

Example 5 with ByteBuffer

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);
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) ByteBuffer(org.apache.mina.common.ByteBuffer) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext)

Aggregations

ByteBuffer (org.apache.mina.common.ByteBuffer)8 Exchange (org.apache.camel.Exchange)2 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)2 DefaultExchange (org.apache.camel.impl.DefaultExchange)2 InputStream (java.io.InputStream)1 Converter (org.apache.camel.Converter)1 IoSession (org.apache.mina.common.IoSession)1 ProtocolEncoder (org.apache.mina.filter.codec.ProtocolEncoder)1 ProtocolEncoderOutput (org.apache.mina.filter.codec.ProtocolEncoderOutput)1