From bf2c8109abaf8808b8e37c000388b0f74c313581 Mon Sep 17 00:00:00 2001 From: Tyler Camp Date: Fri, 25 Sep 2020 17:12:57 -0400 Subject: [PATCH] Lazily open file streams to prevent blocking on reads --- .../plugins/codedx/CodeDxPublisher.java | 18 ++---- .../codedx/DeferredFilePathInputStream.java | 63 +++++++++++++++++++ 2 files changed, 67 insertions(+), 14 deletions(-) create mode 100644 src/main/java/org/jenkinsci/plugins/codedx/DeferredFilePathInputStream.java diff --git a/src/main/java/org/jenkinsci/plugins/codedx/CodeDxPublisher.java b/src/main/java/org/jenkinsci/plugins/codedx/CodeDxPublisher.java index 46db6d3..ebf377e 100644 --- a/src/main/java/org/jenkinsci/plugins/codedx/CodeDxPublisher.java +++ b/src/main/java/org/jenkinsci/plugins/codedx/CodeDxPublisher.java @@ -223,14 +223,8 @@ public void perform( if (sourceAndBinaryZip != null) { - - try { - buildOutput.println("Adding source/binary zip..."); - toSend.put("Jenkins-SourceAndBinary", sourceAndBinaryZip.read()); - } catch (IOException e) { - buildOutput.println("Failed to add source/binary zip."); - } - + buildOutput.println("Adding source/binary zip..."); + toSend.put("Jenkins-SourceAndBinary", new DeferredFilePathInputStream(sourceAndBinaryZip)); } else { buildOutput.println("No matching source/binary files."); } @@ -242,12 +236,8 @@ public void perform( FilePath path = workspace.child(file); if (path.exists()) { - try { - buildOutput.println("Add tool output file " + path.getRemote() + " to request."); - toSend.put(path.getName(), path.read()); - } catch (IOException e) { - buildOutput.println("Failed to add tool output file: " + path); - } + buildOutput.println("Add tool output file " + path.getRemote() + " to request."); + toSend.put(path.getName(), new DeferredFilePathInputStream(path)); } else { buildOutput.println("Path specified but could not be found: " + path); } diff --git a/src/main/java/org/jenkinsci/plugins/codedx/DeferredFilePathInputStream.java b/src/main/java/org/jenkinsci/plugins/codedx/DeferredFilePathInputStream.java new file mode 100644 index 0000000..15281bc --- /dev/null +++ b/src/main/java/org/jenkinsci/plugins/codedx/DeferredFilePathInputStream.java @@ -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; + } +}