Beispiel #1
0
        public static BufferedMessageSet Decompress(Message message)
        {
            switch (message.CompressionCodec)
            {
            case CompressionCodecs.DefaultCompressionCodec:
            case CompressionCodecs.GZIPCompressionCodec:
                byte[] inputBytes = message.Payload;
                using (MemoryStream outputStream = new MemoryStream())
                {
                    using (MemoryStream inputStream = new MemoryStream(inputBytes))
                    {
                        using (GZipStream gzipInputStream = new GZipStream(inputStream, CompressionMode.Decompress))
                        {
                            try
                            {
                                gzipInputStream.CopyTo(outputStream);
                                gzipInputStream.Close();
                            }
                            catch (IOException ex)
                            {
                                Logger.Error("Error while reading from the GZIP input stream", ex);
                                throw;
                            }
                        }
                    }

                    return(BufferedMessageSet.ParseFrom(outputStream.ToArray()));
                }

            default:
                throw new UnknownCodecException(String.Format(CultureInfo.CurrentCulture, "Unknown Codec: {0}", message.CompressionCodec));
            }
        }
Beispiel #2
0
        public static BufferedMessageSet Decompress(Message message, int partition)
        {
            switch (message.CompressionCodec)
            {
            case CompressionCodecs.DefaultCompressionCodec:
            case CompressionCodecs.GZIPCompressionCodec:
                var inputBytes = message.Payload;
                using (var outputStream = new MemoryStream())
                {
                    using (var inputStream = new MemoryStream(inputBytes))
                    {
                        using (var gzipInputStream = new GZipStream(inputStream, CompressionMode.Decompress))
                        {
                            try
                            {
                                gzipInputStream.CopyTo(outputStream);
                                gzipInputStream.Close();
                            }
                            catch (IOException ex)
                            {
                                Logger.InfoFormat("Error while reading from the GZIP input stream: {0}",
                                                  ex.FormatException());
                                throw;
                            }
                        }
                    }

                    outputStream.Position = 0;
                    using (var reader = new KafkaBinaryReader(outputStream))
                    {
                        return(BufferedMessageSet.ParseFrom(reader, (int)outputStream.Length, partition));
                    }
                }

            case CompressionCodecs.SnappyCompressionCodec:
                try
                {
                    using (var stream = new MemoryStream(SnappyHelper.Decompress(message.Payload)))
                    {
                        using (var reader = new KafkaBinaryReader(stream))
                        {
                            return(BufferedMessageSet.ParseFrom(reader, (int)stream.Length, partition));
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.ErrorFormat("Error while reading from the Snappy input stream  {0}",
                                       ex.FormatException());
                    throw;
                }

            default:
                throw new UnknownCodecException(string.Format(CultureInfo.CurrentCulture, "Unknown Codec: {0}",
                                                              message.CompressionCodec));
            }
        }