Ejemplo n.º 1
0
        /// <summary>
        /// Saves writer content to the given byte[] and it's offset.
        /// </summary>
        /// <param name="data">The byte[].</param>
        /// <param name="offset">The offset.</param>
        /// <returns></returns>
        public int ToArray(byte[] data, int offset)
        {
            long size = length + currentSegment.Length;

            if (data.Length < size + offset)
            {
                throw new ArgumentException("Not enough memory in data.", "data");
            }

            int position = offset;

            ManagedBinaryMemoryWriterSegment segment = this.segment;

            while (segment != null)
            {
                segment.ToArray(data, ref position);

                segment = segment.next;
            }

            return((int)size);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Generates a new array with the contents of this writer. Beware, the content shouldn't exceed .NET memory array limit of nearly 2 GiB.
        /// </summary>
        /// <returns>The array.</returns>
        public byte[] ToArray()
        {
            long size = length + currentSegment.Length;

            if (size > 2147000000)
            {
                throw new OutOfMemoryException("Array buffers can't exceed 2147000000 bytes. Write to a stream, if you generated bigger data than the near 2 GiB limit.");
            }

            byte[] data = new byte[size];

            int position = 0;

            ManagedBinaryMemoryWriterSegment segment = this.segment;

            while (segment != null)
            {
                segment.ToArray(data, ref position);

                segment = segment.next;
            }

            return(data);
        }