Esempio n. 1
0
        internal bool TryDrainRemainingDataForGetBytes(Span <byte> bytes, out int bytesWritten)
        {
            int originalBytesLength = bytes.Length;

            Rune thisRune;

            while ((thisRune = GetNextRune()).Value != 0)
            {
                switch (encoding.EncodeRune(thisRune, bytes, out int bytesWrittenJustNow))
                {
                case OperationStatus.Done:

                    bytes = bytes.Slice(bytesWrittenJustNow);
                    continue;

                case OperationStatus.DestinationTooSmall:

                    // Since we're not consuming the Rune we just read, back up as many chars as necessary
                    // to undo the read we just performed, then report to our caller that we ran out of space.

                    for (int i = 0; i < thisRune.Utf16SequenceLength; i++)
                    {
                        MovePrevious();
                    }

                    bytesWritten = originalBytesLength - bytes.Length;
                    return(false);    // ran out of destination buffer

                case OperationStatus.InvalidData:

                    // We can't fallback the fallback. We can't make forward progress, so report to our caller
                    // that something went terribly wrong. The error message contains the fallback char that
                    // couldn't be converted. (Ideally we'd provide the first char that originally triggered
                    // the fallback, but it's complicated to keep this state around, and a fallback producing
                    // invalid data should be a very rare occurrence.)

                    ThrowLastCharRecursive(thisRune.Value);
                    break;     // will never be hit; call above throws

                default:

                    Debug.Fail("Unexpected return value.");
                    break;
                }
            }

            bytesWritten = originalBytesLength - bytes.Length;
            return(true); // finished successfully
        }