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:
parent
4da04616c9
commit
2bbef67770
|
@ -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,8 +71,14 @@ 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
|
||||||
|
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;
|
long byteCount = 0;
|
||||||
int bytesRead;
|
int bytesRead;
|
||||||
while ((bytesRead = in.read(buffer)) != -1) {
|
while ((bytesRead = in.read(buffer)) != -1) {
|
||||||
|
@ -81,15 +86,7 @@ public abstract class Streams {
|
||||||
byteCount += bytesRead;
|
byteCount += bytesRead;
|
||||||
}
|
}
|
||||||
out.flush();
|
out.flush();
|
||||||
success = true;
|
|
||||||
return byteCount;
|
return byteCount;
|
||||||
} finally {
|
|
||||||
if (success) {
|
|
||||||
IOUtils.close(in, out);
|
|
||||||
} else {
|
|
||||||
IOUtils.closeWhileHandlingException(in, out);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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,8 +122,14 @@ 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
|
||||||
|
try (Reader in2 = in; Writer out2 = out) {
|
||||||
|
return doCopy(in2, out2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int doCopy(Reader in, Writer out) throws IOException {
|
||||||
int byteCount = 0;
|
int byteCount = 0;
|
||||||
char[] buffer = new char[BUFFER_SIZE];
|
char[] buffer = new char[BUFFER_SIZE];
|
||||||
int bytesRead;
|
int bytesRead;
|
||||||
|
@ -141,15 +138,7 @@ public abstract class Streams {
|
||||||
byteCount += bytesRead;
|
byteCount += bytesRead;
|
||||||
}
|
}
|
||||||
out.flush();
|
out.flush();
|
||||||
success = true;
|
|
||||||
return byteCount;
|
return byteCount;
|
||||||
} finally {
|
|
||||||
if (success) {
|
|
||||||
IOUtils.close(in, out);
|
|
||||||
} else {
|
|
||||||
IOUtils.closeWhileHandlingException(in, out);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue