/// <summary>
 /// Copies compressed contents from <paramref name="source"/> into the <paramref name="target"/> from the <paramref name="targetOffset"/>
 /// </summary>
 /// <param name="target">The <see cref="ByteBuffer"/> that will be written to.</param>
 /// <param name="source">The source <see cref="ByteBuffer"/> to read the data from.</param>
 /// <param name="targetOffset">The <paramref name="target"/> buffer's offset to start writing from.</param>
 /// <returns>The number of bytes written.</returns>
 public static int CompressedCopy(this ByteBuffer target, ByteBuffer source, int targetOffset)
 {
     byte[] compressed;
     using (var ms = new MemoryStream(source.ToArray()))
     {
         compressed = Compress(ms);
     }
     target.BlockCopy(compressed, 0, targetOffset, compressed.Length);
     return compressed.Length;
 }
 /// <summary>
 /// Reads bytes from a specific offset.
 /// </summary>
 /// <param name="buffer">The buffer to read the bytes from.</param>
 /// <param name="offset">The offset in the buffer to start reading.</param>
 /// <param name="length">The number of bytes to read.</param>
 /// <returns>The value read from the buffer.</returns>
 public static byte[] ReadBytes(this byte[] buffer, int offset, int length)
 {
     byte[] bytes = new byte[length];
     buffer.BlockCopy(offset, bytes, 0, length);
     return bytes;
 }
Exemple #3
0
        /// <summary>
        /// Decompress a byte array.
        /// </summary>
        /// <param name="source">The <see cref="Byte"/> array to decompress.</param>
        /// <param name="length">The number of bytes to read into the byte array for compression.</param>
        /// <param name="startIndex">An <see cref="Int32"/> representing the start index of the byte array.</param>
        /// <returns>A decompressed <see cref="Byte"/> array.</returns>
        public static byte[] Decompress(this byte[] source, int startIndex, int length)
        {
            const byte CompressionStrengthMask = (byte)(Bits.Bit00 | Bits.Bit01);

            // Unmask compression strength from first buffer byte
            CompressionStrength strength = (CompressionStrength)(source[startIndex] & CompressionStrengthMask);

            if (strength == CompressionStrength.NoCompression)
            {
                // No compression was applied to original buffer, return specified portion of the buffer
                return source.BlockCopy(startIndex + 1, length - 1);
            }
            else
            {
                // Create a new decompression deflater
                MemoryStream compressedData = new MemoryStream(source, startIndex + 1, length - 1);
                DeflateStream inflater = new DeflateStream(compressedData, CompressionMode.Decompress);
                int compressionDepth = (source[startIndex] & ~CompressionStrengthMask) >> 2;

                // Read uncompressed data
                byte[] destination = inflater.ReadStream();

                // When user requests muli-pass compression, there may be multiple compression passes on a buffer,
                // so we cycle through the needed uncompressions to get back to the original data
                if (strength == CompressionStrength.MultiPass && compressionDepth > 0)
                    return destination.Decompress();
                else
                    return destination;
            }
        }