Skip to content

Commit

Permalink
Merge pull request #236 from reportportal/develop
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
HardNorth committed Feb 12, 2024
2 parents 2a51007 + a73d00f commit 12c5ee7
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Changelog

## [Unreleased]
### Added
- `Utils.copyFiles` static method to use in examples, by @HardNorth

## [5.2.4]
### Changed
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/epam/reportportal/utils/files/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
44 changes: 44 additions & 0 deletions src/test/java/com/epam/reportportal/utils/files/TestUtils.java
Original file line number Diff line number Diff line change
@@ -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))))
);
}

}

0 comments on commit 12c5ee7

Please sign in to comment.