4747
4848import java.io.ByteArrayInputStream;
4949import java.io.ByteArrayOutputStream;
50+ import java.io.EOFException;
5051import java.io.IOException;
5152import java.io.InputStream;
5253import java.util.zip.DataFormatException;
@@ -112,7 +113,7 @@ public Inflater getInflater() {
112113 return inf;
113114 }
114115
115- public void setInput () throws IOException {
116+ public void fillInput () throws IOException {
116117 fill();
117118 }
118119 }
@@ -162,7 +163,7 @@ private static boolean isGZIPStreamReady(DecompressStream stream, byte[] data, i
162163 // GZIPInputStream will read the header during initialization
163164 stream.stream = new GZIPDecompressStream(stream.in);
164165 stream.inflater = stream.stream.getInflater();
165- stream.stream.setInput ();
166+ stream.stream.fillInput ();
166167 return true;
167168 }
168169 } catch (ZipException ze) {
@@ -178,7 +179,7 @@ private static boolean isGZIPStreamFinishing(DecompressStream stream, byte[] dat
178179 stream.in.append(data, 0, length);
179180 try {
180181 if (stream.in.length() >= HEADER_TRAILER_SIZE) {
181- stream.stream.setInput ();
182+ stream.stream.fillInput ();
182183 // this should trigger reading trailer
183184 stream.stream.read();
184185 stream.stream = null;
@@ -246,19 +247,30 @@ private byte[] createByteArray(byte[] bytes, int length, int maxLength, int bufS
246247 int maxLen = maxLength <= 0 ? Integer.MAX_VALUE : maxLength;
247248 byte[] result = new byte[Math.min(maxLen, bufSize)];
248249
249- int bytesWritten = result.length;
250250 ByteArrayOutputStream baos = new ByteArrayOutputStream();
251251 boolean zdictIsSet = false;
252- while (baos.size() < maxLen && bytesWritten == result.length) {
252+ while (baos.size() < maxLen && !stream.inflater.finished()) {
253+ if (stream.inflater.needsInput()) {
254+ if (stream.stream == null) {
255+ break;
256+ }
257+ try {
258+ stream.stream.fillInput();
259+ } catch (EOFException e) {
260+ break;
261+ } catch (IOException e) {
262+ throw CompilerDirectives.shouldNotReachHere(e);
263+ }
264+ }
265+ int bytesWritten;
253266 try {
254267 int len = Math.min(maxLen - baos.size(), result.length);
255268 bytesWritten = stream.inflater.inflate(result, 0, len);
256269 if (bytesWritten == 0 && !zdictIsSet && stream.inflater.needsDictionary()) {
257270 if (getZdict().length > 0) {
258271 setDictionary();
259272 zdictIsSet = true;
260- // we inflate again with a dictionary
261- bytesWritten = stream.inflater.inflate(result, 0, len);
273+ continue;
262274 } else {
263275 throw PRaiseNode.raiseStatic(nodeForRaise, ZLibError, WHILE_SETTING_ZDICT);
264276 }
@@ -320,7 +332,7 @@ protected static byte[] decompress(byte[] bytes, int length, int wbits, int bufs
320332 private void saveUnconsumedInput(byte[] data, int length,
321333 byte[] unusedDataBytes, int unconsumedTailLen, Node inliningTarget) {
322334 int unusedLen = getRemaining();
323- byte[] tail = PythonUtils.arrayCopyOfRange(data, length - unusedLen, length);
335+ byte[] tail = PythonUtils.arrayCopyOfRange(data, Math.max(0, length - unusedLen) , length);
324336 PythonLanguage language = PythonLanguage.get(inliningTarget);
325337 if (isEof()) {
326338 if (unconsumedTailLen > 0) {
0 commit comments