/// <summary> /// Writes out part of an array of bytes. /// </summary> public static void WriteRawBytes(ref Span <byte> buffer, ref WriterInternalState state, ReadOnlySpan <byte> value) { if (buffer.Length - state.position >= value.Length) { // We have room in the current buffer. value.CopyTo(buffer.Slice(state.position, value.Length)); state.position += value.Length; } else { // When writing to a CodedOutputStream backed by a Stream, we could avoid // copying the data twice (first copying to the current buffer and // and later writing from the current buffer to the underlying Stream) // in some circumstances by writing the data directly to the underlying Stream. // Current this is not being done to avoid specialcasing the code for // CodedOutputStream vs IBufferWriter<byte>. int bytesWritten = 0; while (buffer.Length - state.position < value.Length - bytesWritten) { int length = buffer.Length - state.position; value.Slice(bytesWritten, length).CopyTo(buffer.Slice(state.position, length)); bytesWritten += length; state.position += length; WriteBufferHelper.RefreshBuffer(ref buffer, ref state); } // copy the remaining data int remainderLength = value.Length - bytesWritten; value.Slice(bytesWritten, remainderLength).CopyTo(buffer.Slice(state.position, remainderLength)); state.position += remainderLength; } }
private static void WriteRawByte(ref Span <byte> buffer, ref WriterInternalState state, byte value) { if (state.position == buffer.Length) { WriteBufferHelper.RefreshBuffer(ref buffer, ref state); } buffer[state.position++] = value; }