public void AddFile(FileSegment segment, CancellationToken cancellationToken, IProgressHandler progress) { string name = segment.Name; string path = segment.Path; Guard.IsNotNullOrEmpty(name, nameof(name)); Guard.IsNotNullOrEmpty(path, nameof(path)); stream.Write(StreamKind.FS_CODE); stream.Write(2 * name.Length); for (int n = 0; n < name.Length; n++) { stream.Write(name[n]); } IStreamPosition sizePosition = stream.SavePosition(); stream.Write(0L); using (FileEncodingInputStream fileStream = new FileEncodingInputStream(path, platform)) { long sequenceLength = encoder.Encode(fileStream, stream, encodingToken, cancellationToken, progress); LayoutStreamInMemory(sequenceLength); IStreamPosition endPosition = stream.SavePosition(); stream.RestorePosition(sizePosition); stream.Write(sequenceLength); stream.RestorePosition(endPosition); } }
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 ReadSymbolTest() { byte[] data = { 0xFF, 0x33, 0x00, 0x12, 0x78 }; byte symbol; using (FileEncodingInputStream stream = CreateFileEncodingInputStream(data)) { Assert.IsTrue(stream.ReadSymbol(out symbol)); Assert.AreEqual(0xFF, symbol); Assert.IsTrue(stream.ReadSymbol(out symbol)); Assert.AreEqual(0x33, symbol); Assert.IsTrue(stream.ReadSymbol(out symbol)); Assert.AreEqual(0x00, symbol); Assert.IsTrue(stream.ReadSymbol(out symbol)); Assert.AreEqual(0x12, symbol); Assert.IsTrue(stream.ReadSymbol(out symbol)); Assert.AreEqual(0x78, symbol); Assert.IsFalse(stream.ReadSymbol(out symbol)); Assert.AreEqual(0x00, symbol); Assert.IsFalse(stream.ReadSymbol(out symbol)); Assert.AreEqual(0x00, symbol); } }
public void ResetTest() { byte[] data = { 0x12, 0x35, 0x01, 0x14 }; byte symbol; using (FileEncodingInputStream stream = CreateFileEncodingInputStream(data)) { Assert.IsTrue(stream.ReadSymbol(out symbol)); Assert.AreEqual(0x12, symbol); Assert.IsTrue(stream.ReadSymbol(out symbol)); Assert.AreEqual(0x35, symbol); stream.Reset(); Assert.IsTrue(stream.ReadSymbol(out symbol)); Assert.AreEqual(0x12, symbol); Assert.IsTrue(stream.ReadSymbol(out symbol)); Assert.AreEqual(0x35, symbol); Assert.IsTrue(stream.ReadSymbol(out symbol)); Assert.AreEqual(0x01, symbol); Assert.IsTrue(stream.ReadSymbol(out symbol)); Assert.AreEqual(0x14, symbol); Assert.IsFalse(stream.ReadSymbol(out symbol)); Assert.AreEqual(0x00, symbol); } }