Skip to content

Commit 5242f9e

Browse files
committed
Allow IOUtils.close() to rethrow exception if passed in
1 parent 8d188af commit 5242f9e

File tree

3 files changed

+45
-56
lines changed

3 files changed

+45
-56
lines changed

libs/elasticsearch-core/src/main/java/org/elasticsearch/core/internal/io/IOUtils.java

+33-6
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,52 @@ private IOUtils() {
4141
}
4242

4343
/**
44-
* Closes all given <tt>Closeable</tt>s. Some of the <tt>Closeable</tt>s may be null; they are ignored. After everything is closed, the
45-
* method either throws the first exception it hit while closing, or completes normally if there were no exceptions.
44+
* Closes all given <tt>Closeable</tt>s. Some of the <tt>Closeable</tt>s may be null; they are
45+
* ignored. After everything is closed, the method either throws the first exception it hit
46+
* while closing with other exceptions added as suppressed, or completes normally if there were
47+
* no exceptions.
4648
*
4749
* @param objects objects to close
4850
*/
4951
public static void close(final Closeable... objects) throws IOException {
50-
close(Arrays.asList(objects));
52+
close(null, Arrays.asList(objects));
5153
}
5254

5355
/**
54-
* Closes all given {@link Closeable}s.
56+
* Closes all given <tt>Closeable</tt>s. Some of the <tt>Closeable</tt>s may be null; they are
57+
* ignored. After everything is closed, the method adds any exceptions as suppressed to the
58+
* original exception, or throws the first exception it hit if {@code Exception} is null. If
59+
* no exceptions are encountered and the passed in exception is null, it completes normally.
5560
*
5661
* @param objects objects to close
62+
*/
63+
public static void close(final Exception e, final Closeable... objects) throws IOException {
64+
close(e, Arrays.asList(objects));
65+
}
66+
67+
/**
68+
* Closes all given <tt>Closeable</tt>s. Some of the <tt>Closeable</tt>s may be null; they are
69+
* ignored. After everything is closed, the method either throws the first exception it hit
70+
* while closing with other exceptions added as suppressed, or completes normally if there were
71+
* no exceptions.
5772
*
58-
* @see #close(Closeable...)
73+
* @param objects objects to close
5974
*/
6075
public static void close(final Iterable<? extends Closeable> objects) throws IOException {
61-
Exception ex = null;
76+
close(null, objects);
77+
}
6278

79+
/**
80+
* Closes all given {@link Closeable}s. If a non-null exception is passed in, or closing a
81+
* stream causes an exception, throws the exception with other {@link RuntimeException} or
82+
* {@link IOException} exceptions added as suppressed.
83+
*
84+
* @param ex existing Exception to add exceptions occurring during close to
85+
* @param objects objects to close
86+
*
87+
* @see #close(Closeable...)
88+
*/
89+
public static void close(Exception ex, final Iterable<? extends Closeable> objects) throws IOException {
6390
for (final Closeable object : objects) {
6491
try {
6592
if (object != null) {

libs/elasticsearch-core/src/main/java/org/elasticsearch/core/internal/io/Streams.java

+6-8
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ public class Streams {
4343
* @return the number of bytes copied
4444
* @throws IOException in case of I/O errors
4545
*/
46-
public static long copy(InputStream in, OutputStream out) throws IOException {
46+
public static long copy(final InputStream in, final OutputStream out) throws IOException {
4747
Objects.requireNonNull(in, "No InputStream specified");
4848
Objects.requireNonNull(out, "No OutputStream specified");
4949
final byte[] buffer = new byte[8192];
50-
boolean success = false;
50+
Exception err = null;
5151
try {
5252
long byteCount = 0;
5353
int bytesRead;
@@ -56,14 +56,12 @@ public static long copy(InputStream in, OutputStream out) throws IOException {
5656
byteCount += bytesRead;
5757
}
5858
out.flush();
59-
success = true;
6059
return byteCount;
60+
} catch (IOException | RuntimeException e) {
61+
err = e;
62+
throw e;
6163
} finally {
62-
if (success) {
63-
IOUtils.close(in, out);
64-
} else {
65-
IOUtils.closeWhileHandlingException(in, out);
66-
}
64+
IOUtils.close(err, in, out);
6765
}
6866
}
6967
}

libs/elasticsearch-core/src/main/java9/org/elasticsearch/core/internal/io/Streams.java

+6-42
Original file line numberDiff line numberDiff line change
@@ -41,53 +41,17 @@ public abstract class Streams {
4141
* @return the number of bytes copied
4242
* @throws IOException in case of I/O errors
4343
*/
44-
public static long copy(InputStream in, OutputStream out) throws IOException {
45-
boolean success = false;
44+
public static long copy(final InputStream in, final OutputStream out) throws IOException {
45+
Exception err = null;
4646
try {
4747
final long byteCount = in.transferTo(out);
4848
out.flush();
49-
success = true;
5049
return byteCount;
50+
} catch (IOException | RuntimeException e) {
51+
err = e;
52+
throw e;
5153
} finally {
52-
if (success) {
53-
Exception ex = null;
54-
try {
55-
in.close();
56-
} catch (IOException | RuntimeException e) {
57-
ex = e;
58-
}
59-
60-
try {
61-
out.close();
62-
} catch (IOException | RuntimeException e) {
63-
if (ex == null) {
64-
ex = e;
65-
} else {
66-
ex.addSuppressed(e);
67-
}
68-
}
69-
70-
if (ex != null) {
71-
if (ex instanceof IOException) {
72-
throw (IOException) ex;
73-
} else {
74-
throw (RuntimeException) ex;
75-
}
76-
}
77-
} else {
78-
try {
79-
in.close();
80-
} catch (IOException | RuntimeException e) {
81-
// empty
82-
}
83-
84-
try {
85-
out.close();
86-
} catch (IOException | RuntimeException e) {
87-
// empty
88-
}
89-
}
54+
IOUtils.close(err, in, out);
9055
}
91-
9256
}
9357
}

0 commit comments

Comments
 (0)