예제 #1
0
        public EndianWriter(Stream output, Endianness endianess, Encoding encoding, bool leaveOpen)
        {
            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }
            if (encoding == null)
            {
                throw new ArgumentNullException(nameof(encoding));
            }
            if (!output.CanWrite)
            {
                throw new ArgumentException("Can't write to the output stream", nameof(output));
            }
            Contract.EndContractBlock();

            OutStream      = output;
            buffer         = new byte[16];
            Encoding       = encoding;
            encoder        = Encoding.GetEncoder();
            this.leaveOpen = leaveOpen;

            Endianess         = endianess;
            resolvedEndianess = EndianessHelper.Resolve(endianess);
        }
예제 #2
0
        public EndianReader(Stream input, Endianness endianess, Encoding encoding, bool leaveOpen)
        {
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }
            if (encoding == null)
            {
                throw new ArgumentNullException(nameof(encoding));
            }
            if (!input.CanRead)
            {
                throw new ArgumentException("Can't read from the output stream", nameof(input));
            }
            Contract.EndContractBlock();

            BaseStream   = input;
            decoder      = encoding.GetDecoder();
            maxCharsSize = encoding.GetMaxCharCount(MaxCharBytesSize);
            var minBufferSize = encoding.GetMaxByteCount(1);  // max bytes per one char

            if (minBufferSize < 16)
            {
                minBufferSize = 16;
            }
            buffer = new byte[minBufferSize];
            // m_charBuffer and m_charBytes will be left null.

            // For Encodings that always use 2 bytes per char (or more),
            // special case them here to make Read() & Peek() faster.
            use2BytesPerChar = encoding is UnicodeEncoding;
            this.leaveOpen   = leaveOpen;

            Endianness        = endianess;
            resolvedEndianess = EndianessHelper.Resolve(endianess);

            Contract.Assert(decoder != null, "[EndianReader.ctor]m_decoder!=null");
        }