NehanPathan commented on code in PR #1170: URL: https://github.com/apache/lucenenet/pull/1170#discussion_r2308904983
########## src/Lucene.Net/Support/IO/StreamExtensions.cs: ########## @@ -210,5 +213,154 @@ public static long ReadInt64(this Stream stream) buff[6] << 16 | buff[7] << 24); return (long)((ulong)hi) << 32 | lo; } + + //async versions of the above methods + public static async Task WriteInt32Async(this Stream output, int value, CancellationToken cancellationToken = default) + { + if (output is null) + throw new ArgumentNullException(nameof(output)); + + 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, buff.Length, cancellationToken).ConfigureAwait(false); + } + + public static async Task WriteInt64Async(this Stream output, long value, CancellationToken cancellationToken = default) + { + if (output is null) + throw new ArgumentNullException(nameof(output)); + + 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, buff.Length, cancellationToken).ConfigureAwait(false); + } + + public static async Task WriteUTFAsync(this Stream output, string value, CancellationToken cancellationToken = default) + { + if (output is null) + throw new ArgumentNullException(nameof(output)); + if (value is null) + throw new ArgumentNullException(nameof(value)); + + long utfCount = CountUTFBytes(value); + if (utfCount > ushort.MaxValue) + throw new EncoderFallbackException("Encoded string too long."); + + 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); + } + + public static async Task<int> ReadInt32Async(this Stream input, CancellationToken cancellationToken = default) + { + if (input is null) + throw new ArgumentNullException(nameof(input)); + + byte[] buff = new byte[4]; + int read = await input.ReadAsync(buff, 0, buff.Length, cancellationToken).ConfigureAwait(false); + if (read < buff.Length) throw new EndOfStreamException(); + + return (buff[0] << 24) | (buff[1] << 16) | (buff[2] << 8) | buff[3]; + } + + public static async Task<long> ReadInt64Async(this Stream input, CancellationToken cancellationToken = default) + { + if (input is null) + throw new ArgumentNullException(nameof(input)); + + byte[] buff = new byte[8]; + int read = await input.ReadAsync(buff, 0, buff.Length, cancellationToken).ConfigureAwait(false); + if (read < buff.Length) throw new EndOfStreamException(); + + return ((long)buff[0] << 56) | + ((long)buff[1] << 48) | + ((long)buff[2] << 40) | + ((long)buff[3] << 32) | + ((long)buff[4] << 24) | + ((long)buff[5] << 16) | + ((long)buff[6] << 8) | + buff[7]; + } + + public static async Task<string> ReadUTFAsync(this Stream input, CancellationToken cancellationToken = default) + { + if (input is null) + throw new ArgumentNullException(nameof(input)); + + byte[] lenBuff = new byte[2]; + int readLen = await input.ReadAsync(lenBuff, 0, lenBuff.Length, cancellationToken).ConfigureAwait(false); + if (readLen < lenBuff.Length) throw new EndOfStreamException(); + + int length = (lenBuff[0] << 8) | lenBuff[1]; + byte[] buffer = new byte[length]; Review Comment: @NightOwl888 @paulirwin All suggested changes have been applied — method renames, ArrayPool usage for buffers, and cleaned up comments. Please let me know if anything looks off or feels odd. Happy to adjust further if needed. -- 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