示例#1
0
        /// <summary>
        /// Copies binary image of object that implements <see cref="ISupportBinaryImage"/> to a <see cref="Stream"/>.
        /// </summary>
        /// <param name="imageSource"><see cref="ISupportBinaryImage"/> source.</param>
        /// <param name="stream">Destination <see cref="Stream"/>.</param>
        /// <exception cref="ArgumentNullException"><paramref name="imageSource"/> cannot be null.</exception>
        public static void CopyBinaryImageToStream(this ISupportBinaryImage imageSource, Stream stream)
        {
            if ((object)imageSource == null)
            {
                throw new ArgumentNullException("imageSource");
            }

            int length = imageSource.BinaryLength;

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

            try
            {
                // Copy generated binary image to buffer
                int writeCount = imageSource.GenerateBinaryImage(buffer, 0);

                // Write buffer bytes to stream, if any were generated
                if (writeCount > 0)
                {
                    stream.Write(buffer, 0, writeCount);
                }
            }
            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 ((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);
                }
            }
        }
示例#3
0
        /// <summary>
        /// Compresses the given value and places it in the compressed buffer.
        /// </summary>
        /// <param name="value">The value to be compressed.</param>
        /// <returns>The size, in bytes, of the compressed value.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="value"/> cannot be null.</exception>
        public unsafe int Compress(ISupportBinaryImage value)
        {
            byte[] buffer;
            int    bufferLength;

            int   compressedSize = 0;
            byte *iter, end;

            if ((object)value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            bufferLength = value.BinaryLength.AlignDoubleWord();
            buffer       = new byte[bufferLength];

            fixed(byte *start = buffer)
            {
                value.GenerateBinaryImage(buffer, 0);
                end = start + bufferLength;

                for (iter = start; iter < end; iter += 4)
                {
                    compressedSize += Compress(iter);
                }
            }

            return(compressedSize);
        }
示例#4
0
        /// <summary>
        /// Parses the object implementing the <see cref="ISupportBinaryImage"/> interface.
        /// </summary>
        /// <param name="image">Object to be parsed that implements the <see cref="ISupportBinaryImage"/> interface.</param>
        /// <remarks>
        /// This function takes the binary image from <see cref="ISupportBinaryImage"/> and writes the buffer to the <see cref="BinaryImageParserBase"/> stream for parsing.
        /// </remarks>
        public virtual void Parse(ISupportBinaryImage image)
        {
            int length = image.BinaryLength;

            byte[] buffer = new byte[length];

            // Generate the binary image
            image.GenerateBinaryImage(buffer, 0);

            // Write the buffer to the parsing queue
            Write(buffer, 0, length);
        }
示例#5
0
        /// <summary>
        /// Returns a binary image of an object that implements <see cref="ISupportBinaryImage"/>.
        /// </summary>
        /// <param name="imageSource"><see cref="ISupportBinaryImage"/> source.</param>
        /// <returns>A binary image of an object that implements <see cref="ISupportBinaryImage"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="imageSource"/> cannot be null.</exception>
        /// <remarks>
        /// This is a convienence method. It is often optimal to use <see cref="ISupportBinaryImage.GenerateBinaryImage"/>
        /// directly using a common buffer instead of always allocating new buffers.
        /// </remarks>
        public static byte[] BinaryImage(this ISupportBinaryImage imageSource)
        {
            if ((object)imageSource == null)
            {
                throw new ArgumentNullException("imageSource");
            }

            byte[] buffer = new byte[imageSource.BinaryLength];

            imageSource.GenerateBinaryImage(buffer, 0);

            return(buffer);
        }
        /// <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));
        }
        /// <summary>
        /// Copies binary image of object that implements <see cref="ISupportBinaryImage"/> to a <see cref="Stream"/>.
        /// </summary>
        /// <param name="imageSource"><see cref="ISupportBinaryImage"/> source.</param>
        /// <param name="stream">Destination <see cref="Stream"/>.</param>
        /// <exception cref="ArgumentNullException"><paramref name="imageSource"/> cannot be null.</exception>
        public static void CopyBinaryImageToStream(this ISupportBinaryImage imageSource, Stream stream)
        {
            if (imageSource == null)
            {
                throw new ArgumentNullException(nameof(imageSource));
            }

            int length = imageSource.BinaryLength;

            byte[] buffer = new byte[length];

            // Copy generated binary image to buffer
            int writeCount = imageSource.GenerateBinaryImage(buffer, 0);

            // Write buffer bytes to stream, if any were generated
            if (writeCount > 0)
            {
                stream.Write(buffer, 0, writeCount);
            }
        }
示例#8
0
        /// <summary>
        /// Parses the object implementing the <see cref="ISupportBinaryImage"/> interface.
        /// </summary>
        /// <param name="image">Object to be parsed that implements the <see cref="ISupportBinaryImage"/> interface.</param>
        /// <remarks>
        /// This function takes the binary image from <see cref="ISupportBinaryImage"/> and writes the buffer to the <see cref="BinaryImageParserBase"/> stream for parsing.
        /// </remarks>
        public virtual void Parse(ISupportBinaryImage image)
        {
            int length = image.BinaryLength;

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

            try
            {
                // Generate the binary image
                image.GenerateBinaryImage(buffer, 0);

                // Write the buffer to the parsing queue
                Write(buffer, 0, length);
            }
            finally
            {
                if (buffer != null)
                {
                    BufferPool.ReturnBuffer(buffer);
                }
            }
        }
