Пример #1
0
 internal FixedSizeSet(int size, IEqualityComparer <T>?comparer = null)
 {
     buffer        = new ArrayRental <Node>(size * size, true);
     this.comparer = comparer ?? EqualityComparer <T> .Default;
     this.size     = size;
     count         = 0;
 }
Пример #2
0
            protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context)
            {
                const int       maxChars        = 128; //it is empiric value measured using Console.WriteLine(builder.Length)
                EncodingContext encodingContext = DefaultHttpEncoding;

                using (var encodingBuffer = new ArrayRental <byte>(DefaultHttpEncoding.GetMaxByteCount(maxChars)))
                {
                    //write start boundary
                    await stream.WriteStringAsync(DoubleDash + boundary + CrLf, encodingContext, (byte[])encodingBuffer).ConfigureAwait(false);

                    encodingContext.Reset();
                    var builder = new StringBuilder(maxChars);
                    //write each nested content
                    var writeDivider = false;
                    foreach (var entry in entries)
                    {
                        await EncodeHeadersToStreamAsync(stream, builder, entry, writeDivider, boundary, encodingContext, (byte[])encodingBuffer).ConfigureAwait(false);

                        encodingContext.Reset();
                        Debug.Assert(builder.Length <= maxChars);
                        writeDivider = true;
                        await entry.CopyToAsync(stream).ConfigureAwait(false);
                    }
                    //write footer
                    await stream.WriteStringAsync(CrLf + DoubleDash + boundary + DoubleDash + CrLf, encodingContext, (byte[])encodingBuffer).ConfigureAwait(false);
                }
                encodingContext.Reset();
            }
Пример #3
0
        async Task IDataTransferObject.CopyToAsync(Stream output, CancellationToken token)
        {
            //TODO: Should be rewritten for .NET Standard 2.1
            const int defaultBufferSize = 128;

            using (var buffer = new ArrayRental <byte>(defaultBufferSize))
                await output.WriteStringAsync(Content, Type.GetEncoding(), (byte[])buffer, token).ConfigureAwait(false);
        }
Пример #4
0
 async Task IMessage.CopyToAsync(Stream output)
 {
     //TODO: Should be rewritten for .NET Standard 2.1
     foreach (var segment in Content)
     {
         using (var array = new ArrayRental <byte>(segment.Length))
         {
             segment.CopyTo(array.Memory);
             await output.WriteAsync(array, 0, segment.Length).ConfigureAwait(false);
         }
     }
 }
Пример #5
0
        private static Rental <Type[]> GetResolvedTypeArray(ImmutableArray <TypeRef> typeRefs)
        {
            if (typeRefs.IsDefault)
            {
                return(default(Rental <Type[]>));
            }

            var result = ArrayRental <Type> .Get(typeRefs.Length);

            for (int i = 0; i < typeRefs.Length; i++)
            {
                result.Value[i] = typeRefs[i].ResolvedType;
            }

            return(result);
        }
Пример #6
0
        /// <summary>
        /// Reads the string asynchronously using the specified encoding.
        /// </summary>
        /// <param name="stream">The stream to read from.</param>
        /// <param name="length">The length of the string.</param>
        /// <param name="context">The text decoding context.</param>
        /// <param name="buffer">The buffer that is allocated by the caller.</param>
        /// <param name="token">The token that can be used to cancel asynchronous operation.</param>
        /// <returns>The string decoded from the log entry content stream.</returns>
        public static async Task <string> ReadStringAsync(this Stream stream, int length, DecodingContext context, byte[] buffer, CancellationToken token = default)
        {
            //TODO: Should be rewritten for .NET Standard 2.1
            var maxChars = context.Encoding.GetMaxCharCount(buffer.Length);

            if (maxChars == 0)
            {
                throw new ArgumentException(ExceptionMessages.BufferTooSmall, nameof(buffer));
            }
            var decoder = context.GetDecoder();
            var charBuffer = new ArrayRental <char>(maxChars);
            var result = default(ArrayRental <char>);
            int currentPos = 0, resultOffset = 0;

            try
            {
                do
                {
                    var readLength = Math.Min(length - currentPos, buffer.Length);
                    var n          = await stream.ReadAsync(buffer, 0, readLength, token).ConfigureAwait(false);

                    if (n == 0)
                    {
                        throw new EndOfStreamException();
                    }
                    var charsRead = decoder.GetChars(buffer, 0, n, (char[])charBuffer, 0);
                    if (currentPos == 0 && n == length)
                    {
                        return(new string((char[])charBuffer, 0, charsRead));
                    }
                    if (result.IsEmpty)
                    {
                        result = new ArrayRental <char>(length);
                    }
                    Memory.Copy(ref charBuffer[0], ref result[resultOffset], (uint)charsRead);
                    resultOffset += charsRead;
                    currentPos   += n;
                }while (currentPos < length);
                return(new string((char[])result, 0, resultOffset));
            }
            finally
            {
                charBuffer.Dispose();
                result.Dispose();
            }
        }