Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore previous work on concatenated gzip streams #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/main/java/com/jcraft/jzlib/GZIPInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,25 @@ public GZIPInputStream(InputStream in,
super(in, inflater, size, close_in);
}

public int read(byte[] b, int off, int len) throws IOException {
int i = super.read(b, off, len);
if(i == -1){
if(inflater.avail_in<2){
int l = in.read(buf, 0, buf.length);
if(l>0)
inflater.setInput(buf, 0, l, true);
}
if(inflater.avail_in>2 &&
((inflater.next_in[inflater.next_in_index]&0xff) == 0x1f) &&
((inflater.next_in[inflater.next_in_index+1]&0xff) == (0x8b&0xff))){
this.inflater.reset();
this.eof=false;
return super.read(b, off, len);
}
}
return i;
}

/**
* @deprecated use getModifiedTime()
* @return long modified time.
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/jcraft/jzlib/Inflater.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ final public class Inflater extends ZStream{
static final private int Z_BUF_ERROR=-5;
static final private int Z_VERSION_ERROR=-6;

private int param_w = -1;
private JZlib.WrapperType param_wrapperType = null;
private boolean param_nowrap = false;

public Inflater() {
super();
init();
Expand All @@ -68,6 +72,8 @@ public Inflater(JZlib.WrapperType wrapperType) throws GZIPException {

public Inflater(int w, JZlib.WrapperType wrapperType) throws GZIPException {
super();
param_w = w;
param_wrapperType = wrapperType;
int ret = init(w, wrapperType);
if(ret!=Z_OK)
throw new GZIPException(ret+": "+msg);
Expand All @@ -83,11 +89,23 @@ public Inflater(boolean nowrap) throws GZIPException {

public Inflater(int w, boolean nowrap) throws GZIPException {
super();
param_w = w;
param_nowrap = nowrap;
int ret = init(w, nowrap);
if(ret!=Z_OK)
throw new GZIPException(ret+": "+msg);
}

void reset() {
finished = false;
if(param_wrapperType != null){
init(param_w, param_wrapperType);
}
else {
init(param_w, param_nowrap);
}
}

private boolean finished = false;

public int init(){
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/jcraft/jzlib/InflaterInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class InflaterInputStream extends FilterInputStream {

private boolean closed = false;

private boolean eof = false;
protected boolean eof = false;

private boolean close_in = true;

Expand Down
23 changes: 23 additions & 0 deletions src/test/scala/GZIPIOStreamTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,27 @@ class GZIPIOStreamTest extends FlatSpec with BeforeAndAfter with ShouldMatchers

csIn.getValue() should equal(csOut.getValue)
}

behavior of "GZIPInputStream"

it can "inflate a concatenated gzip stream." in {
// echo -n "a" | gzip > data
// echo -n "b" | gzip >> data
// echo -n "c" | gzip >> data
val data = {
val baos1 = new ByteArrayOutputStream
List("a", "b", "c").map{s =>
val gos = new GZIPOutputStream(baos1)
gos.write(s.getBytes);
gos.close
}
baos1.toByteArray
}

val gis = new GZIPInputStream(new ByteArrayInputStream(data))
val baos = new ByteArrayOutputStream()
gis -> baos

baos.toByteArray should equal("abc".getBytes)
}
}