示例#9
0
 public override void Parse(ISupportBinaryImage image)
 {
     throw new NotImplementedException("This method should not be called directly, call the Parse(TSourceIdentifier,ISupportBinaryImage) method to queue data for parsing instead.");
 }
示例#10
0
 /// <summary>
 /// Queues the object implementing the <see cref="ISupportBinaryImage"/> interface, from the specified data source, onto the stream for parsing.
 /// </summary>
 /// <param name="source">Identifier of the data source.</param>
 /// <param name="image">Object to be parsed that implements the <see cref="ISupportBinaryImage"/> interface.</param>
 /// <remarks>
 /// This method takes the binary image from <see cref="ISupportBinaryImage"/> and writes the buffer to the <see cref="BinaryImageParserBase"/> stream for parsing.
 /// </remarks>
 public virtual void Parse(TSourceIdentifier source, ISupportBinaryImage image)
 {
     Parse(source, image.BinaryImage());
 }
示例#11
0
 /// <summary>
 /// This is a common optimized block copy function for any kind of data.
 /// </summary>
 /// <param name="channel">Source channel with BinaryImage data to copy.</param>
 /// <param name="destination">Destination buffer to hold copied buffer data.</param>
 /// <param name="index">
 /// Index into <paramref name="destination"/> buffer to begin copy. Index is automatically incremented by <see cref="ISupportBinaryImage.BinaryLength"/>.
 /// </param>
 /// <remarks>
 /// This function automatically advances index for convenience.
 /// </remarks>
 public static void CopyImage(this ISupportBinaryImage channel, byte[] destination, ref int index)
 {
     index += channel.GenerateBinaryImage(destination, index);
 }
示例#12
0
        /// <summary>
        /// Compresses the given value and places it in the compressed buffer.
        /// </summary>
        /// <param name="value">The value to be compressed.</param>
        /// <returns>The size, in bytes, of the compressed value.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="value"/> cannot be null.</exception>
        public unsafe int Compress(ISupportBinaryImage value)
        {
            byte[] buffer;
            int bufferLength;

            int compressedSize = 0;
            byte* iter, end;

            if ((object)value == null)
                throw new ArgumentNullException("value");

            bufferLength = value.BinaryLength.AlignDoubleWord();
            buffer = new byte[bufferLength];

            fixed (byte* start = buffer)
            {
                value.GenerateBinaryImage(buffer, 0);
                end = start + bufferLength;

                for (iter = start; iter < end; iter += 4)
                    compressedSize += Compress(iter);
            }

            return compressedSize;
        }
示例#13
0
        /// <summary>
        /// Parses the object implementing the <see cref="ISupportBinaryImage"/> interface.
        /// </summary>
        /// <param name="image">Object to be parsed that implements the <see cref="ISupportBinaryImage"/> interface.</param>
        /// <remarks>
        /// This function takes the binary image from <see cref="ISupportBinaryImage"/> and writes the buffer to the <see cref="BinaryImageParserBase"/> stream for parsing.
        /// </remarks>
        public virtual void Parse(ISupportBinaryImage image)
        {
            int length = image.BinaryLength;
            byte[] buffer = BufferPool.TakeBuffer(length);

            try
            {
                // Generate the binary image
                image.GenerateBinaryImage(buffer, 0);

                // Write the buffer to the parsing queue
                Write(buffer, 0, length);
            }
            finally
            {
                if (buffer != null)
                    BufferPool.ReturnBuffer(buffer);
            }
        }
示例#14
0
 /// <summary>
 /// Parses the object implementing the <see cref="ISupportBinaryImage"/> interface.
 /// </summary>
 /// <param name="image">Object to be parsed that implements the <see cref="ISupportBinaryImage"/> interface.</param>
 /// <remarks>
 /// This function takes the binary image from <see cref="ISupportBinaryImage"/> and writes the buffer to the <see cref="BinaryImageParserBase"/> stream for parsing.
 /// </remarks>
 public virtual void Parse(ISupportBinaryImage image)
 {
     Write(image.BinaryImage, 0, image.BinaryLength);
 }
示例#15
0
        /// <summary>
        /// Parses the object implementing the <see cref="ISupportBinaryImage"/> interface.
        /// </summary>
        /// <param name="image">Object to be parsed that implements the <see cref="ISupportBinaryImage"/> interface.</param>
        /// <remarks>
        /// This function takes the binary image from <see cref="ISupportBinaryImage"/> and writes the buffer to the <see cref="BinaryImageParserBase"/> stream for parsing.
        /// </remarks>
        public virtual void Parse(ISupportBinaryImage image)
        {
            int length = image.BinaryLength;
            byte[] buffer = new byte[length];

            // Generate the binary image
            image.GenerateBinaryImage(buffer, 0);

            // Write the buffer to the parsing queue
            Write(buffer, 0, length);
        }