/// <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); } } }
/// <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); }
/// <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); }
/// <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> /// 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); } }
/// <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); } } }
/// <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); }
/// <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; }
/// <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); } }