-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathbz2.java
More file actions
78 lines (65 loc) · 2.43 KB
/
Copy pathbz2.java
File metadata and controls
78 lines (65 loc) · 2.43 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
package org.python.modules.bz2;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
import org.python.core.ClassDictInit;
import org.python.core.Py;
import org.python.core.PyObject;
import org.python.core.PyBytes;
import org.python.expose.ExposedFunction;
import org.python.expose.ExposedModule;
import org.python.expose.ModuleInit;
/**
* Python _bz2 module
*/
@ExposedModule
public class bz2 {
@ModuleInit
public static void classDictInit(PyObject dict) {
dict.__setitem__("BZ2File", PyBZ2File.TYPE);
dict.__setitem__("BZ2Compressor", PyBZ2Compressor.TYPE);
dict.__setitem__("BZ2Decompressor", PyBZ2Decompressor.TYPE);
}
@ExposedFunction
public static PyObject compress(PyObject[] args, String[] keywords) {
PyBytes returnData = null;
try {
ByteArrayOutputStream compressedArray = new ByteArrayOutputStream();
BZip2CompressorOutputStream bzbuf = new BZip2CompressorOutputStream(
compressedArray);
bzbuf.write(Py.unwrapBuffer(args[0]));
bzbuf.finish();
bzbuf.close();
returnData = new PyBytes(compressedArray.toString("iso-8859-1"));
compressedArray.close();
} catch (IOException e) {
throw Py.IOError(e.getMessage());
}
return returnData;
}
@ExposedFunction
public static PyObject decompress(PyObject data) {
if (data.toString().equals("")) {
return Py.EmptyByte;
}
try {
ByteArrayInputStream inputArray = new ByteArrayInputStream(Py.unwrapBuffer(data));
BZip2CompressorInputStream bzbuf = new BZip2CompressorInputStream(
inputArray);
ByteArrayOutputStream outputArray = new ByteArrayOutputStream();
final byte[] buffer = new byte[8192];
int n = 0;
while ((n = bzbuf.read(buffer)) != -1) {
outputArray.write(buffer, 0, n);
}
outputArray.close();
bzbuf.close();
inputArray.close();
return new PyBytes(outputArray.toString("iso-8859-1"));
} catch (IOException e) {
throw Py.ValueError(e.getMessage());
}
}
}