Beispiel #1
0
        public BinaryDecoderWriter(BinaryCodec codec, Stream outputStream, int?bufferSize = null, bool closeStream = true)
            : base()
        {
            if (codec == null)
            {
                throw new ArgumentNullException(nameof(codec));
            }

            if (outputStream == null)
            {
                throw new ArgumentNullException(nameof(outputStream));
            }

            _codec        = codec;
            _decoder      = codec.GetDecoder();
            _outputStream = outputStream;
            _closeStream  = closeStream;

            _inputBuffer       = new char[codec.MinimumInputBuffer];
            _inputBufferOffset = 0;
            _inputBufferLength = _inputBuffer.Length;

            int outputBufferSize = bufferSize ?? DefaultBufferSize;

            if (outputBufferSize < codec.MinimumOutputBuffer)
            {
                outputBufferSize = codec.MinimumOutputBuffer;
            }

            _outputBuffer       = new byte[outputBufferSize];
            _outputBufferOffset = 0;
            _outputBufferLength = _outputBuffer.Length;
        }
Beispiel #2
0
        public BinaryDecoderStream(BinaryCodec codec, TextReader inputReader, int?bufferLength = null, bool closeReader = true)
        {
            if (codec == null)
            {
                throw new ArgumentNullException(nameof(codec));
            }

            if (inputReader == null)
            {
                throw new ArgumentNullException(nameof(inputReader));
            }

            int inputBufferLength = bufferLength ?? DefaultInputBufferLength;

            if (inputBufferLength <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(bufferLength));
            }

            inputBufferLength = Math.Max(inputBufferLength, codec.MinimumInputBuffer);

            // all of the codecs currently have a minimumOutputBuffer of 1, this code relies
            // on that knowledge.. so let's check that it's true..
            System.Diagnostics.Debug.Assert(codec.MinimumOutputBuffer == 1);

            _convertStatus = ConvertStatus.InputRequired;
            _decoder       = codec.GetDecoder();
            _inputReader   = inputReader;
            _inputBuffer   = new char[inputBufferLength];
            _closeReader   = closeReader;
        }
Beispiel #3
0
        private static void Decode(BinaryCodec encoding, TextReader inputReader, Stream outputStream)
        {
            char[] inputBuffer  = new char[encoding.MinimumInputBuffer];
            byte[] outputBuffer = new byte[encoding.MinimumOutputBuffer];

            bool readEof         = false;
            int  inputBufferEnd  = 0;
            int  outputBufferEnd = 0;
            int  inputBufferUsed;
            int  outputBufferUsed;

            BinaryDecoder decoder = encoding.GetDecoder();

            while (true)
            {
                if ((inputBufferEnd < inputBuffer.Length) && (!readEof))
                {
                    int charsRead = inputReader.Read(inputBuffer, inputBufferEnd, inputBuffer.Length - inputBufferEnd);
                    if (charsRead == 0)
                    {
                        readEof = true;
                    }

                    inputBufferEnd += charsRead;
                }

                // stop when we've read EOF and Convert returns true..
                bool finished = ((decoder.Convert(inputBuffer, 0, inputBufferEnd,
                                                  outputBuffer, 0, outputBuffer.Length,
                                                  readEof,
                                                  out inputBufferUsed,
                                                  out outputBufferUsed)) && (readEof));

                // dump any output produced to outputWriter..
                outputStream.Write(outputBuffer, 0, outputBufferUsed);

                if (finished)
                {
                    break;
                }

                // shift input as needed..
                if (inputBufferUsed != 0)
                {
                    if (inputBufferUsed < inputBufferEnd)
                    {
                        Buffer.BlockCopy(inputBuffer, inputBufferUsed * sizeof(char), inputBuffer, 0, (inputBufferEnd - inputBufferUsed) * sizeof(char));
                        inputBufferEnd -= inputBufferUsed;
                    }
                    else
                    {
                        inputBufferEnd = 0;
                    }
                }
            }
        }