Пример #1
0
        /// <summary>
        ///     Decodes and decompresses the container.
        /// </summary>
        /// <param name="buffer"> The buffer. </param>
        /// <returns> The decompressed container. </returns>
        /// <exception cref="IOException"> if an I/O error occurs. </exception>
        public static Container Decode(ByteBuffer buffer)
        {
            /* decode the type and length */
            var type   = buffer.get() & 0xFF;
            var length = buffer.getInt();

            /* check if we should decompress the data or not */
            if (type == COMPRESSION_NONE)
            {
                /* simply grab the data and wrap it in a buffer */
                var temp = new byte[length];
                buffer.get(temp);
                var data = ByteBuffer.wrap(temp);

                /* decode the version if present */
                var version = -1;
                if (buffer.remaining() >= 2)
                {
                    version = buffer.getShort();
                }

                /* and return the decoded container */
                return(new Container(type, data, version));
            }
            else
            {
                /* grab the length of the uncompressed data */
                var uncompressedLength = buffer.getInt();

                /* grab the data */
                var compressed = new byte[length];
                buffer.get(compressed);

                /* uncompress it */
                byte[] uncompressed;
                if (type == COMPRESSION_BZIP2)
                {
                    uncompressed = CompressionUtils.Bunzip2(compressed);
                }
                else if (type == COMPRESSION_GZIP)
                {
                    uncompressed = CompressionUtils.Gunzip(compressed);
                }
                else
                {
                    throw new IOException("Invalid compression type");
                }

                /* check if the lengths are equal */
                if (uncompressed.Length != uncompressedLength)
                {
                    throw new IOException("Length mismatch");
                }

                /* decode the version if present */
                var version = -1;
                if (buffer.remaining() >= 2)
                {
                    version = buffer.getShort();
                }

                /* and return the decoded container */
                return(new Container(type, ByteBuffer.wrap(uncompressed), version));
            }
        }