From 2bbef677700142fcd68e9d2f8846976d5ac95a72 Mon Sep 17 00:00:00 2001 From: Adrien Grand Date: Mon, 11 Mar 2019 07:58:01 +0100 Subject: [PATCH] Propagate exceptions in o.e.common.io.Streams. (#39042) (#39848) This commit propagates some exceptions that were previously swallowed and also makes sure that exceptions closing streams are either propagated if the try block succeeded or added as suppressed exceptions otherwise. --- .../org/elasticsearch/common/io/Streams.java | 87 ++++++++----------- 1 file changed, 35 insertions(+), 52 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/common/io/Streams.java b/server/src/main/java/org/elasticsearch/common/io/Streams.java index cc4542c8390..46a6956914f 100644 --- a/server/src/main/java/org/elasticsearch/common/io/Streams.java +++ b/server/src/main/java/org/elasticsearch/common/io/Streams.java @@ -19,7 +19,6 @@ package org.elasticsearch.common.io; -import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.BytesStream; import org.elasticsearch.common.io.stream.StreamOutput; @@ -72,26 +71,24 @@ public abstract class Streams { public static long copy(InputStream in, OutputStream out, byte[] buffer) throws IOException { Objects.requireNonNull(in, "No InputStream specified"); Objects.requireNonNull(out, "No OutputStream specified"); - boolean success = false; - try { - long byteCount = 0; - int bytesRead; - while ((bytesRead = in.read(buffer)) != -1) { - out.write(buffer, 0, bytesRead); - byteCount += bytesRead; - } - out.flush(); - success = true; - return byteCount; - } finally { - if (success) { - IOUtils.close(in, out); - } else { - IOUtils.closeWhileHandlingException(in, out); - } + // Leverage try-with-resources to close in and out so that exceptions in close() are either propagated or added as suppressed + // exceptions to the main exception + try (InputStream in2 = in; OutputStream out2 = out) { + return doCopy(in2, out2, buffer); } } + private static long doCopy(InputStream in, OutputStream out, byte[] buffer) throws IOException { + long byteCount = 0; + int bytesRead; + while ((bytesRead = in.read(buffer)) != -1) { + out.write(buffer, 0, bytesRead); + byteCount += bytesRead; + } + out.flush(); + return byteCount; + } + /** * Copy the contents of the given byte array to the given OutputStream. * Closes the stream when done. @@ -103,14 +100,8 @@ public abstract class Streams { public static void copy(byte[] in, OutputStream out) throws IOException { Objects.requireNonNull(in, "No input byte array specified"); Objects.requireNonNull(out, "No OutputStream specified"); - try { - out.write(in); - } finally { - try { - out.close(); - } catch (IOException ex) { - // do nothing - } + try (OutputStream out2 = out) { + out2.write(in); } } @@ -131,27 +122,25 @@ public abstract class Streams { public static int copy(Reader in, Writer out) throws IOException { Objects.requireNonNull(in, "No Reader specified"); Objects.requireNonNull(out, "No Writer specified"); - boolean success = false; - try { - int byteCount = 0; - char[] buffer = new char[BUFFER_SIZE]; - int bytesRead; - while ((bytesRead = in.read(buffer)) != -1) { - out.write(buffer, 0, bytesRead); - byteCount += bytesRead; - } - out.flush(); - success = true; - return byteCount; - } finally { - if (success) { - IOUtils.close(in, out); - } else { - IOUtils.closeWhileHandlingException(in, out); - } + // Leverage try-with-resources to close in and out so that exceptions in close() are either propagated or added as suppressed + // exceptions to the main exception + try (Reader in2 = in; Writer out2 = out) { + return doCopy(in2, out2); } } + private static int doCopy(Reader in, Writer out) throws IOException { + int byteCount = 0; + char[] buffer = new char[BUFFER_SIZE]; + int bytesRead; + while ((bytesRead = in.read(buffer)) != -1) { + out.write(buffer, 0, bytesRead); + byteCount += bytesRead; + } + out.flush(); + return byteCount; + } + /** * Copy the contents of the given String to the given output Writer. * Closes the write when done. @@ -163,14 +152,8 @@ public abstract class Streams { public static void copy(String in, Writer out) throws IOException { Objects.requireNonNull(in, "No input String specified"); Objects.requireNonNull(out, "No Writer specified"); - try { - out.write(in); - } finally { - try { - out.close(); - } catch (IOException ex) { - // do nothing - } + try (Writer out2 = out) { + out2.write(in); } }