当前位置: 首页>>代码示例>>C#>>正文


C# Encoding.GetMaxCharCount方法代码示例

本文整理汇总了C#中System.Text.Encoding.GetMaxCharCount方法的典型用法代码示例。如果您正苦于以下问题:C# Encoding.GetMaxCharCount方法的具体用法?C# Encoding.GetMaxCharCount怎么用?C# Encoding.GetMaxCharCount使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Text.Encoding的用法示例。


在下文中一共展示了Encoding.GetMaxCharCount方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Init

        private void Init(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize, bool leaveOpen)
        {
            _stream = stream;
            _encoding = encoding;
            _decoder = encoding.GetDecoder();
            if (bufferSize < MinBufferSize)
            {
                bufferSize = MinBufferSize;
            }

            _byteBuffer = new byte[bufferSize];
            _maxCharsPerBuffer = encoding.GetMaxCharCount(bufferSize);
            _charBuffer = new char[_maxCharsPerBuffer];
            _byteLen = 0;
            _bytePos = 0;
            _detectEncoding = detectEncodingFromByteOrderMarks;

            // Encoding.GetPreamble() always allocates and returns a new byte[] array for
            // encodings that have a preamble.
            // We can avoid repeated allocations for the default and commonly used Encoding.UTF8
            // encoding by using our own private cached instance of the UTF8 preamble.
            // We specifically look for Encoding.UTF8 because we know it has a preamble,
            // whereas other instances of UTF8Encoding may not have a preamble enabled, and
            // there's no public way to tell if the preamble is enabled for an instance other
            // than calling GetPreamble(), which we're trying to avoid.
            // This means that other instances of UTF8Encoding are excluded from this optimization.
            _preamble = object.ReferenceEquals(encoding, Encoding.UTF8) ?
                (s_utf8Preamble ?? (s_utf8Preamble = encoding.GetPreamble())) :
                encoding.GetPreamble();

            _checkPreamble = (_preamble.Length > 0);
            _isBlocked = false;
            _closable = !leaveOpen;
        }
开发者ID:antiufo,项目名称:Shaman.ValueString,代码行数:34,代码来源:ValueStringStreamReader.cs

示例2: BinaryReader

 public BinaryReader(Stream input, Encoding encoding)
 {
     if (input == null)
     {
         throw new ArgumentNullException("input");
     }
     if (encoding == null)
     {
         throw new ArgumentNullException("encoding");
     }
     if (!input.CanRead)
     {
         throw new ArgumentException(Environment.GetResourceString("Argument_StreamNotReadable"));
     }
     this.m_stream = input;
     this.m_decoder = encoding.GetDecoder();
     this.m_maxCharsSize = encoding.GetMaxCharCount(0x80);
     int maxByteCount = encoding.GetMaxByteCount(1);
     if (maxByteCount < 0x10)
     {
         maxByteCount = 0x10;
     }
     this.m_buffer = new byte[maxByteCount];
     this.m_charBuffer = null;
     this.m_charBytes = null;
     this.m_2BytesPerChar = encoding is UnicodeEncoding;
     this.m_isMemoryStream = this.m_stream.GetType() == typeof(MemoryStream);
 }
开发者ID:pritesh-mandowara-sp,项目名称:DecompliedDotNetLibraries,代码行数:28,代码来源:BinaryReader.cs

示例3: EndianReader

        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");
        }
开发者ID:vbfox,项目名称:U2FExperiments,代码行数:33,代码来源:EndianReader.cs

示例4: StdInStreamReader

        private readonly StringBuilder _readLineSB; // SB that holds readLine output

        internal StdInStreamReader(Stream stream, Encoding encoding, int bufferSize) : base(stream: stream, encoding: encoding, detectEncodingFromByteOrderMarks: false, bufferSize: bufferSize, leaveOpen: true)
        {
            _unprocessedBufferToBeRead = new char[encoding.GetMaxCharCount(BytesToBeRead)];
            _startIndex = 0;
            _endIndex = 0;
            _readLineSB = new StringBuilder();
        }
开发者ID:noahfalk,项目名称:corefx,代码行数:9,代码来源:StdInStreamReader.cs

示例5: HttpRequestStreamReader

        public HttpRequestStreamReader(Stream stream, Encoding encoding, int bufferSize)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            if (!stream.CanRead)
            {
                throw new ArgumentException(Resources.HttpRequestStreamReader_StreamNotReadable, nameof(stream));
            }

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

            _stream = stream;
            _encoding = encoding;
            _decoder = encoding.GetDecoder();

            if (bufferSize < MinBufferSize)
            {
                bufferSize = MinBufferSize;
            }

            _byteBufferSize = bufferSize;
            _byteBuffer = new byte[bufferSize];
            var maxCharsPerBuffer = encoding.GetMaxCharCount(bufferSize);
            _charBuffer = new char[maxCharsPerBuffer];
        }
