Beispiel #1
0
        public static int Compress(Stream input, Stream output)
        {
            var length = (int)(input.Length - input.Position);

            var varInt = new VarInt32(length).GetEncodedValue();

            output.Write(varInt, 0, varInt.Length);

            int bytesWritten = varInt.Length;

            int bytesToRead = Math.Min(length, CompressorConstants.BlockSize);

            var fragment = MemoryPool.Instance.Take(bytesToRead);

            int maxOutput = MaxCompressedOutput(bytesToRead);

            var block = MemoryPool.Instance.Take(maxOutput);

            while (length > 0)
            {
                var fragmentSize = input.Read(fragment, 0, bytesToRead);
                var hashTable    = new HashTable(fragmentSize);

                int blockSize = CompressFragment(fragment, fragmentSize, hashTable, block);
                output.Write(block, 0, blockSize);
                bytesWritten += blockSize;

                length -= bytesToRead;
            }

            MemoryPool.Instance.Return(fragment);
            MemoryPool.Instance.Return(block);

            return(bytesWritten);
        }
Beispiel #2
0
        public int Compress(Stream input, Stream output)
        {
            var length = (int) input.Length;

            var varInt = new VarInt32(length).GetEncodedValue();
            output.Write(varInt, 0, varInt.Length);

            int bytesWritten = varInt.Length;

            int bytesToRead = Math.Min(length, CompressorConstants.BlockSize);
            var fragment = new byte[bytesToRead];
            int maxOutput = MaxCompressedOutput(bytesToRead);

            var block = new byte[maxOutput];

            while (length > 0)
            {
                var fragmentSize = input.Read(fragment, 0, bytesToRead);
                var hashTable = new HashTable(fragmentSize);

                int blockSize = CompressFragment(fragment, fragmentSize, hashTable, block);
                output.Write(block, 0, blockSize);
                bytesWritten += blockSize;

                length -= bytesToRead;
            }

            return bytesWritten;
        }
Beispiel #3
0
        public static int Compress(Stream input, Stream output)
        {
            var length = (int)(input.Length - input.Position);

            var varInt = new VarInt32(length).GetEncodedValue();
            output.Write(varInt, 0, varInt.Length);

            int bytesWritten = varInt.Length;

            int bytesToRead = Math.Min(length, CompressorConstants.BlockSize);

            var fragment = MemoryPool.Instance.Take(bytesToRead);

            int maxOutput = MaxCompressedOutput(bytesToRead);

            var block = MemoryPool.Instance.Take(maxOutput);

            while (length > 0)
            {
                var fragmentSize = input.Read(fragment, 0, bytesToRead);
                var hashTable = new HashTable(fragmentSize);

                int blockSize = CompressFragment(fragment, fragmentSize, hashTable, block);
                output.Write(block, 0, blockSize);
                bytesWritten += blockSize;

                length -= bytesToRead;
            }

            MemoryPool.Instance.Return(fragment);
            MemoryPool.Instance.Return(block);

            return bytesWritten;
        }
Beispiel #4
0
        public void QuadByteEncode()
        {
            var varint = new VarInt32(0x200000);
            Assert.That(varint.GetEncodedValue(), Is.EqualTo(new byte[] { 0x80, 0x80, 0x80, 0x01 }));

            varint = new VarInt32(0xfffffff);
            Assert.That(varint.GetEncodedValue(), Is.EqualTo(new byte[] { 0xff, 0xff, 0xff, 0x7f }));
        }
Beispiel #5
0
        public void NegativeEncode()
        {
            var varint = new VarInt32(-1);
            Assert.That(varint.GetEncodedValue(), Is.EqualTo(new byte[] { 0xff, 0xff, 0xff, 0xff, 0xf }));

            varint = new VarInt32(int.MinValue);
            Assert.That(varint.GetEncodedValue(), Is.EqualTo(new byte[] { 0x80, 0x80, 0x80, 0x80, 0x8 }));
        }
Beispiel #6
0
        public void FullByteEncode()
        {
            var varint = new VarInt32(0x10000000);
            Assert.That(varint.GetEncodedValue(), Is.EqualTo(new byte[] { 0x80, 0x80, 0x80, 0x80, 0x01 }));

            varint = new VarInt32(int.MaxValue);
            Assert.That(varint.GetEncodedValue(), Is.EqualTo(new byte[] { 0xff, 0xff, 0xff, 0xff, 0x7 }));
        }
Beispiel #7
0
 public void DoubleByteDecode()
 {
     var varint = new VarInt32(new byte[] { 0x80, 0x1 });
     Assert.That(varint.Value, Is.EqualTo(128));
 }