Skip to content
Permalink
Browse files
[FIXED JENKINS-23471]
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 83da718ab06b2f881aa08434684a02ea95ff5135
Showing with 12 additions and 1 deletion.
  1. +12 −1 src/main/java/hudson/remoting/ChunkedInputStream.java
@@ -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;

@@ -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

0 comments on commit 83da718

Please sign in to comment.