开发者ID:phinq19,项目名称:git_example,代码行数:31,代码来源:HttpRequestStreamReader.cs

示例6: BinaryReader

        public BinaryReader(Stream input, 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(Environment.GetResourceString("Argument_StreamNotReadable"));
            Contract.EndContractBlock();
            m_stream = input;
            m_decoder = encoding.GetDecoder();
            m_maxCharsSize = encoding.GetMaxCharCount(MaxCharBytesSize);
            int minBufferSize = encoding.GetMaxByteCount(1);  // max bytes per one char
            if (minBufferSize < 16) 
                minBufferSize = 16;
            m_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.
            m_2BytesPerChar = encoding is UnicodeEncoding;
            // check if BinaryReader is based on MemoryStream, and keep this for it's life
            // we cannot use "as" operator, since derived classes are not allowed
            m_isMemoryStream = (m_stream.GetType() == typeof(MemoryStream));
            m_leaveOpen = leaveOpen;

            Contract.Assert(m_decoder!=null, "[BinaryReader.ctor]m_decoder!=null");
        }
开发者ID:JonHanna,项目名称:coreclr,代码行数:29,代码来源:BinaryReader.cs

示例7: StdInReader

        private int _endIndex; // Index after last unprocessed index in the buffer;

        internal StdInReader(Encoding encoding, int bufferSize)
        {
            _encoding = encoding;
            _unprocessedBufferToBeRead = new char[encoding.GetMaxCharCount(BytesToBeRead)];
            _startIndex = 0;
            _endIndex = 0;
            _readLineSB = new StringBuilder();
        }
开发者ID:ChuangYang,项目名称:corefx,代码行数:10,代码来源:StdInReader.cs

示例8: CancellableStreamReader

 public CancellableStreamReader(Stream stream, Encoding encoding)
 {
     _stream = stream;
     _decoder = encoding.GetDecoder();
     _byteBuffer = new byte[BufferLength];
     _buffer = new char[encoding.GetMaxCharCount(BufferLength)];
     _bufferedLength = -1;
     _bufferCursor = -1;
 }
开发者ID:karno,项目名称:StarryEyes,代码行数:9,代码来源:CancellableStreamReader.cs

示例9: MyBinaryReader

 public MyBinaryReader(Stream stream, Encoding encoding)
     : base(stream, encoding)
 {
     this.m_decoder = encoding.GetDecoder();
     this.m_maxCharsSize = encoding.GetMaxCharCount(0x80);
     int maxByteCount = encoding.GetMaxByteCount(1);
     if (maxByteCount < 0x10)
     {
         maxByteCount = 0x10;
     }
 }
开发者ID:ChristianHeinz71,项目名称:SpaceEngineers,代码行数:11,代码来源:MyBinaryReader.cs

示例10: ReusableTextReader

        /// <summary>Initializes a new reusable reader.</summary>
        /// <param name="encoding">The Encoding to use.  Defaults to UTF8.</param>
        /// <param name="bufferSize">The size of the buffer to use when reading from the stream.</param>
        public ReusableTextReader(Encoding encoding = null, int bufferSize = 1024)
        {
            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }

            _decoder = encoding.GetDecoder();
            _bytes = new byte[bufferSize];
            _chars = new char[encoding.GetMaxCharCount(_bytes.Length)];
        }
开发者ID:noahfalk,项目名称:corefx,代码行数:14,代码来源:ReusableTextReader.cs

