-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Streams.copy into elasticsearch-core and make a multi-release jar (
#29322) * Move Streams.copy into elasticsearch-core and make a multi-release jar This moves the method `Streams.copy(InputStream in, OutputStream out)` into the `elasticsearch-core` project (inside the `o.e.core.internal.io` package). It also makes this class into a multi-release class where the Java 9 equivalent uses `InputStream#transferTo`. This is a followup from #29300 (comment)
- Loading branch information
Showing
17 changed files
with
278 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
libs/elasticsearch-core/src/main/java/org/elasticsearch/core/internal/io/Streams.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
libs/elasticsearch-core/src/main/java9/org/elasticsearch/core/internal/io/Streams.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
IOUtils.close(err, in, out); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
libs/elasticsearch-core/src/test/java/org/elasticsearch/core/internal/io/StreamsTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.