Skip to content

Commit

Permalink
[FIXED JENKINS-23471]
Browse files Browse the repository at this point in the history
if readUntilBreak hits stream EOF, it would end up hanging in a busy loop. EOF is a break boundary,
so it should cause the method to return gracefully.
  • Loading branch information
kohsuke committed Sep 5, 2014
1 parent 081bcd9 commit 83da718
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/main/java/hudson/remoting/ChunkedInputStream.java
Expand Up @@ -61,6 +61,11 @@ private boolean nextPayload() throws IOException {
return false;
}

/**
*
* @return
* true if the underlying stream hits EOF
*/
private boolean readHeader() throws IOException {
if (remaining>0) return false;

Expand All @@ -81,16 +86,22 @@ private boolean readHeader() throws IOException {
protected void onBreak() {
}

/**
* Reads bytes until we hit the chunk boundary. Bytes read will be written to the sink.
*/
public void readUntilBreak(OutputStream sink) throws IOException {
byte[] buf = new byte[4096];
while (true) {
if (remaining>0) {
// more bytes to read in the current chunk
int read = read(buf, 0, Math.min(remaining,buf.length));
if (read==-1)
throw new IOException("Unexpected EOF");
sink.write(buf,0,read);
} else {
readHeader(); // move on to the next chunk
// move on to the next chunk
if (readHeader())
return; // stream has EOFed. No more bytes to read.
}
if (isLast && remaining==0)
return; // we've read the all payload of the last chunk
Expand Down

0 comments on commit 83da718

Please sign in to comment.