|
41 | 41 |
|
42 | 42 | package com.oracle.graal.python.builtins.modules;
|
43 | 43 |
|
| 44 | +import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError; |
| 45 | +import static com.oracle.graal.python.runtime.exception.PythonErrorType.ZLibError; |
| 46 | + |
| 47 | +import java.io.ByteArrayOutputStream; |
| 48 | +import java.util.List; |
| 49 | +import java.util.zip.Adler32; |
| 50 | +import java.util.zip.CRC32; |
| 51 | +import java.util.zip.DataFormatException; |
| 52 | +import java.util.zip.Deflater; |
| 53 | +import java.util.zip.Inflater; |
| 54 | + |
44 | 55 | import com.oracle.graal.python.builtins.Builtin;
|
45 | 56 | import com.oracle.graal.python.builtins.CoreFunctions;
|
46 | 57 | import com.oracle.graal.python.builtins.PythonBuiltinClassType;
|
|
56 | 67 | import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
|
57 | 68 | import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
|
58 | 69 | import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode;
|
59 |
| -import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode; |
60 | 70 | import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
|
61 | 71 | import com.oracle.graal.python.nodes.util.CastToIntegerFromIntNode;
|
62 | 72 | import com.oracle.graal.python.runtime.PythonCore;
|
63 |
| -import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError; |
64 |
| -import static com.oracle.graal.python.runtime.exception.PythonErrorType.ZLibError; |
65 | 73 | import com.oracle.truffle.api.CompilerDirectives;
|
66 | 74 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
|
67 | 75 | import com.oracle.truffle.api.dsl.Cached;
|
|
73 | 81 | import com.oracle.truffle.api.interop.ForeignAccess;
|
74 | 82 | import com.oracle.truffle.api.interop.TruffleObject;
|
75 | 83 | import com.oracle.truffle.api.profiles.ConditionProfile;
|
76 |
| -import java.io.ByteArrayOutputStream; |
77 |
| -import java.util.Arrays; |
78 |
| -import java.util.List; |
79 |
| -import java.util.zip.Adler32; |
80 |
| -import java.util.zip.CRC32; |
81 |
| -import java.util.zip.DataFormatException; |
82 |
| -import java.util.zip.Deflater; |
83 |
| -import java.util.zip.Inflater; |
84 | 84 |
|
85 | 85 | @CoreFunctions(defineModule = ZLibModuleBuiltins.MODULE_NAME)
|
86 | 86 | public class ZLibModuleBuiltins extends PythonBuiltins {
|
@@ -386,10 +386,20 @@ abstract static class DeflateInitNode extends PythonBuiltinNode {
|
386 | 386 | @Specialization
|
387 | 387 | @TruffleBoundary
|
388 | 388 | Object deflateInit(int level, int method, int wbits, int memLevel, int strategy, Object zdict) {
|
| 389 | + Deflater deflater; |
| 390 | + if (wbits < 0) { |
| 391 | + deflater = new Deflater(level, true); |
| 392 | + // generate a RAW stream |
| 393 | + } else if (wbits >= 25) { |
| 394 | + // include gzip container |
| 395 | + throw raise(PythonBuiltinClassType.NotImplementedError, "gzip containers"); |
| 396 | + } else { |
| 397 | + deflater = new Deflater(level, true); |
| 398 | + } |
| 399 | + |
389 | 400 | if (method != DEFLATED) {
|
390 | 401 | throw raise(PythonBuiltinClassType.ValueError, "only DEFLATED (%d) allowed as method, got %d", DEFLATED, method);
|
391 | 402 | }
|
392 |
| - Deflater deflater = new Deflater(level); |
393 | 403 | deflater.setStrategy(strategy);
|
394 | 404 | if (zdict instanceof String) {
|
395 | 405 | deflater.setDictionary(((String) zdict).getBytes());
|
@@ -420,26 +430,28 @@ abstract static class DeflateCompress extends PythonTernaryBuiltinNode {
|
420 | 430 | @Specialization
|
421 | 431 | @TruffleBoundary
|
422 | 432 | Object deflateCompress(DeflaterWrapper stream, PIBytesLike pb, int mode) {
|
| 433 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
423 | 434 | byte[] data = toBytes.execute(pb);
|
| 435 | + byte[] result = new byte[DEF_BUF_SIZE]; |
| 436 | + |
424 | 437 | stream.deflater.setInput(data);
|
425 |
| - byte[] result = new byte[data.length]; |
426 |
| - int bytesWritten = stream.deflater.deflate(result, 0, result.length, mode); |
427 |
| - while (bytesWritten > 0 && bytesWritten >= result.length) { |
428 |
| - result = Arrays.copyOf(result, bytesWritten * 2); |
429 |
| - bytesWritten += stream.deflater.deflate(result, bytesWritten, result.length - bytesWritten, mode); |
| 438 | + int deflateMode = mode; |
| 439 | + if (mode == Z_FINISH) { |
| 440 | + deflateMode = Z_SYNC_FLUSH; |
| 441 | + stream.deflater.finish(); |
430 | 442 | }
|
431 |
| - return factory().createBytes(result); |
432 |
| - } |
433 |
| - } |
434 | 443 |
|
435 |
| - @Builtin(name = "zlib_deflateEnd", fixedNumOfPositionalArgs = 1) |
436 |
| - @GenerateNodeFactory |
437 |
| - abstract static class DeflateEnd extends PythonUnaryBuiltinNode { |
438 |
| - @Specialization |
439 |
| - @TruffleBoundary |
440 |
| - PNone deflateEnd(DeflaterWrapper stream) { |
441 |
| - stream.deflater.end(); |
442 |
| - return PNone.NONE; |
| 444 | + int bytesWritten = result.length; |
| 445 | + while (bytesWritten == result.length) { |
| 446 | + bytesWritten = stream.deflater.deflate(result, 0, result.length, deflateMode); |
| 447 | + baos.write(result, 0, bytesWritten); |
| 448 | + } |
| 449 | + |
| 450 | + if (mode == Z_FINISH) { |
| 451 | + stream.deflater.end(); |
| 452 | + } |
| 453 | + |
| 454 | + return factory().createBytes(baos.toByteArray()); |
443 | 455 | }
|
444 | 456 | }
|
445 | 457 |
|
|
0 commit comments