-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathDeflateCodec.cs
More file actions
93 lines (85 loc) · 3.2 KB
/
Copy pathDeflateCodec.cs
File metadata and controls
93 lines (85 loc) · 3.2 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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System.IO;
using System.IO.Compression;
namespace Avro.File
{
/// <summary>
/// Implements deflate compression and decompression.
/// </summary>
/// <seealso cref="Codec" />
/// <seealso cref="DeflateStream" />
public class DeflateCodec : Codec
{
/// <inheritdoc/>
public override byte[] Compress(byte[] uncompressedData)
{
MemoryStream outStream = new MemoryStream();
using (DeflateStream Compress =
new DeflateStream(outStream,
CompressionMode.Compress))
{
Compress.Write(uncompressedData, 0, uncompressedData.Length);
}
return outStream.ToArray();
}
/// <inheritdoc/>
public override void Compress(MemoryStream inputStream, MemoryStream outputStream)
{
outputStream.SetLength(0);
using (DeflateStream Compress =
new DeflateStream(outputStream,
CompressionMode.Compress,
true))
{
Compress.Write(inputStream.GetBuffer(), 0, (int)inputStream.Length);
}
}
/// <inheritdoc/>
public override byte[] Decompress(byte[] compressedData, int length)
{
using (MemoryStream inStream = new MemoryStream(compressedData, 0, length))
using (MemoryStream outStream = new MemoryStream())
{
using (DeflateStream decompress = new DeflateStream(inStream, CompressionMode.Decompress))
{
// Bound the decompressed size to guard against a block with a very
// high compression ratio expanding to far more memory than its
// compressed size.
CopyBounded(decompress, outStream, GetMaxDecompressLength());
}
return outStream.ToArray();
}
}
/// <inheritdoc/>
public override string GetName()
{
return DataFileConstants.DeflateCodec;
}
/// <inheritdoc/>
public override bool Equals(object other)
{
return this == other || GetType().Name == other.GetType().Name;
}
/// <inheritdoc/>
public override int GetHashCode()
{
return DataFileConstants.DeflateCodecHash;
}
}
}