コード例 #1
0
        /// <summary>
        /// Copies a <see cref="Stream"/> instance to a new instance of a <see cref="MemoryStream"/>.
        /// </summary>
        /// <param name="source">The <see cref="Stream"/> to copy from.</param>
        /// <returns>A new instance of a <see cref="MemoryStream"/> containing the data in the <paramref name="source"/>.</returns>
        public static MemoryStream ToMemoryStream(this Stream source)
        {
            if (source.IsNull())
            {
                throw new ArgumentNullException("stream");
            }

            MemoryStream mStream = source as MemoryStream;

            if (mStream.IsNotNull())
            {
                return(mStream);
            }

            return(new MemoryStream(source.ToByteArray()));
        }
コード例 #2
0
/// <summary>
/// Converts a <see cref="Stream"/> to a <see cref="Byte"/> <see cref="Array"/>.
/// </summary>
/// <param name="stream">The <see cref="Stream"/> to convert.</param>
/// <returns>A <see cref="Byte"/> <see cref="Array"/> of the data contained in the <paramref name="stream"/>.</returns>
/// <exception cref="ArgumentNullException"> thrown if the <paramref name="stream"/> is <c>Null</c>.</exception>
        public static byte[] ToByteArray(this Stream stream)
        {
            if (stream.IsNull())
            {
                throw new ArgumentNullException("stream");
            }

            MemoryStream mStream = stream as MemoryStream;

            if (mStream.IsNotNull())
            {
                return(mStream.ToArray());
            }

            stream.Position = 0;
            byte[] buffer = new byte[stream.Length];
            for (int totalBytesCopied = 0; totalBytesCopied < stream.Length;)
            {
                totalBytesCopied += stream.Read(buffer, totalBytesCopied, Convert.ToInt32(stream.Length) - totalBytesCopied);
            }
            return(buffer);
        }