Beispiel #1
0
        /// <summary>
        /// Sets and optionally compresses the data for the <see cref="IDataObject"/>.
        /// </summary>
        /// <param name="dataObject">The data object.</param>
        /// <param name="data">The data.</param>
        /// <param name="compressionMethod">The compression method to use if any.</param>
        /// <returns>The data object with the data set.</returns>
        public static IDataObject SetData(this IDataObject dataObject, byte[] data, string compressionMethod = EtpCompression.None)
        {
            var encoding = string.Empty;

            if (EtpCompression.RequiresCompression(compressionMethod) && data?.Length > 0)
            {
                if (!EtpCompression.CanCompress(compressionMethod))
                {
                    throw new NotSupportedException($"Compression method not supported: {compressionMethod}");
                }

                using (var compressed = new MemoryStream())
                {
                    using (var uncompressed = new MemoryStream(data))
                        using (var compressionStream = EtpCompression.TryGetCompresionStream(compressionMethod, compressed))
                        {
                            uncompressed.CopyTo(compressionStream);
                        }

                    data     = compressed.ToArray();
                    encoding = compressionMethod;
                }
            }

            dataObject.ContentEncoding = encoding;
            dataObject.Data            = data ?? new byte[0];

            return(dataObject);
        }
Beispiel #2
0
        /// <summary>
        /// Encodes the specified message.
        /// </summary>
        /// <typeparam name="T">The type of the message body.</typeparam>
        /// <param name="message">The message.</param>
        /// <param name="asBinary">Whether or not to encode as binary.</param>
        /// <param name="includeExtension">Whether or not to include a message header extension.</param>
        /// <param name="compression">The compression type.</param>
        /// <returns>The encoded byte array containing the message data.</returns>
        public static byte[] Encode <T>(this EtpMessage <T> message, bool asBinary = true, bool includeExtension = false, string compression = EtpCompression.None) where T : IEtpMessageBody
        {
            using (var stream = new MemoryStream())
                using (var compressionStream = message.Header.CanBeCompressed() ? EtpCompression.TryGetCompresionStream(compression, stream) : null)
                {
                    // create avro binary encoder to write to memory stream
                    var headerEncoder = asBinary ? (IAvroEncoder) new BinaryAvroEncoder(stream) : new JsonAvroEncoder(stream);
                    var bodyEncoder   = headerEncoder;

                    if (compressionStream != null)
                    {
                        // add Compressed flag to message flags before writing header
                        message.Header.SetCompressed();
                        bodyEncoder = asBinary ? (IAvroEncoder) new BinaryAvroEncoder(compressionStream) : new JsonAvroEncoder(compressionStream);
                    }

                    // serialize header
                    message.Header.Encode(headerEncoder);

                    // serialize header extension
                    if (message.Extension != null && includeExtension)
                    {
                        message.Extension.Encode(bodyEncoder); // Use body encoder to handle compression
                    }
                    message.Body.Encode(bodyEncoder);

                    if (compressionStream != null)
                    {
                        compressionStream.Close();
                    }

                    return(stream.ToArray());
                }
        }
Beispiel #3
0
        /// <summary>
        /// Gets the data contained by the <see cref="IDataObject"/> and decompresses the byte array, if necessary.
        /// </summary>
        /// <param name="dataObject">The data object.</param>
        /// <returns>The decompressed data as a byte array.</returns>
        public static byte[] GetData(this IDataObject dataObject)
        {
            if (!EtpCompression.RequiresDecompression(dataObject.ContentEncoding) || dataObject.Data?.Length == 0)
            {
                return(dataObject.Data);
            }

            if (!EtpCompression.CanDecompress(dataObject.ContentEncoding))
            {
                throw new NotSupportedException($"Content encoding not supported: {dataObject.ContentEncoding}");
            }

            using (var uncompressed = new MemoryStream())
            {
                using (var compressed = new MemoryStream(dataObject.Data))
                    using (var decompressionStream = EtpCompression.TryGetDecompresionStream(dataObject.ContentEncoding, compressed))
                    {
                        decompressionStream.CopyTo(uncompressed);
                    }

                return(uncompressed.ToArray());
            }
        }