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;
}
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);
}
}
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);
}
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;
}
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());
}
}
Aggregations