Search in sources :

Example 1 with ByteBufferAccessor

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();
}
Also used : ObjectSerializationCache(org.apache.kafka.common.protocol.ObjectSerializationCache) ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor)

Example 2 with ByteBufferAccessor

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);
    }
}
Also used : ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) ByteBuffer(java.nio.ByteBuffer) EnvelopeRequestData(org.apache.kafka.common.message.EnvelopeRequestData) Send(org.apache.kafka.common.network.Send) Test(org.junit.jupiter.api.Test)

Example 3 with ByteBufferAccessor

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);
    }
}
Also used : EnvelopeResponseData(org.apache.kafka.common.message.EnvelopeResponseData) ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) ByteBuffer(java.nio.ByteBuffer) Send(org.apache.kafka.common.network.Send) Test(org.junit.jupiter.api.Test)

Example 4 with ByteBufferAccessor

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());
    }
}
Also used : ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) Test(org.junit.jupiter.api.Test)

Example 5 with ByteBufferAccessor

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);
}
Also used : ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) Test(org.junit.jupiter.api.Test)

Aggregations

ByteBufferAccessor (org.apache.kafka.common.protocol.ByteBufferAccessor)39 ByteBuffer (java.nio.ByteBuffer)24 Test (org.junit.jupiter.api.Test)23 ObjectSerializationCache (org.apache.kafka.common.protocol.ObjectSerializationCache)13 TopicPartition (org.apache.kafka.common.TopicPartition)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)4 Uuid (org.apache.kafka.common.Uuid)4 UnsupportedVersionException (org.apache.kafka.common.errors.UnsupportedVersionException)4 Collections (java.util.Collections)3 List (java.util.List)3 Map (java.util.Map)3 LeaderChangeMessage (org.apache.kafka.common.message.LeaderChangeMessage)3 UpdateMetadataEndpoint (org.apache.kafka.common.message.UpdateMetadataRequestData.UpdateMetadataEndpoint)3 Send (org.apache.kafka.common.network.Send)3 BufferUnderflowException (java.nio.BufferUnderflowException)2 Arrays.asList (java.util.Arrays.asList)2 Collections.emptyList (java.util.Collections.emptyList)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2