Ejemplo n.º 1
0
        public CharWriteBuffer(Stream stream, Encoding encoding, int initialSize = defaultInitialSize)
        {
            this.stream      = new StreamWriter(stream, encoding);
            this.bufferOwner = BufferArrayPool <char> .Rent(initialSize);

            this.buffer        = bufferOwner;
            this.position      = 0;
            this.streamWritten = 0;
        }
Ejemplo n.º 2
0
        public CharWriteBuffer(int initialSize)
        {
            this.stream      = null;
            this.bufferOwner = BufferArrayPool <char> .Rent(initialSize);

            this.buffer        = bufferOwner;
            this.position      = 0;
            this.streamWritten = 0;
        }
Ejemplo n.º 3
0
        public ByteWriter(int initialSize, Encoding encoding = null)
        {
            this.fromPool    = true;
            this.bufferOwner = BufferArrayPool <byte> .Rent(initialSize);

            this.buffer   = bufferOwner;
            this.encoding = encoding ?? defaultEncoding;
            this.position = 0;
            this.length   = buffer.Length;
        }
Ejemplo n.º 4
0
        public CharReader(Stream stream, Encoding encoding)
        {
            this.stream            = new StreamReader(stream, encoding);
            this.streamBufferOwner = BufferArrayPool <char> .Rent(initialStreamBufferSize);

            this.streamBuffer   = this.streamBufferOwner;
            this.buffer         = this.streamBuffer;
            this.bufferPosition = 0;
            this.bufferLength   = 0;
            this.segmentStart   = -1;
        }
Ejemplo n.º 5
0
        public void Dispose()
        {
            if (fromPool)
            {
                if (bufferOwner != null)
                {
                    buffer.Clear();
                    BufferArrayPool <byte> .Return(bufferOwner);

                    bufferOwner = null;
                    buffer      = null;
                }
            }
        }
Ejemplo n.º 6
0
        public void Dispose()
        {
            if (streamBufferOwner != null)
            {
                streamBuffer.Clear();
                BufferArrayPool <char> .Return(streamBufferOwner);

                streamBufferOwner = null;
                streamBuffer      = null;
            }
            if (stream != null)
            {
                stream.Dispose();
            }
        }
Ejemplo n.º 7
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;
            }
        }
Ejemplo n.º 8
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;
        }
Ejemplo n.º 9
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;
        }