GSF.IO.BlockAllocatedMemoryStream.ToArray C# (CSharp) Метод

ToArray() публичный Метод

Writes the stream contents to a byte array, regardless of the Position property.
This may fail if there is not enough contiguous memory available to hold current size of stream. When possible use methods which operate on streams directly instead.
Cannot create a byte array with more than 2,147,483,591 elements. The stream is closed.
public ToArray ( ) : byte[]
Результат byte[]
        public byte[] ToArray()
        {
            if (m_disposed)
                throw new ObjectDisposedException("BlockAllocatedMemoryStream", "The stream is closed.");

            if (m_length > 0x7FFFFFC7L)
                throw new InvalidOperationException("Cannot create a byte array of size " + m_length);

            byte[] destination = new byte[m_length];
            long originalPosition = m_position;

            m_position = 0;
            Read(destination, 0, (int)m_length);
            m_position = originalPosition;

            return destination;
        }

Usage Example

Пример #1
0
 /// <summary>
 /// Reads entire <see cref="Stream"/> contents, and returns <see cref="byte"/> array of data.
 /// </summary>
 /// <param name="source">The <see cref="Stream"/> to be converted to <see cref="byte"/> array.</param>
 /// <returns>An array of <see cref="byte"/>.</returns>
 public static byte[] ReadStream(this Stream source)
 {
     using (BlockAllocatedMemoryStream outStream = new BlockAllocatedMemoryStream())
     {
         source.CopyTo(outStream);
         return(outStream.ToArray());
     }
 }
All Usage Examples Of GSF.IO.BlockAllocatedMemoryStream::ToArray