Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from jenkinsci/bugfix-upload-hang
Lazily open file streams to prevent blocking on reads
- Loading branch information
Showing
2 changed files
with
67 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/main/java/org/jenkinsci/plugins/codedx/DeferredFilePathInputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.jenkinsci.plugins.codedx; | ||
|
||
import hudson.FilePath; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
public class DeferredFilePathInputStream extends InputStream { | ||
FilePath fp; | ||
InputStream is; | ||
|
||
public DeferredFilePathInputStream(FilePath fp) | ||
{ | ||
this.fp = fp; | ||
this.is = null; | ||
} | ||
|
||
private void initStream() throws IOException { | ||
if (this.is == null) { | ||
try { | ||
this.is = fp.read(); | ||
} catch (InterruptedException e) { | ||
throw new IOException("Operation was interrupted", e); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public int read(byte[] b) throws IOException { | ||
initStream(); | ||
|
||
int numRead = this.is.read(b); | ||
if (numRead < b.length) { | ||
this.is.close(); | ||
} | ||
|
||
return numRead; | ||
} | ||
|
||
@Override | ||
public int read(byte[] b, int off, int len) throws IOException { | ||
initStream(); | ||
|
||
int numRead = this.is.read(b, off, len); | ||
if (numRead < len) { | ||
this.is.close(); | ||
} | ||
|
||
return numRead; | ||
} | ||
|
||
@Override | ||
public int read() throws IOException { | ||
initStream(); | ||
|
||
int result = this.is.read(); | ||
if (result < 0) { | ||
this.is.close(); | ||
} | ||
|
||
return result; | ||
} | ||
} |