Skip to content

Commit

Permalink
added a debug probe for diagnosing "Not in GZIP format" problem.
Browse files Browse the repository at this point in the history
  • Loading branch information
kohsuke committed Nov 5, 2008
1 parent 3ed4f3a commit 694ed60
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
14 changes: 12 additions & 2 deletions core/src/main/java/hudson/FilePath.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import hudson.util.FormFieldValidator;
import hudson.util.IOException2;
import hudson.util.StreamResource;
import hudson.util.HeadBufferingStream;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
Expand Down Expand Up @@ -990,10 +991,17 @@ private Integer writeToTar(File baseDir, String fileMask, String excludes, Pipe
/**
* Reads from a tar stream and stores obtained files to the base dir.
*/
private static void readFromTar(String name, File baseDir, InputStream in) throws IOException {
private static void readFromTar(String name, File baseDir, InputStream _in) throws IOException {
Untar untar = new Untar();
untar.setProject(new Project());
untar.add(new StreamResource(name,new BufferedInputStream(new GZIPInputStream(in))));
HeadBufferingStream in = new HeadBufferingStream(_in,SIDE_BUFFER_SIZE);
try {
untar.add(new StreamResource(name,new BufferedInputStream(new GZIPInputStream(in))));
} catch (IOException e) {
// various people reported "java.io.IOException: Not in GZIP format" here, so diagnose this problem better
in.fillSide();
throw new IOException2(e.getMessage()+"\nstream="+Util.toHexString(in.getSideBuffer()),e);
}
untar.setDest(baseDir);
try {
untar.execute();
Expand Down Expand Up @@ -1187,6 +1195,8 @@ private void readObject(ObjectInputStream ois) throws IOException, ClassNotFound

private static final long serialVersionUID = 1L;

public static int SIDE_BUFFER_SIZE = 1024;

/**
* Adapts {@link FileCallable} to {@link Callable}.
*/
Expand Down
69 changes: 69 additions & 0 deletions core/src/main/java/hudson/util/HeadBufferingStream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package hudson.util;

import org.apache.commons.io.output.ByteArrayOutputStream;

import java.io.FilterInputStream;
import java.io.InputStream;
import java.io.IOException;

/**
* {@link FilterInputStream} that buffers the first N bytes to a byte array on the side.
* This byte array can be then accessed later.
*
* <p>
* Useful for sniffing the content of the stream after the error is discovered.
*
* @author Kohsuke Kawaguchi
*/
public class HeadBufferingStream extends FilterInputStream {
private final ByteArrayOutputStream side;
private final int sideBufferSize;

public HeadBufferingStream(InputStream in, int sideBufferSize) {
super(in);
this.sideBufferSize = sideBufferSize;
this.side = new ByteArrayOutputStream(sideBufferSize);
}

public int read() throws IOException {
int i = in.read();
if(i>=0 && space()>0)
side.write(i);
return i;
}

public int read(byte b[], int off, int len) throws IOException {
int r = in.read(b, off, len);
if(r>0) {
int sp = space();
if(sp>0)
side.write(b,off,Math.min(r, sp));
}
return r;
}

/**
* Available space in the {@link #side} buffer.
*/
private int space() {
return sideBufferSize-side.size();
}

/**
* Read until we fill up the side buffer.
*/
public void fillSide() throws IOException {
byte[] buf = new byte[space()];
while(space()>0) {
if(read(buf)<0)
return;
}
}

/**
* Gets the side buffer content.
*/
public byte[] getSideBuffer() {
return side.toByteArray();
}
}

0 comments on commit 694ed60

Please sign in to comment.