示例#1
0
        /// <summary>
        /// Converts a given number of bytes into a char array and returns it. Expects the required bytes
        /// to already be in the buffer
        /// </summary>
        internal char[] ReadChars(int byteCount)
        {
            if (byteCount <= Size)
            {
                Ensure(byteCount);
                var result = TextEncoding.GetChars(_buf, ReadPosition, byteCount);
                ReadPosition += byteCount;
                return(result);
            }

            // Worst case: our buffer isn't big enough. For now, pessimistically allocate a char buffer
            // that will hold the maximum number of characters for the column length
            // TODO: Optimize
            var pessimisticNumChars = TextEncoding.GetMaxCharCount(byteCount);
            var pessimisticOutput   = new char[pessimisticNumChars];
            var actualNumChars      = PopulateCharArray(pessimisticOutput, byteCount);

            if (actualNumChars == pessimisticNumChars)
            {
                return(pessimisticOutput);
            }
            var output = new char[actualNumChars];

            Array.Copy(pessimisticOutput, 0, output, 0, actualNumChars);
            return(output);
        }