Search in sources :

Example 11 with ByteBuffer

use of java.nio.ByteBuffer in project camel by apache.

the class SqsProducer method translateAttributes.

private Map<String, MessageAttributeValue> translateAttributes(Map<String, Object> headers, Exchange exchange) {
    Map<String, MessageAttributeValue> result = new HashMap<String, MessageAttributeValue>();
    HeaderFilterStrategy headerFilterStrategy = getEndpoint().getHeaderFilterStrategy();
    for (Entry<String, Object> entry : headers.entrySet()) {
        // only put the message header which is not filtered into the message attribute
        if (!headerFilterStrategy.applyFilterToCamelHeaders(entry.getKey(), entry.getValue(), exchange)) {
            Object value = entry.getValue();
            if (value instanceof String) {
                MessageAttributeValue mav = new MessageAttributeValue();
                mav.setDataType("String");
                mav.withStringValue((String) value);
                result.put(entry.getKey(), mav);
            } else if (value instanceof ByteBuffer) {
                MessageAttributeValue mav = new MessageAttributeValue();
                mav.setDataType("Binary");
                mav.withBinaryValue((ByteBuffer) value);
                result.put(entry.getKey(), mav);
            } else {
                // cannot translate the message header to message attribute value
                LOG.warn("Cannot put the message header key={}, value={} into Sqs MessageAttribute", entry.getKey(), entry.getValue());
            }
        }
    }
    return result;
}
Also used : HashMap(java.util.HashMap) HeaderFilterStrategy(org.apache.camel.spi.HeaderFilterStrategy) ByteBuffer(java.nio.ByteBuffer) MessageAttributeValue(com.amazonaws.services.sqs.model.MessageAttributeValue)

Example 12 with ByteBuffer

use of java.nio.ByteBuffer in project camel by apache.

the class RecordStringConverter method toString.

@Converter
public static String toString(Record record) {
    Charset charset = Charset.forName("UTF-8");
    ByteBuffer buffer = record.getData();
    if (buffer.hasArray()) {
        byte[] bytes = record.getData().array();
        return new String(bytes, charset);
    } else {
        byte[] bytes = new byte[buffer.remaining()];
        buffer.get(bytes);
        return new String(bytes, charset);
    }
}
Also used : Charset(java.nio.charset.Charset) ByteBuffer(java.nio.ByteBuffer) Converter(org.apache.camel.Converter)

Example 13 with ByteBuffer

use of java.nio.ByteBuffer in project camel by apache.

the class JettyContentExchange9 method send.

public void send(HttpClient client) throws IOException {
    org.eclipse.jetty.client.api.Request.Listener listener = new Request.Listener.Adapter() {

        @Override
        public void onSuccess(Request request) {
            onRequestComplete();
        }

        @Override
        public void onFailure(Request request, Throwable failure) {
            onConnectionFailed(failure);
        }
    };
    InputStreamResponseListener responseListener = new InputStreamResponseListener() {

        OutputStreamBuilder osb = OutputStreamBuilder.withExchange(exchange);

        @Override
        public void onContent(Response response, ByteBuffer content, Callback callback) {
            byte[] buffer = new byte[content.limit()];
            content.get(buffer);
            try {
                osb.write(buffer);
                callback.succeeded();
            } catch (IOException e) {
                callback.failed(e);
            }
        }

        @Override
        public void onComplete(Result result) {
            if (result.isFailed()) {
                doTaskCompleted(result.getFailure());
            } else {
                try {
                    Object content = osb.build();
                    if (content instanceof byte[]) {
                        onResponseComplete(result, (byte[]) content);
                    } else {
                        StreamCache cos = (StreamCache) content;
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        cos.writeTo(baos);
                        onResponseComplete(result, baos.toByteArray());
                    }
                } catch (IOException e) {
                    doTaskCompleted(e);
                }
            }
        }
    };
    request.followRedirects(supportRedirect).listener(listener).send(responseListener);
}
Also used : InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) Request(org.eclipse.jetty.client.api.Request) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) Result(org.eclipse.jetty.client.api.Result) Response(org.eclipse.jetty.client.api.Response) Callback(org.eclipse.jetty.util.Callback) AsyncCallback(org.apache.camel.AsyncCallback) StreamCache(org.apache.camel.StreamCache) OutputStreamBuilder(org.apache.camel.converter.stream.OutputStreamBuilder)

Example 14 with ByteBuffer

use of java.nio.ByteBuffer in project flink by apache.

the class PythonSender method sendRecord.

//=====IO===========================================================================================================
/**
	 * Writes a single record to the memory-mapped file. This method does NOT take care of synchronization. The user
	 * must guarantee that the file may be written to before calling this method. This method essentially reserves the
	 * whole buffer for one record. As such it imposes some performance restrictions and should only be used when
	 * absolutely necessary.
	 *
	 * @param value record to send
	 * @return size of the written buffer
	 * @throws IOException
	 */
@SuppressWarnings("unchecked")
public int sendRecord(Object value) throws IOException {
    fileBuffer.clear();
    int group = 0;
    serializer[group] = getSerializer(value);
    ByteBuffer bb = serializer[group].serialize(value);
    if (bb.remaining() > MAPPED_FILE_SIZE) {
        throw new RuntimeException("Serialized object does not fit into a single buffer.");
    }
    fileBuffer.put(bb);
    int size = fileBuffer.position();
    reset();
    return size;
}
Also used : ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer)

Example 15 with ByteBuffer

use of java.nio.ByteBuffer in project flink by apache.

the class MemorySegmentSimpleTest method testByteBufferWrapping.

@Test
public void testByteBufferWrapping() {
    try {
        MemorySegment seg = MemorySegmentFactory.allocateUnpooledSegment(1024);
        ByteBuffer buf1 = seg.wrap(13, 47);
        assertEquals(13, buf1.position());
        assertEquals(60, buf1.limit());
        assertEquals(47, buf1.remaining());
        ByteBuffer buf2 = seg.wrap(500, 267);
        assertEquals(500, buf2.position());
        assertEquals(767, buf2.limit());
        assertEquals(267, buf2.remaining());
        ByteBuffer buf3 = seg.wrap(0, 1024);
        assertEquals(0, buf3.position());
        assertEquals(1024, buf3.limit());
        assertEquals(1024, buf3.remaining());
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : ByteBuffer(java.nio.ByteBuffer) MemorySegment(org.apache.flink.core.memory.MemorySegment) Test(org.junit.Test)

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