use of org.apache.kafka.common.protocol.ByteBufferAccessor in project kafka by apache.
the class RequestUtils method serialize.
public static ByteBuffer serialize(Message header, short headerVersion, Message apiMessage, short apiVersion) {
ObjectSerializationCache cache = new ObjectSerializationCache();
int headerSize = header.size(cache, headerVersion);
int messageSize = apiMessage.size(cache, apiVersion);
ByteBufferAccessor writable = new ByteBufferAccessor(ByteBuffer.allocate(headerSize + messageSize));
header.write(writable, cache, headerVersion);
apiMessage.write(writable, cache, apiVersion);
writable.flip();
return writable.buffer();
}
use of org.apache.kafka.common.protocol.ByteBufferAccessor in project kafka by apache.
the class EnvelopeRequestTest method testToSend.
@Test
public void testToSend() throws IOException {
for (short version : ApiKeys.ENVELOPE.allVersions()) {
ByteBuffer requestData = ByteBuffer.wrap("foobar".getBytes());
RequestHeader header = new RequestHeader(ApiKeys.ENVELOPE, version, "clientId", 15);
EnvelopeRequest request = new EnvelopeRequest.Builder(requestData, "principal".getBytes(), InetAddress.getLocalHost().getAddress()).build(version);
Send send = request.toSend(header);
ByteBuffer buffer = TestUtils.toBuffer(send);
assertEquals(send.size() - 4, buffer.getInt());
assertEquals(header, RequestHeader.parse(buffer));
EnvelopeRequestData parsedRequestData = new EnvelopeRequestData();
parsedRequestData.read(new ByteBufferAccessor(buffer), version);
assertEquals(request.data(), parsedRequestData);
}
}
use of org.apache.kafka.common.protocol.ByteBufferAccessor in project kafka by apache.
the class EnvelopeResponseTest method testToSend.
@Test
public void testToSend() {
for (short version : ApiKeys.ENVELOPE.allVersions()) {
ByteBuffer responseData = ByteBuffer.wrap("foobar".getBytes());
EnvelopeResponse response = new EnvelopeResponse(responseData, Errors.NONE);
short headerVersion = ApiKeys.ENVELOPE.responseHeaderVersion(version);
ResponseHeader header = new ResponseHeader(15, headerVersion);
Send send = response.toSend(header, version);
ByteBuffer buffer = TestUtils.toBuffer(send);
assertEquals(send.size() - 4, buffer.getInt());
assertEquals(header, ResponseHeader.parse(buffer, headerVersion));
EnvelopeResponseData parsedResponseData = new EnvelopeResponseData();
parsedResponseData.read(new ByteBufferAccessor(buffer), version);
assertEquals(response.data(), parsedResponseData);
}
}
use of org.apache.kafka.common.protocol.ByteBufferAccessor in project kafka by apache.
the class RawTaggedFieldWriterTest method testOutOfOrderTags.
@Test
public void testOutOfOrderTags() {
List<RawTaggedField> tags = Arrays.asList(new RawTaggedField(5, new byte[] { 0x4, 0x5, 0x6 }), new RawTaggedField(2, new byte[] { 0x1, 0x2, 0x3 }), new RawTaggedField(7, new byte[] { 0x0 }));
RawTaggedFieldWriter writer = RawTaggedFieldWriter.forFields(tags);
assertEquals(3, writer.numFields());
try {
writer.writeRawTags(new ByteBufferAccessor(ByteBuffer.allocate(1024)), 8);
fail("expected to get RuntimeException");
} catch (RuntimeException e) {
assertEquals("Invalid raw tag field list: tag 2 comes after tag 5, but is " + "not higher than it.", e.getMessage());
}
}
use of org.apache.kafka.common.protocol.ByteBufferAccessor in project kafka by apache.
the class RawTaggedFieldWriterTest method testWritingSeveralRawTaggedFields.
@Test
public void testWritingSeveralRawTaggedFields() {
List<RawTaggedField> tags = Arrays.asList(new RawTaggedField(2, new byte[] { 0x1, 0x2, 0x3 }), new RawTaggedField(5, new byte[] { 0x4, 0x5 }));
RawTaggedFieldWriter writer = RawTaggedFieldWriter.forFields(tags);
assertEquals(2, writer.numFields());
byte[] arr = new byte[9];
ByteBufferAccessor accessor = new ByteBufferAccessor(ByteBuffer.wrap(arr));
writer.writeRawTags(accessor, 1);
assertArrayEquals(new byte[] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }, arr);
writer.writeRawTags(accessor, 3);
assertArrayEquals(new byte[] { 0x2, 0x3, 0x1, 0x2, 0x3, 0x0, 0x0, 0x0, 0x0 }, arr);
writer.writeRawTags(accessor, 7);
assertArrayEquals(new byte[] { 0x2, 0x3, 0x1, 0x2, 0x3, 0x5, 0x2, 0x4, 0x5 }, arr);
writer.writeRawTags(accessor, Integer.MAX_VALUE);
assertArrayEquals(new byte[] { 0x2, 0x3, 0x1, 0x2, 0x3, 0x5, 0x2, 0x4, 0x5 }, arr);
}
Aggregations