Skip to content

Commit 7a4de80

Browse files
committed
Add public Options fields
closes #30
1 parent c901529 commit 7a4de80

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Zstd
1818
provides both good compression ratio _and_ speed for your standard compression
1919
needs. "Standard" translates into everyday situations which neither look for
2020
highest possible ratio (which LZMA and ZPAQ cover) nor extreme speeds (which
21-
LZ4 covers). Zstandard is licensed under [BSD 3-Clause License](Native/LICENSE).
21+
LZ4 covers). Zstandard is licensed under [BSD 3-Clause License](ZstdNet/build/LICENSE).
2222

2323
**Zstd** is initially developed by Yann Collet and the source is available at:
2424
https://github.com/facebook/zstd
@@ -285,7 +285,7 @@ performance and memory overhead.
285285
* `TrainFromBuffer` generates a compression dictionary from a collection of samples.
286286

287287
```c#
288-
static byte[] TrainFromBuffer(ICollection<byte[]> samples, int dictCapacity = DefaultDictCapacity);
288+
static byte[] TrainFromBuffer(IEnumerable<byte[]> samples, int dictCapacity = DefaultDictCapacity);
289289
```
290290

291291
Options:

ZstdNet.Tests/SteamingCompressionTests.cs

+36
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,42 @@ public void StreamingCompressionSimpleWrite(byte[] data, int offset, int count)
110110
Assert.AreEqual(dataToCompress, resultStream.ToArray());
111111
}
112112

113+
[Test]
114+
public void StreamingCompressionKeepReferenceToDict()
115+
{
116+
var dict = TrainDict();
117+
Span<byte> data = dict;
118+
119+
var tempStream = new MemoryStream();
120+
using(var compressionStream = new CompressionStream(tempStream, new CompressionOptions(dict)))
121+
{
122+
for(int i = 0; i < data.Length; i++)
123+
{
124+
compressionStream.Write(data.Slice(i, 1));
125+
126+
GC.Collect();
127+
GC.WaitForPendingFinalizers();
128+
GC.Collect();
129+
}
130+
}
131+
132+
tempStream.Seek(0, SeekOrigin.Begin);
133+
134+
using(var decompressionStream = new DecompressionStream(tempStream, new DecompressionOptions(dict)))
135+
{
136+
for(int i = 0; i < data.Length; i++)
137+
{
138+
Assert.AreEqual(data[i], decompressionStream.ReadByte());
139+
140+
GC.Collect();
141+
GC.WaitForPendingFinalizers();
142+
GC.Collect();
143+
}
144+
145+
Assert.AreEqual(-1, decompressionStream.ReadByte());
146+
}
147+
}
148+
113149
[TestCase(1)]
114150
[TestCase(2)]
115151
[TestCase(3)]

ZstdNet/CompressionStream.cs

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public class CompressionStream : Stream
2020
private IntPtr cStream;
2121
private UIntPtr pos;
2222

23+
public readonly CompressionOptions Options;
24+
2325
public CompressionStream(Stream stream)
2426
: this(stream, CompressionOptions.Default)
2527
{}
@@ -42,6 +44,7 @@ public CompressionStream(Stream stream, CompressionOptions options, int bufferSi
4244
cStream = ZSTD_createCStream().EnsureZstdSuccess();
4345
ZSTD_CCtx_reset(cStream, ZSTD_ResetDirective.ZSTD_reset_session_only).EnsureZstdSuccess();
4446

47+
Options = options;
4548
if(options != null)
4649
{
4750
options.ApplyCompressionParams(cStream);

ZstdNet/DecompressionStream.cs

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public class DecompressionStream : Stream
2121
private UIntPtr pos;
2222
private UIntPtr size;
2323

24+
public readonly DecompressionOptions Options;
25+
2426
public DecompressionStream(Stream stream)
2527
: this(stream, null)
2628
{}
@@ -43,6 +45,7 @@ public DecompressionStream(Stream stream, DecompressionOptions options, int buff
4345
dStream = ZSTD_createDStream().EnsureZstdSuccess();
4446
ZSTD_DCtx_reset(dStream, ZSTD_ResetDirective.ZSTD_reset_session_only).EnsureZstdSuccess();
4547

48+
Options = options;
4649
if(options != null)
4750
{
4851
options.ApplyDecompressionParams(dStream);

0 commit comments

Comments
 (0)