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); }
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; }
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; }
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 })); }
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 })); }
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 })); }
public void DoubleByteDecode() { var varint = new VarInt32(new byte[] { 0x80, 0x1 }); Assert.That(varint.Value, Is.EqualTo(128)); }