Beispiel #1
0
        private unsafe void ReadBuffer(int sizeNeeded)
        {
            if (stream == null)
            {
                return;
            }

            if (bufferPosition > 1)
            {
                //Always keep one char for BackOne or hold chars for segmentStart
                int shift;
                if (segmentStart > -1)
                {
                    shift = segmentStart;
                }
                else
                {
                    shift = bufferPosition - 1;
                }
                streamBuffer.Slice(shift, bufferLength - shift).CopyTo(streamBuffer);
                segmentStart   -= shift;
                bufferLength   -= shift;
                bufferPosition -= shift;
            }

            if (streamBuffer.Length - bufferPosition < sizeNeeded)
            {
                BufferArrayPool <char> .Grow(ref streamBufferOwner, bufferLength + sizeNeeded);

                streamBuffer = streamBufferOwner;
                buffer       = streamBuffer;
            }

            while (bufferLength < streamBuffer.Length)
            {
#if NETSTANDARD2_0
                var read = stream.ReadToSpan(streamBuffer.Slice(bufferLength));
#else
                var read = stream.Read(streamBuffer.Slice(bufferLength));
#endif
                if (read == 0)
                {
                    break;
                }
                bufferLength += read;
            }
        }
Beispiel #2
0
        private void EnsureBufferSize(int additionalSize)
        {
            if (position + additionalSize <= buffer.Length)
            {
                return;
            }

            if (!fromPool)
            {
                throw new InvalidOperationException($"{nameof(ByteWriter)} has reached it's buffer limit");
            }

            var minSize = position + additionalSize;

            BufferArrayPool <byte> .Grow(ref bufferOwner, minSize);

            buffer = bufferOwner;
            length = buffer.Length;
        }
Beispiel #3
0
        private void EnsureBufferSize(int additionalSize)
        {
            if (position + additionalSize <= buffer.Length)
            {
                return;
            }

            if (bufferOwner == null)
            {
                bufferOwner = BufferArrayPool <char> .Rent(defaultInitialSize);

                buffer = bufferOwner;
                if (position + additionalSize < buffer.Length)
                {
                    return;
                }
            }

            if (position > 0 && stream != null)
            {
#if NETSTANDARD2_0
                stream.WriteToSpan(buffer.Slice(0, position));
#else
                stream.Write(buffer.Slice(0, position));
#endif
                streamWritten += position;
                position       = 0;

                if (position + additionalSize < buffer.Length)
                {
                    return;
                }
            }

            var minSize = position + additionalSize;
            BufferArrayPool <char> .Grow(ref bufferOwner, minSize);

            buffer = bufferOwner;
        }