forked from sshnet/SSH.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZlib.cs
More file actions
130 lines (111 loc) · 3.97 KB
/
Copy pathZlib.cs
File metadata and controls
130 lines (111 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System.IO;
#if NET
using System.IO.Compression;
#else
using Org.BouncyCastle.Utilities.Zlib;
#endif
namespace Renci.SshNet.Compression
{
/// <summary>
/// Represents the "zlib" compression algorithm.
/// </summary>
#pragma warning disable CA1724 // Type names should not match namespaces
public class Zlib : Compressor
#pragma warning restore CA1724 // Type names should not match namespaces
{
#if NET
private readonly ZLibStream _compressor;
private readonly ZLibStream _decompressor;
#else
private readonly ZOutputStream _compressor;
private readonly ZOutputStream _decompressor;
#endif
private MemoryStream _compressorStream;
private MemoryStream _decompressorStream;
private bool _isDisposed;
/// <summary>
/// Initializes a new instance of the <see cref="Zlib"/> class.
/// </summary>
public Zlib()
: this(delayedCompression: false)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Zlib"/> class.
/// </summary>
/// <param name="delayedCompression">
/// <inheritdoc cref="Compressor(bool)" path="/param[@name='delayedCompression']"/>
/// </param>
protected Zlib(bool delayedCompression)
: base(delayedCompression)
{
_compressorStream = new MemoryStream();
_decompressorStream = new MemoryStream();
#if NET
_compressor = new ZLibStream(_compressorStream, CompressionMode.Compress);
_decompressor = new ZLibStream(_decompressorStream, CompressionMode.Decompress);
#else
_compressor = new ZOutputStream(_compressorStream, level: JZlib.Z_DEFAULT_COMPRESSION) { FlushMode = JZlib.Z_PARTIAL_FLUSH };
_decompressor = new ZOutputStream(_decompressorStream) { FlushMode = JZlib.Z_PARTIAL_FLUSH };
#endif
}
/// <inheritdoc/>
public override string Name
{
get { return "zlib"; }
}
/// <inheritdoc/>
protected override byte[] CompressCore(byte[] data, int offset, int length)
{
_compressorStream.SetLength(0);
_compressor.Write(data, offset, length);
_compressor.Flush();
return _compressorStream.ToArray();
}
/// <inheritdoc/>
protected override byte[] DecompressCore(byte[] data, int offset, int length)
{
#if NET
_decompressorStream.Write(data, offset, length);
_decompressorStream.Position = 0;
using var outputStream = new MemoryStream();
_decompressor.CopyTo(outputStream);
_decompressorStream.SetLength(0);
return outputStream.ToArray();
#else
_decompressorStream.SetLength(0);
_decompressor.Write(data, offset, length);
_decompressor.Flush();
return _decompressorStream.ToArray();
#endif
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.</param>
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (_isDisposed)
{
return;
}
if (disposing)
{
var compressorStream = _compressorStream;
if (compressorStream != null)
{
compressorStream.Dispose();
_compressorStream = null;
}
var decompressorStream = _decompressorStream;
if (decompressorStream != null)
{
decompressorStream.Dispose();
_decompressorStream = null;
}
_isDisposed = true;
}
}
}
}