Skip to content

Add ArraySegment<byte> as native supported type #2219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 38 additions & 10 deletions src/Confluent.Kafka/Producer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,11 @@ private void InitializeSerializers(
}

// setup value serializer.
if (valueSerializer == null && asyncValueSerializer == null)
if (typeof(TValue) == typeof(ArraySegment<byte>))
{
// No serializer needed for native buffers
}
else if (valueSerializer == null && asyncValueSerializer == null)
{
if (!defaultSerializers.TryGetValue(typeof(TValue), out object serializer))
{
Expand Down Expand Up @@ -770,11 +774,23 @@ public async Task<DeliveryResult<TKey, TValue>> ProduceAsync(
}

byte[] valBytes;
int valOffset = 0, valLength = 0;
try
{
valBytes = (valueSerializer != null)
? valueSerializer.Serialize(message.Value, new SerializationContext(MessageComponentType.Value, topicPartition.Topic, headers))
: await asyncValueSerializer.SerializeAsync(message.Value, new SerializationContext(MessageComponentType.Value, topicPartition.Topic, headers)).ConfigureAwait(false);
if (message.Value is ArraySegment<byte> arraySegment)
{
valBytes = arraySegment.Array;
valOffset = arraySegment.Offset;
valLength = arraySegment.Count;
}
else
{
valBytes = (valueSerializer != null)
? valueSerializer.Serialize(message.Value, new SerializationContext(MessageComponentType.Value, topicPartition.Topic, headers))
: await asyncValueSerializer.SerializeAsync(message.Value, new SerializationContext(MessageComponentType.Value, topicPartition.Topic, headers)).ConfigureAwait(false);
valOffset = 0;
valLength = valBytes == null ? 0 : valBytes.Length;
}
}
catch (Exception ex)
{
Expand Down Expand Up @@ -805,7 +821,7 @@ public async Task<DeliveryResult<TKey, TValue>> ProduceAsync(

ProduceImpl(
topicPartition.Topic,
valBytes, 0, valBytes == null ? 0 : valBytes.Length,
valBytes, valOffset, valLength,
keyBytes, 0, keyBytes == null ? 0 : keyBytes.Length,
message.Timestamp, topicPartition.Partition, headers.BackingList,
handler);
Expand All @@ -816,7 +832,7 @@ public async Task<DeliveryResult<TKey, TValue>> ProduceAsync(
{
ProduceImpl(
topicPartition.Topic,
valBytes, 0, valBytes == null ? 0 : valBytes.Length,
valBytes, valOffset, valLength,
keyBytes, 0, keyBytes == null ? 0 : keyBytes.Length,
message.Timestamp, topicPartition.Partition, headers.BackingList,
null);
Expand Down Expand Up @@ -893,11 +909,23 @@ public void Produce(
}

byte[] valBytes;
int valOffset = 0, valLength = 0;
try
{
valBytes = (valueSerializer != null)
? valueSerializer.Serialize(message.Value, new SerializationContext(MessageComponentType.Value, topicPartition.Topic, headers))
: throw new InvalidOperationException("Produce called with an IAsyncSerializer value serializer configured but an ISerializer is required.");
if (message.Value is ArraySegment<byte> arraySegment)
{
valBytes = arraySegment.Array;
valOffset = arraySegment.Offset;
valLength = arraySegment.Count;
}
else
{
valBytes = (valueSerializer != null)
? valueSerializer.Serialize(message.Value, new SerializationContext(MessageComponentType.Value, topicPartition.Topic, headers))
: throw new InvalidOperationException("Produce called with an IAsyncSerializer value serializer configured but an ISerializer is required.");
valOffset = 0;
valLength = valBytes == null ? 0 : valBytes.Length;
}
}
catch (Exception ex)
{
Expand All @@ -915,7 +943,7 @@ public void Produce(
{
ProduceImpl(
topicPartition.Topic,
valBytes, 0, valBytes == null ? 0 : valBytes.Length,
valBytes, valOffset, valLength,
keyBytes, 0, keyBytes == null ? 0 : keyBytes.Length,
message.Timestamp, topicPartition.Partition,
headers.BackingList,
Expand Down