-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Move Streams.copy into elasticsearch-core and make a multi-release jar #29322
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
969a053
d368952
7753e01
a703f16
8d188af
5242f9e
85fe2fe
87e4485
81a4020
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.core.internal.io; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Simple utility methods for file and stream copying. | ||
* All copy methods use a block size of 4096 bytes, | ||
* and close all affected streams when done. | ||
* <p> | ||
* Mainly for use within the framework, | ||
* but also useful for application code. | ||
*/ | ||
public class Streams { | ||
|
||
/** | ||
* Copy the contents of the given InputStream to the given OutputStream. | ||
* Closes both streams when done. | ||
* | ||
* @param in the stream to copy from | ||
* @param out the stream to copy to | ||
* @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 { | ||
Objects.requireNonNull(in, "No InputStream specified"); | ||
Objects.requireNonNull(out, "No OutputStream specified"); | ||
final byte[] buffer = new byte[8192]; | ||
Exception err = null; | ||
try { | ||
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); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.core.internal.io; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
|
||
/** | ||
* Simple utility methods for file and stream copying. | ||
* All copy methods close all affected streams when done. | ||
* <p> | ||
* Mainly for use within the framework, | ||
* but also useful for application code. | ||
*/ | ||
public abstract class Streams { | ||
|
||
/** | ||
* Copy the contents of the given InputStream to the given OutputStream. | ||
* Closes both streams when done. | ||
* | ||
* @param in the stream to copy from | ||
* @param out the stream to copy to | ||
* @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 { | ||
Exception err = null; | ||
try { | ||
final long byteCount = in.transferTo(out); | ||
out.flush(); | ||
return byteCount; | ||
} catch (IOException | RuntimeException e) { | ||
err = e; | ||
throw e; | ||
} finally { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that you can simplify all of this gross exception handling with: try (in; out) {
final long byteCount = in.transferTo(out);
out.flush();
return byteCount;
} Note that this also uses a Java 9 language-level feature (a resource reference in the try-with-resources statement). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See item 2 in JEP 213 for an explanation of this feature. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is also possible that try (in;
out) {
} is clearer. I am not sure. |
||
IOUtils.close(err, in, out); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.core.internal.io; | ||
|
||
import org.elasticsearch.test.ESTestCase; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Arrays; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
public class StreamsTests extends ESTestCase { | ||
public void testCopyFromInputStream() throws IOException { | ||
byte[] content = "content".getBytes(StandardCharsets.UTF_8); | ||
ByteArrayInputStream in = new ByteArrayInputStream(content); | ||
ByteArrayOutputStream out = new ByteArrayOutputStream(content.length); | ||
long count = Streams.copy(in, out); | ||
|
||
assertThat(count, equalTo((long) content.length)); | ||
assertThat(Arrays.equals(content, out.toByteArray()), equalTo(true)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you make
ex
final please?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Certainly