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.
This commit is contained in:
Adrien Grand 2019-03-11 07:58:01 +01:00 committed by GitHub
parent 4da04616c9
commit 2bbef67770
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -19,7 +19,6 @@
package org.elasticsearch.common.io; package org.elasticsearch.common.io;
import org.elasticsearch.core.internal.io.IOUtils;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.BytesStream; import org.elasticsearch.common.io.stream.BytesStream;
import org.elasticsearch.common.io.stream.StreamOutput; 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 { public static long copy(InputStream in, OutputStream out, byte[] buffer) throws IOException {
Objects.requireNonNull(in, "No InputStream specified"); Objects.requireNonNull(in, "No InputStream specified");
Objects.requireNonNull(out, "No OutputStream specified"); Objects.requireNonNull(out, "No OutputStream specified");
boolean success = false; // Leverage try-with-resources to close in and out so that exceptions in close() are either propagated or added as suppressed
try { // exceptions to the main exception
long byteCount = 0; try (InputStream in2 = in; OutputStream out2 = out) {
int bytesRead; return doCopy(in2, out2, buffer);
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);
}
} }
} }
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. * Copy the contents of the given byte array to the given OutputStream.
* Closes the stream when done. * Closes the stream when done.
@ -103,14 +100,8 @@ public abstract class Streams {
public static void copy(byte[] in, OutputStream out) throws IOException { public static void copy(byte[] in, OutputStream out) throws IOException {
Objects.requireNonNull(in, "No input byte array specified"); Objects.requireNonNull(in, "No input byte array specified");
Objects.requireNonNull(out, "No OutputStream specified"); Objects.requireNonNull(out, "No OutputStream specified");
try { try (OutputStream out2 = out) {
out.write(in); out2.write(in);
} finally {
try {
out.close();
} catch (IOException ex) {
// do nothing
}
} }
} }
@ -131,27 +122,25 @@ public abstract class Streams {
public static int copy(Reader in, Writer out) throws IOException { public static int copy(Reader in, Writer out) throws IOException {
Objects.requireNonNull(in, "No Reader specified"); Objects.requireNonNull(in, "No Reader specified");
Objects.requireNonNull(out, "No Writer specified"); Objects.requireNonNull(out, "No Writer specified");
boolean success = false; // Leverage try-with-resources to close in and out so that exceptions in close() are either propagated or added as suppressed
try { // exceptions to the main exception
int byteCount = 0; try (Reader in2 = in; Writer out2 = out) {
char[] buffer = new char[BUFFER_SIZE]; return doCopy(in2, out2);
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);
}
} }
} }
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. * Copy the contents of the given String to the given output Writer.
* Closes the write when done. * Closes the write when done.
@ -163,14 +152,8 @@ public abstract class Streams {
public static void copy(String in, Writer out) throws IOException { public static void copy(String in, Writer out) throws IOException {
Objects.requireNonNull(in, "No input String specified"); Objects.requireNonNull(in, "No input String specified");
Objects.requireNonNull(out, "No Writer specified"); Objects.requireNonNull(out, "No Writer specified");
try { try (Writer out2 = out) {
out.write(in); out2.write(in);
} finally {
try {
out.close();
} catch (IOException ex) {
// do nothing
}
} }
} }