public override bool Write(ref DirectBuffer directBuf) { if (_charPos == -1) { if (_byteLen <= _writeBuf.WriteSpaceLeft) { // Can simply write the string to the buffer if (_str != null) { _writeBuf.WriteString(_str, _charLen); _str = null; } else { Contract.Assert(_chars != null); _writeBuf.WriteChars(_chars, _charLen); _str = null; } _writeBuf = null; return(true); } if (_byteLen <= _writeBuf.UsableSize) { // Buffer is currently too full, but the string can fit. Force a write to fill. return(false); } // Bad case: the string doesn't fit in our buffer. _charPos = 0; // For strings, chunked/incremental conversion isn't supported // (see https://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/6584398-add-system-text-encoder-convert-method-string-in) // So for now allocate a temporary byte buffer to hold the entire string and write it directly. if (_str != null) { directBuf.Buffer = new byte[_byteLen]; _writeBuf.TextEncoding.GetBytes(_str, 0, _charLen, directBuf.Buffer, 0); return(false); } Contract.Assert(_chars != null); // For char arrays, fall through to chunked writing below } if (_str != null) { // We did a direct buffer write above, and must now clean up _str = null; _writeBuf = null; return(true); } int charsUsed; bool completed; _writeBuf.WriteStringChunked(_chars, _charPos, _chars.Length - _charPos, false, out charsUsed, out completed); if (completed) { // Flush encoder _writeBuf.WriteStringChunked(_chars, _charPos, _chars.Length - _charPos, true, out charsUsed, out completed); _chars = null; _writeBuf = null; return(true); } _charPos += charsUsed; return(false); }