Skip to content

Commit

Permalink
Append leftover data to input, not output
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Mar 11, 2021
1 parent d9f3585 commit 9b4c615
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions core/src/main/java/org/jruby/ext/zlib/JZlibInflate.java
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,10 @@ private void run(boolean finish) {
case com.jcraft.jzlib.JZlib.Z_STREAM_END:
if (flater.avail_in > 0) {
// MRI behavior: pass-through
input.append(flater.next_in,
flater.next_in_index, flater.avail_in);
byte[] next_in = flater.next_in;
int next_in_index = flater.next_in_index;
int avail_in = flater.avail_in;
appendInput(next_in, next_in_index, avail_in);
flater.setInput("".getBytes());
}
case com.jcraft.jzlib.JZlib.Z_OK:
Expand All @@ -280,6 +282,10 @@ private void run(boolean finish) {
}
}

private void appendInput(byte[] src, int index, int len) {
input.append(src, index, len);
}

@Override
protected int internalTotalIn() {
return (int) flater.total_in;
Expand Down Expand Up @@ -315,15 +321,14 @@ protected IRubyObject internalFinish(Block block) {
run(true);
// MRI behavior: in finished mode, we work as pass-through
if (internalFinished()) {
if (input.getRealSize() > 0) {
if (collected.length - collectedIdx < input.length()) {
byte[] tmp = new byte[collected.length + input.length()];
resetBuffer(input);
if (flater.avail_in > 0) {
if (collected.length - collectedIdx < flater.avail_in) {
byte[] tmp = new byte[collected.length + flater.avail_in];
System.arraycopy(collected, 0, tmp, 0, collectedIdx);
collected = tmp;
}
System.arraycopy(input.getUnsafeBytes(), input.begin(), collected, collectedIdx, input.length());
collectedIdx += input.length();
resetBuffer(input);
appendInput(flater.next_in, flater.next_in_index, flater.avail_in);
}
}
return flushOutput(getRuntime().getCurrentContext(), block);
Expand Down

0 comments on commit 9b4c615

Please sign in to comment.