Search in sources :

Example 6 with ByteBuffer

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;
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteBuffer(java.nio.ByteBuffer) Converter(org.apache.camel.Converter)

Example 7 with ByteBuffer

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);
}
Also used : ByteBuffer(java.nio.ByteBuffer)

Example 8 with ByteBuffer

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);
}
Also used : ByteBuffer(java.nio.ByteBuffer)

Example 9 with ByteBuffer

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());
}
Also used : ByteBuffer(java.nio.ByteBuffer)

Example 10 with ByteBuffer

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));
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) StreamSource(javax.xml.transform.stream.StreamSource) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ByteBuffer (java.nio.ByteBuffer)8537 Test (org.junit.Test)1905 IOException (java.io.IOException)972 ArrayList (java.util.ArrayList)430 File (java.io.File)216 FileChannel (java.nio.channels.FileChannel)204 MappedByteBuffer (java.nio.MappedByteBuffer)190 HashMap (java.util.HashMap)172 CharBuffer (java.nio.CharBuffer)144 ByteArrayOutputStream (java.io.ByteArrayOutputStream)138 InetSocketAddress (java.net.InetSocketAddress)137 Random (java.util.Random)119 InputStream (java.io.InputStream)115 FileInputStream (java.io.FileInputStream)114 Map (java.util.Map)113 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)99 Test (org.testng.annotations.Test)96 IntBuffer (java.nio.IntBuffer)93 SocketChannel (java.nio.channels.SocketChannel)92 FileOutputStream (java.io.FileOutputStream)87