Пример #1
0
        /// <summary>
        /// Parses binary image of object that implements <see cref="ISupportBinaryImage"/> from a <see cref="Stream"/>.
        /// </summary>
        /// <param name="imageSource"><see cref="ISupportBinaryImage"/> source.</param>
        /// <param name="stream">Source <see cref="Stream"/>.</param>
        /// <returns>The number of bytes parsed from the <paramref name="stream"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="imageSource"/> cannot be null.</exception>
        public static int ParseBinaryImageFromStream(this ISupportBinaryImage imageSource, Stream stream)
        {
            if ((object)imageSource == null)
            {
                throw new ArgumentNullException("imageSource");
            }

            int length = imageSource.BinaryLength;

            byte[] buffer = BufferPool.TakeBuffer(length);

            try
            {
                // Read buffer bytes from stream
                int readCount = stream.Read(buffer, 0, length);

                // Parse binary image from buffer bytes read from stream
                return(imageSource.ParseBinaryImage(buffer, 0, readCount));
            }
            finally
            {
                if (buffer != null)
                {
                    BufferPool.ReturnBuffer(buffer);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Parses binary image of object that implements <see cref="ISupportBinaryImage"/> from a <see cref="Stream"/>.
        /// </summary>
        /// <param name="imageSource"><see cref="ISupportBinaryImage"/> source.</param>
        /// <param name="stream">Source <see cref="Stream"/>.</param>
        /// <returns>The number of bytes parsed from the <paramref name="stream"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="imageSource"/> cannot be null.</exception>
        public static int ParseBinaryImageFromStream(this ISupportBinaryImage imageSource, Stream stream)
        {
            if (imageSource == null)
            {
                throw new ArgumentNullException(nameof(imageSource));
            }

            int length = imageSource.BinaryLength;

            byte[] buffer = new byte[length];

            // Read buffer bytes from stream
            int readCount = stream.Read(buffer, 0, length);

            // Parse binary image from buffer bytes read from stream
            return(imageSource.ParseBinaryImage(buffer, 0, readCount));
        }