use of org.apache.mina.core.buffer.IoBuffer in project camel by apache.
the class Mina2UdpProtocolCodecFactory method getEncoder.
public ProtocolEncoder getEncoder(IoSession session) throws Exception {
return new ProtocolEncoder() {
public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {
IoBuffer buf = toIoBuffer(message);
buf.flip();
out.write(buf);
}
public void dispose(IoSession session) throws Exception {
// do nothing
}
};
}
use of org.apache.mina.core.buffer.IoBuffer in project camel by apache.
the class Mina2Converter method toIoBuffer.
@Converter
public static IoBuffer toIoBuffer(byte[] bytes) {
IoBuffer buf = IoBuffer.allocate(bytes.length);
buf.put(bytes);
return buf;
}
use of org.apache.mina.core.buffer.IoBuffer in project camel by apache.
the class Mina2ConverterTest method testToStringTwoTimes.
public void testToStringTwoTimes() throws UnsupportedEncodingException {
String in = "Hello World 你好";
IoBuffer bb = IoBuffer.wrap(in.getBytes("UTF-8"));
Exchange exchange = new DefaultExchange(new DefaultCamelContext());
exchange.setProperty(Exchange.CHARSET_NAME, "UTF-8");
String out = Mina2Converter.toString(bb, exchange);
assertEquals("Hello World 你好", out);
// should NOT be possible to convert to string without affecting the ByteBuffer
out = Mina2Converter.toString(bb, exchange);
assertEquals("", out);
}
use of org.apache.mina.core.buffer.IoBuffer in project camel by apache.
the class Mina2ConverterTest method testToInputStream.
public void testToInputStream() throws Exception {
byte[] in = "Hello World".getBytes();
IoBuffer bb = IoBuffer.wrap(in);
InputStream is = Mina2Converter.toInputStream(bb);
for (byte b : in) {
int out = is.read();
assertEquals(b, out);
}
}
use of org.apache.mina.core.buffer.IoBuffer in project camel by apache.
the class Mina2ConverterTest method testToByteBuffer.
public void testToByteBuffer() {
byte[] in = "Hello World".getBytes();
IoBuffer bb = Mina2Converter.toIoBuffer(in);
assertNotNull(bb);
// convert back to byte[] and see if the bytes are equal
// must flip to change direction to read
bb.flip();
byte[] out = Mina2Converter.toByteArray(bb);
for (int i = 0; i < out.length; i++) {
assertEquals(in[i], out[i]);
}
}
Aggregations