forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBZ2Module.cs
More file actions
117 lines (101 loc) · 4.29 KB
/
BZ2Module.cs
File metadata and controls
117 lines (101 loc) · 4.29 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
/* **************************************************************************
*
* Copyright 2012 Jeff Hardy
*
* Licensed 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
*
* http://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.Linq;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using Ionic.BZip2;
using IronPython.Runtime;
using Microsoft.Scripting.Runtime;
using IronPython.Runtime.Operations;
using Microsoft.Scripting.Utils;
[assembly: PythonModule("bz2", typeof(IronPython.Modules.Bz2.Bz2Module))]
namespace IronPython.Modules.Bz2 {
public static partial class Bz2Module {
public const string __doc__ =
@"The python bz2 module provides a comprehensive interface for
the bz2 compression library. It implements a complete file
interface, one shot (de)compression functions, and types for
sequential (de)compression.";
internal const int DEFAULT_COMPRESSLEVEL = 9;
private const int PARALLEL_THRESHOLD = 10 * 1024 * 1024; // 10 MiB recommended by ParallelBZip2OutputStream devs
[Documentation(@"compress(data [, compresslevel=9]) -> string
Compress data in one shot. If you want to compress data sequentially,
use an instance of BZ2Compressor instead. The compresslevel parameter, if
given, must be a number between 1 and 9.
")]
public static Bytes compress([BytesConversion]IList<byte> data,
[DefaultParameterValue(DEFAULT_COMPRESSLEVEL)]int compresslevel) {
using (var mem = new MemoryStream()) {
using (var bz2 = data.Count > PARALLEL_THRESHOLD ?
(Stream)new ParallelBZip2OutputStream(mem, true) :
(Stream)new BZip2OutputStream(mem, true)) {
var buffer = data.ToArrayNoCopy();
bz2.Write(buffer, 0, data.Count);
}
return Bytes.Make(mem.ToArray());
}
}
[Documentation(@"decompress(data) -> decompressed data
Decompress data in one shot. If you want to decompress data sequentially,
use an instance of BZ2Decompressor instead.
")]
public static Bytes decompress([BytesConversion]IList<byte> data) {
if (data.Count == 0) {
return new Bytes();
}
byte[] buffer = new byte[1024];
using (var output = new MemoryStream()) {
using (var input = new MemoryStream(data.ToArrayNoCopy(), false)) {
using (var bz2 = new BZip2InputStream(input)) {
int read = 0;
while(true) {
try {
read = bz2.Read(buffer, 0, buffer.Length);
} catch (IOException e) {
throw PythonOps.ValueError(e.Message);
}
if (read > 0) {
output.Write(buffer, 0, read);
} else {
break;
}
}
}
}
return Bytes.Make(output.ToArray());
}
}
/// <summary>
/// Try to convert IList(Of byte) to byte[] without copying, if possible.
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
private static byte[] ToArrayNoCopy(this IList<byte> bytes) {
byte[] bytesA = bytes as byte[];
if (bytesA != null) {
return bytesA;
}
Bytes bytesP = bytes as Bytes;
if (bytesP != null) {
return bytesP.GetUnsafeByteArray();
}
return bytes.ToArray();
}
}
}