diff --git a/CHANGELOG.md b/CHANGELOG.md index bdd0c04f..fcff662f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # Changelog ## [Unreleased] +### Added +- `Utils.copyFiles` static method to use in examples, by @HardNorth ## [5.2.4] ### Changed diff --git a/README.md b/README.md index a416f10f..4c63e0a9 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ [![Maven Central](https://img.shields.io/maven-central/v/com.epam.reportportal/client-java.svg?label=Maven%20Central)](https://central.sonatype.com/artifact/com.epam.reportportal/client-java) [![CI Build](https://github.com/reportportal/client-java/actions/workflows/ci.yml/badge.svg)](https://github.com/reportportal/client-java/actions/workflows/ci.yml) [![codecov](https://codecov.io/gh/reportportal/client-java/branch/develop/graph/badge.svg?token=IVTys0o4JT)](https://codecov.io/gh/reportportal/client-java) -[![Join Slack chat!](https://slack.epmrpp.reportportal.io/badge.svg)](https://slack.epmrpp.reportportal.io/) +[![Join Slack chat!](https://img.shields.io/badge/slack-join-brightgreen.svg)](https://slack.epmrpp.reportportal.io/) [![stackoverflow](https://img.shields.io/badge/reportportal-stackoverflow-orange.svg?style=flat)](http://stackoverflow.com/questions/tagged/reportportal) [![Build with Love](https://img.shields.io/badge/build%20with-❤%EF%B8%8F%E2%80%8D-lightgrey.svg)](http://reportportal.io?style=flat) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) diff --git a/src/main/java/com/epam/reportportal/utils/MimeTypeDetector.java b/src/main/java/com/epam/reportportal/utils/MimeTypeDetector.java index 04854244..e4e1a13f 100644 --- a/src/main/java/com/epam/reportportal/utils/MimeTypeDetector.java +++ b/src/main/java/com/epam/reportportal/utils/MimeTypeDetector.java @@ -59,7 +59,7 @@ private static int[] readDetectionBytes(@Nonnull InputStream is) throws IOExcept while (((read = is.read()) != -1) && readNum < BYTES_TO_READ_FOR_DETECTION) { bytes[readNum++] = read; } - if (readNum < BYTES_TO_READ_FOR_DETECTION - 1) { + if (readNum < BYTES_TO_READ_FOR_DETECTION) { bytes = Arrays.copyOf(bytes, readNum); } is.reset(); diff --git a/src/main/java/com/epam/reportportal/utils/files/Utils.java b/src/main/java/com/epam/reportportal/utils/files/Utils.java index c44413c4..9b9c23b2 100644 --- a/src/main/java/com/epam/reportportal/utils/files/Utils.java +++ b/src/main/java/com/epam/reportportal/utils/files/Utils.java @@ -203,4 +203,20 @@ public static TypeAwareByteSource getFile(@Nonnull File file) throws IOException ByteSource byteSource = getFileAsByteSource(file); return new TypeAwareByteSource(byteSource, MimeTypeDetector.detect(byteSource, name)); } + + /** + * Copies a {@link File} into {@link File} in binary mode. + * + * @param source a stream to read from + * @param dest a stream to write to + * @throws IOException in case of a read/write error + */ + public static void copyFiles(@Nonnull File source, @Nonnull File dest) throws IOException { + ByteSource byteSource = Utils.getFileAsByteSource(source); + try (InputStream is = byteSource.openStream()) { + try (OutputStream os = Files.newOutputStream(dest.toPath())) { + Utils.copyStreams(is, os); + } + } + } } diff --git a/src/test/java/com/epam/reportportal/utils/files/TestUtils.java b/src/test/java/com/epam/reportportal/utils/files/TestUtils.java new file mode 100644 index 00000000..ca68d8ef --- /dev/null +++ b/src/test/java/com/epam/reportportal/utils/files/TestUtils.java @@ -0,0 +1,44 @@ +/* + * Copyright 2024 EPAM Systems + * + * Licensed 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 + * + * https://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 com.epam.reportportal.utils.files; + +import org.apache.commons.io.IOUtils; +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; + +public class TestUtils { + public static final String FILE_PATH = "src/test/resources/files/image.png"; + + @Test + public void test_copy_files() throws IOException { + File file = File.createTempFile("rp-test", ".png"); + Utils.copyFiles(new File(FILE_PATH), file); + + assertThat( + IOUtils.toByteArray(Files.newInputStream(file.toPath())), + equalTo(IOUtils.toByteArray(Files.newInputStream(Paths.get(FILE_PATH)))) + ); + } + +}