示例11: Decode

        internal static SourceText Decode(Stream stream, Encoding encoding, SourceHashAlgorithm checksumAlgorithm, bool throwIfBinaryDetected)
        {
            stream.Seek(0, SeekOrigin.Begin);

            int length = (int)stream.Length;
            if (length == 0)
            {
                return SourceText.From(string.Empty, encoding, checksumAlgorithm);
            }

            var maxCharRemainingGuess = encoding.GetMaxCharCount(length);

            using (var reader = new StreamReader(stream, encoding, detectEncodingFromByteOrderMarks: true, bufferSize: Math.Min(length, 4096), leaveOpen: true))
            {
                ArrayBuilder<char[]> chunks = ArrayBuilder<char[]>.GetInstance(1 + maxCharRemainingGuess / ChunkSize);
                while (!reader.EndOfStream)
                {
                    var nextChunkSize = ChunkSize;
                    if (maxCharRemainingGuess < ChunkSize)
                    {
                        // maxCharRemainingGuess typically overestimates a little
                        // so we will first fill a slightly smaller (maxCharRemainingGuess - 64) chunk
                        // and then use 64 char tail, which is likley to be resized.
                        nextChunkSize = Math.Max(maxCharRemainingGuess - 64, 64);
                    }

                    char[] chunk = new char[nextChunkSize];

                    int charsRead = reader.ReadBlock(chunk, 0, chunk.Length);
                    if (charsRead == 0)
                    {
                        break;
                    }

                    maxCharRemainingGuess -= charsRead;

                    if (charsRead < chunk.Length)
                    {
                        Array.Resize(ref chunk, charsRead);
                    }

                    // Check for binary files
                    if (throwIfBinaryDetected && IsBinary(chunk))
                    {
                        throw new InvalidDataException();
                    }

                    chunks.Add(chunk);
                }

                var checksum = CalculateChecksum(stream, checksumAlgorithm);
                return new LargeText(chunks.ToImmutableAndFree(), reader.CurrentEncoding, checksum, checksumAlgorithm);
            }
        }
开发者ID:Rickinio,项目名称:roslyn,代码行数:54,代码来源:LargeText.cs

示例12: Init

 private void Init(Process process, Stream stream, UserCallBack callback, Encoding encoding, int bufferSize) {
     this.process = process;
     this.stream = stream;
     this.encoding = encoding;
     this.userCallBack = callback;
     decoder = encoding.GetDecoder();
     if (bufferSize < MinBufferSize) bufferSize = MinBufferSize;
     byteBuffer = new byte[bufferSize];
     _maxCharsPerBuffer = encoding.GetMaxCharCount(bufferSize);
     charBuffer = new char[_maxCharsPerBuffer];
     cancelOperation = false;
     eofEvent = new ManualResetEvent(false);
     sb = null;
     this.bLastCarriageReturn = false;
 }
开发者ID:JokerMisfits,项目名称:linux-packaging-mono,代码行数:15,代码来源:AsyncStreamReader.cs

示例13: MultipartFormDataReader

        public MultipartFormDataReader(string contentType, Encoding contentEncoding, Stream inputStream, int bufferSize)
        {
            this.contentType = contentType;
            this.contentEncoding = contentEncoding;
            this.inputStream = inputStream;

            mode = ParsingMode.Headers;

            ParseContentType();

            mem = new MemoryStream(bufferSize);
            text = new char[
                contentEncoding.GetMaxCharCount(mem.Capacity)
                ];
        }
开发者ID:Digiex,项目名称:monotorrentwebui-winrt,代码行数:15,代码来源:MultipartFormDataReader.cs

示例14: TcpBinaryReader

        public TcpBinaryReader(Stream input, Encoding encoding)
            : base(input, encoding)
        {
            m_stream = input;
            this.m_decoder = encoding.GetDecoder();
            this.m_maxCharsSize = encoding.GetMaxCharCount(0x80);
            int maxByteCount = encoding.GetMaxByteCount(1);
            if (maxByteCount < 0x10)
            {
                maxByteCount = 0x10;
            }

            this.m_buffer = new byte[maxByteCount];
            this.m_2BytesPerChar = encoding is UnicodeEncoding;
            this.m_isMemoryStream = this.m_stream.GetType() == typeof(MemoryStream);
        }
开发者ID:Indifer,项目名称:Test,代码行数:16,代码来源:TcpBinaryReader.cs

示例15: AsyncStreamReader

        // Creates a new AsyncStreamReader for the given stream. The
        // character encoding is set by encoding and the buffer size,
        // in number of 16-bit characters, is set by bufferSize.
        internal AsyncStreamReader(Stream stream, Action<string> callback, Encoding encoding)
        {
            Debug.Assert(stream != null && encoding != null && callback != null, "Invalid arguments!");
            Debug.Assert(stream.CanRead, "Stream must be readable!");

            _stream = stream;
            _userCallBack = callback;
            _decoder = encoding.GetDecoder();
            _byteBuffer = new byte[DefaultBufferSize];

            // This is the maximum number of chars we can get from one iteration in loop inside ReadBuffer.
            // Used so ReadBuffer can tell when to copy data into a user's char[] directly, instead of our internal char[].
            int maxCharsPerBuffer = encoding.GetMaxCharCount(DefaultBufferSize);
            _charBuffer = new char[maxCharsPerBuffer];

            _cts = new CancellationTokenSource();
            _messageQueue = new Queue<string>();
        }
开发者ID:noahfalk,项目名称:corefx,代码行数:21,代码来源:AsyncStreamReader.cs


注:本文中的System.Text.Encoding.GetMaxCharCount方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。