@@ -279,8 +279,8 @@ private void DeliveryReportCallbackImpl(IntPtr rk, IntPtr rkmessage, IntPtr opaq
279
279
280
280
private void ProduceImpl (
281
281
string topic ,
282
- byte [ ] val , int valOffset , int valLength ,
283
- byte [ ] key , int keyOffset , int keyLength ,
282
+ ReadOnlyMemory < byte > val ,
283
+ ReadOnlyMemory < byte > key ,
284
284
Timestamp timestamp ,
285
285
Partition partition ,
286
286
IReadOnlyList < IHeader > headers ,
@@ -308,8 +308,8 @@ private void ProduceImpl(
308
308
309
309
err = KafkaHandle . Produce (
310
310
topic ,
311
- val , valOffset , valLength ,
312
- key , keyOffset , keyLength ,
311
+ val ,
312
+ key ,
313
313
partition . Value ,
314
314
timestamp . UnixTimestampMs ,
315
315
headers ,
@@ -325,8 +325,8 @@ private void ProduceImpl(
325
325
{
326
326
err = KafkaHandle . Produce (
327
327
topic ,
328
- val , valOffset , valLength ,
329
- key , keyOffset , keyLength ,
328
+ val ,
329
+ key ,
330
330
partition . Value ,
331
331
timestamp . UnixTimestampMs ,
332
332
headers ,
@@ -506,7 +506,14 @@ private void InitializeSerializers(
506
506
IAsyncSerializer < TValue > asyncValueSerializer )
507
507
{
508
508
// setup key serializer.
509
- if ( keySerializer == null && asyncKeySerializer == null )
509
+ if ( typeof ( TKey ) == typeof ( Memory < byte > ) || typeof ( TKey ) == typeof ( ReadOnlyMemory < byte > ) )
510
+ {
511
+ if ( keySerializer != null || asyncKeySerializer != null )
512
+ {
513
+ throw new ArgumentNullException ( null , "Key serializer should not be specified for Memory<byte>" ) ;
514
+ }
515
+ }
516
+ else if ( keySerializer == null && asyncKeySerializer == null )
510
517
{
511
518
if ( ! defaultSerializers . TryGetValue ( typeof ( TKey ) , out object serializer ) )
512
519
{
@@ -529,7 +536,14 @@ private void InitializeSerializers(
529
536
}
530
537
531
538
// setup value serializer.
532
- if ( valueSerializer == null && asyncValueSerializer == null )
539
+ if ( typeof ( TValue ) == typeof ( Memory < byte > ) || typeof ( TValue ) == typeof ( ReadOnlyMemory < byte > ) )
540
+ {
541
+ if ( valueSerializer != null || asyncValueSerializer != null )
542
+ {
543
+ throw new ArgumentNullException ( null , "Value serializer should not be specified for Memory<byte>" ) ;
544
+ }
545
+ }
546
+ else if ( valueSerializer == null && asyncValueSerializer == null )
533
547
{
534
548
if ( ! defaultSerializers . TryGetValue ( typeof ( TValue ) , out object serializer ) )
535
549
{
@@ -750,12 +764,23 @@ public async Task<DeliveryResult<TKey, TValue>> ProduceAsync(
750
764
{
751
765
Headers headers = message . Headers ?? new Headers ( ) ;
752
766
753
- byte [ ] keyBytes ;
767
+ ReadOnlyMemory < byte > keyBytes ;
754
768
try
755
769
{
756
- keyBytes = ( keySerializer != null )
757
- ? keySerializer . Serialize ( message . Key , new SerializationContext ( MessageComponentType . Key , topicPartition . Topic , headers ) )
758
- : await asyncKeySerializer . SerializeAsync ( message . Key , new SerializationContext ( MessageComponentType . Key , topicPartition . Topic , headers ) ) . ConfigureAwait ( false ) ;
770
+ if ( message . Key is Memory < byte > memory )
771
+ {
772
+ keyBytes = memory ;
773
+ }
774
+ else if ( message . Key is ReadOnlyMemory < byte > readOnlyMemory )
775
+ {
776
+ keyBytes = readOnlyMemory ;
777
+ }
778
+ else
779
+ {
780
+ keyBytes = ( keySerializer != null )
781
+ ? keySerializer . Serialize ( message . Key , new SerializationContext ( MessageComponentType . Key , topicPartition . Topic , headers ) )
782
+ : await asyncKeySerializer . SerializeAsync ( message . Key , new SerializationContext ( MessageComponentType . Key , topicPartition . Topic , headers ) ) . ConfigureAwait ( false ) ;
783
+ }
759
784
}
760
785
catch ( Exception ex )
761
786
{
@@ -769,12 +794,23 @@ public async Task<DeliveryResult<TKey, TValue>> ProduceAsync(
769
794
ex ) ;
770
795
}
771
796
772
- byte [ ] valBytes ;
797
+ ReadOnlyMemory < byte > valBytes ;
773
798
try
774
799
{
775
- valBytes = ( valueSerializer != null )
776
- ? valueSerializer . Serialize ( message . Value , new SerializationContext ( MessageComponentType . Value , topicPartition . Topic , headers ) )
777
- : await asyncValueSerializer . SerializeAsync ( message . Value , new SerializationContext ( MessageComponentType . Value , topicPartition . Topic , headers ) ) . ConfigureAwait ( false ) ;
800
+ if ( message . Value is Memory < byte > memory )
801
+ {
802
+ valBytes = memory ;
803
+ }
804
+ else if ( message . Value is ReadOnlyMemory < byte > readOnlyMemory )
805
+ {
806
+ valBytes = readOnlyMemory ;
807
+ }
808
+ else
809
+ {
810
+ valBytes = ( valueSerializer != null )
811
+ ? valueSerializer . Serialize ( message . Value , new SerializationContext ( MessageComponentType . Value , topicPartition . Topic , headers ) )
812
+ : await asyncValueSerializer . SerializeAsync ( message . Value , new SerializationContext ( MessageComponentType . Value , topicPartition . Topic , headers ) ) . ConfigureAwait ( false ) ;
813
+ }
778
814
}
779
815
catch ( Exception ex )
780
816
{
@@ -805,8 +841,8 @@ public async Task<DeliveryResult<TKey, TValue>> ProduceAsync(
805
841
806
842
ProduceImpl (
807
843
topicPartition . Topic ,
808
- valBytes , 0 , valBytes == null ? 0 : valBytes . Length ,
809
- keyBytes , 0 , keyBytes == null ? 0 : keyBytes . Length ,
844
+ valBytes ,
845
+ keyBytes ,
810
846
message . Timestamp , topicPartition . Partition , headers . BackingList ,
811
847
handler ) ;
812
848
@@ -816,8 +852,8 @@ public async Task<DeliveryResult<TKey, TValue>> ProduceAsync(
816
852
{
817
853
ProduceImpl (
818
854
topicPartition . Topic ,
819
- valBytes , 0 , valBytes == null ? 0 : valBytes . Length ,
820
- keyBytes , 0 , keyBytes == null ? 0 : keyBytes . Length ,
855
+ valBytes ,
856
+ keyBytes ,
821
857
message . Timestamp , topicPartition . Partition , headers . BackingList ,
822
858
null ) ;
823
859
@@ -873,12 +909,23 @@ public void Produce(
873
909
874
910
Headers headers = message . Headers ?? new Headers ( ) ;
875
911
876
- byte [ ] keyBytes ;
912
+ ReadOnlyMemory < byte > keyBytes ;
877
913
try
878
914
{
879
- keyBytes = ( keySerializer != null )
880
- ? keySerializer . Serialize ( message . Key , new SerializationContext ( MessageComponentType . Key , topicPartition . Topic , headers ) )
881
- : throw new InvalidOperationException ( "Produce called with an IAsyncSerializer key serializer configured but an ISerializer is required." ) ;
915
+ if ( message . Key is Memory < byte > memory )
916
+ {
917
+ keyBytes = memory ;
918
+ }
919
+ else if ( message . Key is ReadOnlyMemory < byte > readOnlyMemory )
920
+ {
921
+ keyBytes = readOnlyMemory ;
922
+ }
923
+ else
924
+ {
925
+ keyBytes = ( keySerializer != null )
926
+ ? keySerializer . Serialize ( message . Key , new SerializationContext ( MessageComponentType . Key , topicPartition . Topic , headers ) )
927
+ : throw new InvalidOperationException ( "Produce called with an IAsyncSerializer key serializer configured but an ISerializer is required." ) ;
928
+ }
882
929
}
883
930
catch ( Exception ex )
884
931
{
@@ -892,12 +939,23 @@ public void Produce(
892
939
ex ) ;
893
940
}
894
941
895
- byte [ ] valBytes ;
942
+ ReadOnlyMemory < byte > valBytes ;
896
943
try
897
944
{
898
- valBytes = ( valueSerializer != null )
899
- ? valueSerializer . Serialize ( message . Value , new SerializationContext ( MessageComponentType . Value , topicPartition . Topic , headers ) )
900
- : throw new InvalidOperationException ( "Produce called with an IAsyncSerializer value serializer configured but an ISerializer is required." ) ;
945
+ if ( message . Value is Memory < byte > memory )
946
+ {
947
+ valBytes = memory ;
948
+ }
949
+ else if ( message . Value is ReadOnlyMemory < byte > readOnlyMemory )
950
+ {
951
+ valBytes = readOnlyMemory ;
952
+ }
953
+ else
954
+ {
955
+ valBytes = ( valueSerializer != null )
956
+ ? valueSerializer . Serialize ( message . Value , new SerializationContext ( MessageComponentType . Value , topicPartition . Topic , headers ) )
957
+ : throw new InvalidOperationException ( "Produce called with an IAsyncSerializer value serializer configured but an ISerializer is required." ) ;
958
+ }
901
959
}
902
960
catch ( Exception ex )
903
961
{
@@ -915,8 +973,8 @@ public void Produce(
915
973
{
916
974
ProduceImpl (
917
975
topicPartition . Topic ,
918
- valBytes , 0 , valBytes == null ? 0 : valBytes . Length ,
919
- keyBytes , 0 , keyBytes == null ? 0 : keyBytes . Length ,
976
+ valBytes ,
977
+ keyBytes ,
920
978
message . Timestamp , topicPartition . Partition ,
921
979
headers . BackingList ,
922
980
deliveryHandler == null
0 commit comments