public void EncodeTest3() { byte[] data = { 1, 2, 2, 1, 1, 2, 2 }; TestIEncodingInputStream inputStream = new TestIEncodingInputStream(data); TestIEncodingOutputStream outputStream = new TestIEncodingOutputStream(); HuffmanEncoder encoder = new HuffmanEncoder(); EncodingToken token = encoder.CreateEncodingToken(inputStream, CancellationToken.None); long length = encoder.Encode(inputStream, outputStream, token, CancellationToken.None, null); Assert.IsNotNull(token.HuffmanTree); Assert.IsNotNull(token.CodingTable); WeightedSymbol?[] expected = { new WeightedSymbol(0, 7), new WeightedSymbol(1, 3), new WeightedSymbol(2, 4), null, null, null, null, }; Assert.AreEqual(expected, token.HuffmanTree.FlattenValues()); Assert.AreEqual(7, length); AssertOutputStream(outputStream, "0110011"); }
public void Initialize(IPlatformService platform, EncodingToken encodingToken, IEncodingOutputStream stream) { Guard.IsNotNull(platform, nameof(platform)); Guard.IsNotNull(encodingToken, nameof(encodingToken)); Guard.IsNotNull(stream, nameof(stream)); this.platform = platform; this.encodingToken = encodingToken; this.stream = stream; this.encoder = new HuffmanEncoder(); }
public void EncodingProgressTest1(int size) { byte[] data = new byte[size]; TestIEncodingInputStream inputStream = new TestIEncodingInputStream(data); TestIEncodingOutputStream outputStream = new TestIEncodingOutputStream(); HuffmanEncoder encoder = new HuffmanEncoder(); EncodingToken token = encoder.CreateEncodingToken(inputStream, CancellationToken.None); TestIProgressHandler progress = new TestIProgressHandler(); encoder.Encode(inputStream, outputStream, token, CancellationToken.None, progress); CollectionAssert.IsEmpty(progress.ValueList); }
public void EncodeTest4() { byte[] data = { 1, 2, 2, 2, 2, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4 }; TestIEncodingInputStream inputStream = new TestIEncodingInputStream(data); TestIEncodingOutputStream outputStream = new TestIEncodingOutputStream(); HuffmanEncoder encoder = new HuffmanEncoder(); EncodingToken token = encoder.CreateEncodingToken(inputStream, CancellationToken.None); long length = encoder.Encode(inputStream, outputStream, token, CancellationToken.None, null); Assert.IsNotNull(token.HuffmanTree); Assert.IsNotNull(token.CodingTable); Assert.AreEqual(54, length); AssertOutputStream(outputStream, "110111111111111110111111111101010101010001010000000000"); }
public void EncodeTest1() { TestIEncodingInputStream inputStream = new TestIEncodingInputStream(new byte[0]); TestIEncodingOutputStream outputStream = new TestIEncodingOutputStream(); HuffmanEncoder encoder = new HuffmanEncoder(); EncodingToken token = encoder.CreateEncodingToken(inputStream, CancellationToken.None); long length = encoder.Encode(inputStream, outputStream, token, CancellationToken.None, null); Assert.IsNotNull(token.HuffmanTree); Assert.IsNotNull(token.CodingTable); Assert.AreEqual(0, length); CollectionAssert.IsEmpty(token.HuffmanTree.FlattenValues()); CollectionAssert.IsEmpty(outputStream.BitList); }
public void EncodingProgressTest2() { byte[] data = new byte[27 * 1024]; TestIEncodingInputStream inputStream = new TestIEncodingInputStream(data); TestIEncodingOutputStream outputStream = new TestIEncodingOutputStream(); HuffmanEncoder encoder = new HuffmanEncoder(); EncodingToken token = encoder.CreateEncodingToken(inputStream, CancellationToken.None); TestIProgressHandler progress = new TestIProgressHandler(); for (int n = 0; n < 10; n++) { encoder.Encode(inputStream, outputStream, token, CancellationToken.None, progress); inputStream.Reset(); } CollectionAssert.AreEqual(new long[] { 128 * 1024, 128 * 1024 }, progress.ValueList); }
private EncodingStatistics Encode(string inputPath, string outputFile, CancellationToken cancellationToken, IProgress <CodingProgressInfo> progress) { HuffmanEncoder encoder = new HuffmanEncoder(); DirectoryEncodingInputStream directoryInputStream = new DirectoryEncodingInputStream(inputPath, fileSystemService, platform); FileEncodingOutputStream outputStream = new FileEncodingOutputStream(outputFile, platform); ITaskProgressController tpc = CreateTaskProgressController(progress, directoryInputStream); try { outputStream.BeginWrite(); tpc.StartIndeterminate(); EncodingToken encodingToken = encoder.CreateEncodingToken(directoryInputStream, cancellationToken); tpc.EndIndeterminate(); streamBuilder.Initialize(platform, encodingToken, outputStream); streamBuilder.AddWeightsTable(new BootstrapSegment(encodingToken.WeightsTable)); tpc.Start(); foreach (FileSystemEntry entry in fileSystemService.EnumFileSystemEntries(inputPath)) { switch (entry.Type) { case FileSystemEntryType.Directory: streamBuilder.AddDirectory(new DirectorySegment(entry.Name, entry.Cardinality)); break; case FileSystemEntryType.File: streamBuilder.AddFile(new FileSegment(entry.Name, entry.Path), cancellationToken, tpc); break; default: throw new InvalidOperationException(); } } outputStream.EndWrite(); tpc.Finish(); return(new EncodingStatistics(directoryInputStream.SizeInBytes, outputStream.SizeInBytes)); } catch { tpc.Error(); throw; } finally { directoryInputStream.Dispose(); outputStream.Dispose(); } }
private byte[] Encode(byte[] data, out HuffmanTreeBase tree, out long streamSize) { MemoryStream outputMemoryStream = new MemoryStream(); FileEncodingInputStream inputStream = CreateFileEncodingInputStream(data); FileEncodingOutputStream outputStream = CreateFileEncodingOutputStream(outputMemoryStream); HuffmanEncoder encoder = new HuffmanEncoder(); try { outputStream.BeginWrite(); EncodingToken token = encoder.CreateEncodingToken(inputStream, CancellationToken.None); streamSize = encoder.Encode(inputStream, outputStream, token, CancellationToken.None, null); outputStream.EndWrite(); tree = token.HuffmanTree; return(outputMemoryStream.ToArray()); } finally { inputStream.Dispose(); outputStream.Dispose(); } }
public void EncodeTest2() { TestIEncodingInputStream inputStream = new TestIEncodingInputStream(new byte[] { 1 }); TestIEncodingOutputStream outputStream = new TestIEncodingOutputStream(); HuffmanEncoder encoder = new HuffmanEncoder(); EncodingToken token = encoder.CreateEncodingToken(inputStream, CancellationToken.None); long length = encoder.Encode(inputStream, outputStream, token, CancellationToken.None, null); Assert.IsNotNull(token.HuffmanTree); Assert.IsNotNull(token.CodingTable); WeightedSymbol?[] expected = { new WeightedSymbol(1, 1), null, null, }; Assert.AreEqual(expected, token.HuffmanTree.FlattenValues()); Assert.AreEqual(1, length); AssertOutputStream(outputStream, "0"); }
void IStreamBuilder.Initialize(IPlatformService platform, EncodingToken encodingToken, IEncodingOutputStream stream) { Trace += "->Initialize;"; }