Fix Java11 Streams Class Copy (#60641)
In https://github.com/elastic/elasticsearch/pull/60608 we missed aligning the Java11 version of this class and this is breaking BwC tests now. This commit aligns Java11 and Java8 versions of it again.
This commit is contained in:
parent
20ae1b75bd
commit
222665a48e
|
@ -32,26 +32,57 @@ import java.io.OutputStream;
|
|||
*/
|
||||
public abstract class Streams {
|
||||
|
||||
private static final ThreadLocal<byte[]> buffer = ThreadLocal.withInitial(() -> new byte[8 * 1024]);
|
||||
|
||||
/**
|
||||
* Copy the contents of the given InputStream to the given OutputStream.
|
||||
* Closes both streams when done.
|
||||
* Copy the contents of the given InputStream to the given OutputStream. Optionally, closes both streams when done.
|
||||
*
|
||||
* @param in the stream to copy from
|
||||
* @param out the stream to copy to
|
||||
* @param in the stream to copy from
|
||||
* @param out the stream to copy to
|
||||
* @param close whether to close both streams after copying
|
||||
* @param buffer buffer to use for copying
|
||||
* @return the number of bytes copied
|
||||
* @throws IOException in case of I/O errors
|
||||
*/
|
||||
public static long copy(final InputStream in, final OutputStream out) throws IOException {
|
||||
public static long copy(final InputStream in, final OutputStream out, byte[] buffer, boolean close) throws IOException {
|
||||
Exception err = null;
|
||||
try {
|
||||
final long byteCount = in.transferTo(out);
|
||||
long byteCount = 0;
|
||||
int bytesRead;
|
||||
while ((bytesRead = in.read(buffer)) != -1) {
|
||||
out.write(buffer, 0, bytesRead);
|
||||
byteCount += bytesRead;
|
||||
}
|
||||
out.flush();
|
||||
return byteCount;
|
||||
} catch (IOException | RuntimeException e) {
|
||||
err = e;
|
||||
throw e;
|
||||
} finally {
|
||||
IOUtils.close(err, in, out);
|
||||
if (close) {
|
||||
IOUtils.close(err, in, out);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @see #copy(InputStream, OutputStream, byte[], boolean)
|
||||
*/
|
||||
public static long copy(final InputStream in, final OutputStream out, boolean close) throws IOException {
|
||||
return copy(in, out, buffer.get(), close);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see #copy(InputStream, OutputStream, byte[], boolean)
|
||||
*/
|
||||
public static long copy(final InputStream in, final OutputStream out, byte[] buffer) throws IOException {
|
||||
return copy(in, out, buffer, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see #copy(InputStream, OutputStream, byte[], boolean)
|
||||
*/
|
||||
public static long copy(final InputStream in, final OutputStream out) throws IOException {
|
||||
return copy(in, out, buffer.get(), true);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue