Skip to content

Commit

Permalink
Utils.copyFiles static method
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Feb 12, 2024
1 parent 91b66d1 commit a73d00f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 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
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 a73d00f

Please sign in to comment.