// Calls a write action repeatedly while copying bytes from a source list through a buffer.
        // If the provided source is a byte array, no intermediate buffer is created.
        private static void WriteNoCheck(ITriflesByteWriter sink, IList <byte> source, int index, int count, int bufferSize)
        {
            if (count > 0)
            {
                {
                    var byteArraySource = source as byte[];
                    if (byteArraySource != null)
                    {
                        sink.Write(byteArraySource, index, count);
                        return;
                    }
                }

                if (bufferSize <= 0)
                {
                    bufferSize = DefaultWriteBufferSize;
                }

                if (bufferSize > count)
                {
                    bufferSize = count;
                }

                source.BufferViaArray(index, count, sink.Write, new byte[bufferSize]);
            }
        }
 /// <summary>
 /// Writes blocks of bytes to the current sink using data read from a list.
 /// </summary>
 /// <param name="sink"></param>
 /// <param name="source"></param>
 /// <param name="offset"></param>
 /// <param name="count"></param>
 /// <param name="bufferSize">
 /// The size of the temporary buffer used to copy elements from the provided source.
 /// </param>
 internal static void Write(ITriflesByteWriter sink, IList <byte> source, int offset, int count, int bufferSize = 0)
 {
     Checker.NotNull(sink, "sink");
     Checker.NotNull(source, "source");
     Checker.SpanInRange(source.Count, offset, count, "offset", "count");
     WriteNoCheck(sink, source, offset, count, bufferSize);
 }
        // Calls a write action repeatedly while copying bytes from a source enumerable through a
        // buffer. If the provided source is an IList<byte>, the algorithm used may be random-access instead.
        private static void WriteNoCheck(ITriflesByteWriter sink, IEnumerable <byte> source, int bufferSize)
        {
            {
                var ilistSource = source as IList <byte>;
                if (ilistSource != null)
                {
                    WriteNoCheck(sink, ilistSource, 0, ilistSource.Count, bufferSize);
                    return;
                }
            }

            if (bufferSize <= 0)
            {
                bufferSize = DefaultWriteBufferSize;
            }

            source.BufferViaArray(sink.Write, new byte[bufferSize]);
        }
 internal static ITriflesByteWriter ToTriflesByteWriter(ITriflesByteWriter sink)
 {
     Checker.NotNull(sink, "sink");
     return(sink);
 }
 /// <summary>
 /// Writes blocks of bytes to the current sink using data read from a sequence.
 /// </summary>
 /// <param name="sink"></param>
 /// <param name="source"></param>
 /// <param name="bufferSize">
 /// The size of the temporary buffer used to copy elements from the provided source.
 /// </param>
 internal static void Write(ITriflesByteWriter sink, IEnumerable <byte> source, int bufferSize = 0)
 {
     Checker.NotNull(sink, "sink");
     Checker.NotNull(source, "source");
     WriteNoCheck(sink, source, bufferSize);
 }
 /// <summary>
 /// Writes blocks of bytes to the current sink using data read from a list.
 /// </summary>
 /// <param name="sink"></param>
 /// <param name="source"></param>
 /// <param name="bufferSize">
 /// The size of the temporary buffer used to copy elements from the provided source.
 /// </param>
 internal static void Write(ITriflesByteWriter sink, IList <byte> source, int bufferSize = 0)
 {
     Checker.NotNull(sink, "sink");
     Checker.NotNull(source, "source");
     WriteNoCheck(sink, source, 0, source.Count, bufferSize);
 }
 /// <summary>
 /// Returns this sink as an <see cref="ITriflesByteWriter"/>.
 /// </summary>
 /// <param name="sink">A sink to convert.</param>
 /// <returns><paramref name="sink"/> as an <see cref="ITriflesByteWriter"/>.</returns>
 /// <exception cref="ArgumentNullException"><paramref name="sink"/> is <c>null</c>.</exception>
 public static ITriflesByteWriter ToTriflesByteWriter(this ITriflesByteWriter sink)
 {
     return(TriflesByteWriterImpl.ToTriflesByteWriter(sink));
 }
 /// <summary>
 /// Writes blocks of bytes to the current sink using data read from a sequence.
 /// </summary>
 /// <param name="sink"></param>
 /// <param name="source"></param>
 /// <param name="bufferSize">
 /// The size of the temporary buffer used to copy elements from the provided source.
 /// </param>
 public static void Write(this ITriflesByteWriter sink, IEnumerable <byte> source, int bufferSize = 0)
 {
     TriflesByteWriterImpl.Write(sink.ToTriflesByteWriter(), source, bufferSize);
 }
 /// <summary>
 /// Writes blocks of bytes to the current sink using data read from a list.
 /// </summary>
 /// <param name="sink"></param>
 /// <param name="source"></param>
 /// <param name="offset"></param>
 /// <param name="count"></param>
 /// <param name="bufferSize">
 /// The size of the temporary buffer used to copy elements from the provided source.
 /// </param>
 public static void Write(this ITriflesByteWriter sink, IList <byte> source, int offset, int count, int bufferSize = 0)
 {
     TriflesByteWriterImpl.Write(sink.ToTriflesByteWriter(), source, offset, count, bufferSize);
 }