paulirwin commented on code in PR #1170:
URL: https://github.com/apache/lucenenet/pull/1170#discussion_r2301579242


##########
src/Lucene.Net.Replicator/SessionToken.cs:
##########
@@ -112,6 +114,33 @@ public void Serialize(DataOutputStream writer)
             }
         }
 
+        /// <summary>
+        /// Asynchronously serialize the token data for communication between 
server and client.
+        /// </summary>
+        /// <param name="output">The <see cref="Stream"/> to write the token 
data to.</param>
+        /// <param name="cancellationToken">A cancellation token to observe 
while waiting for the flush to complete.</param>
+        /// <returns>A task representing the asynchronous operation.</returns>
+        public async Task SerializeAsync(Stream output, CancellationToken 
cancellationToken = default)
+        {
+            using var writer = new DataOutputStream(output);
+            writer.WriteUTF(Id);

Review Comment:
   To help prime this if we decide to copy the methods, I had Claude Code 
generate a copy of those methods as private static methods that operate on a 
stream parameter instead of a field, and make them async with cancellation 
token support. I have not tested this code, it needs to be validated. 
   
   ```c#
   private static async Task WriteUTFAsync(Stream output, string value, 
CancellationToken cancellationToken = default)
     {
         if (value is null)
             throw new ArgumentNullException(nameof(value));
   
         long utfCount = CountUTFBytes(value);
         if (utfCount > 65535)
         {
             throw new FormatException(SR.Format_InvalidUTFTooLong);
         }
         byte[] buffer = new byte[(int)utfCount + 2];
         int offset = 0;
         offset = WriteInt16ToBuffer((int)utfCount, buffer, offset);
         offset = WriteUTFBytesToBuffer(value, (int)utfCount, buffer, offset);
         await output.WriteAsync(buffer, 0, offset, 
cancellationToken).ConfigureAwait(false);
     }
   
     private static async Task WriteInt32Async(Stream output, int value, 
CancellationToken cancellationToken = default)
     {
         byte[] buff = new byte[4];
         buff[0] = (byte)(value >> 24);
         buff[1] = (byte)(value >> 16);
         buff[2] = (byte)(value >> 8);
         buff[3] = (byte)value;
         await output.WriteAsync(buff, 0, 4, 
cancellationToken).ConfigureAwait(false);
     }
   
     private static async Task WriteInt64Async(Stream output, long value, 
CancellationToken cancellationToken = default)
     {
         byte[] buff = new byte[8];
         buff[0] = (byte)(value >> 56);
         buff[1] = (byte)(value >> 48);
         buff[2] = (byte)(value >> 40);
         buff[3] = (byte)(value >> 32);
         buff[4] = (byte)(value >> 24);
         buff[5] = (byte)(value >> 16);
         buff[6] = (byte)(value >> 8);
         buff[7] = (byte)value;
         await output.WriteAsync(buff, 0, 8, 
cancellationToken).ConfigureAwait(false);
     }
   
     // Helper methods remain synchronous as they don't perform I/O
     private static long CountUTFBytes(string value)
     {
         int utfCount = 0, length = value.Length;
         for (int i = 0; i < length; i++)
         {
             int charValue = value[i];
             if (charValue > 0 && charValue <= 127)
             {
                 utfCount++;
             }
             else if (charValue <= 2047)
             {
                 utfCount += 2;
             }
             else
             {
                 utfCount += 3;
             }
         }
         return utfCount;
     }
   
     private static int WriteInt16ToBuffer(int value, byte[] buffer, int offset)
     {
         buffer[offset++] = (byte)(value >> 8);
         buffer[offset++] = (byte)value;
         return offset;
     }
   
     private static int WriteUTFBytesToBuffer(string value, long count, byte[] 
buffer, int offset)
     {
         int length = value.Length;
         for (int i = 0; i < length; i++)
         {
             int charValue = value[i];
             if (charValue > 0 && charValue <= 127)
             {
                 buffer[offset++] = (byte)charValue;
             }
             else if (charValue <= 2047)
             {
                 buffer[offset++] = (byte)(0xc0 | (0x1f & (charValue >> 6)));
                 buffer[offset++] = (byte)(0x80 | (0x3f & charValue));
             }
             else
             {
                 buffer[offset++] = (byte)(0xe0 | (0x0f & (charValue >> 12)));
                 buffer[offset++] = (byte)(0x80 | (0x3f & (charValue >> 6)));
                 buffer[offset++] = (byte)(0x80 | (0x3f & charValue));
             }
         }
         return offset;
     }
     ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@lucenenet.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to