Skip to content

Commit ff2628b

Browse files
committed
Use Java Files utilities in FileCopyUtils
Use helpers from java.nio.file.Files for some methods in FileCopyUtils. Additionally, add unit tests for the various file-related methods.
1 parent 5d33de9 commit ff2628b

2 files changed

Lines changed: 30 additions & 3 deletions

File tree

spring-core/src/main/java/org/springframework/util/FileCopyUtils.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.util;
1818

19-
import java.io.ByteArrayInputStream;
2019
import java.io.Closeable;
2120
import java.io.File;
2221
import java.io.IOException;
@@ -76,7 +75,7 @@ public static int copy(File in, File out) throws IOException {
7675
public static void copy(byte[] in, File out) throws IOException {
7776
Assert.notNull(in, "No input byte array specified");
7877
Assert.notNull(out, "No output File specified");
79-
copy(new ByteArrayInputStream(in), Files.newOutputStream(out.toPath()));
78+
Files.write(out.toPath(), in);
8079
}
8180

8281
/**
@@ -87,7 +86,7 @@ public static void copy(byte[] in, File out) throws IOException {
8786
*/
8887
public static byte[] copyToByteArray(File in) throws IOException {
8988
Assert.notNull(in, "No input File specified");
90-
return copyToByteArray(Files.newInputStream(in.toPath()));
89+
return Files.readAllBytes(in.toPath());
9190
}
9291

9392

spring-core/src/test/java/org/springframework/util/FileCopyUtilsTests.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@
2121
import java.io.IOException;
2222
import java.io.StringReader;
2323
import java.io.StringWriter;
24+
import java.nio.file.Files;
25+
import java.nio.file.Path;
2426
import java.util.Arrays;
2527

2628
import org.junit.jupiter.api.Test;
29+
import org.junit.jupiter.api.io.TempDir;
2730

2831
import static org.assertj.core.api.Assertions.assertThat;
2932

@@ -87,4 +90,29 @@ void copyToString() throws IOException {
8790
assertThat(result).isEqualTo(content);
8891
}
8992

93+
@Test
94+
void copyFile(@TempDir Path tempDir) throws IOException {
95+
Path source = tempDir.resolve("src");
96+
Path target = tempDir.resolve("target");
97+
Files.write(source, "content".getBytes());
98+
int bytesWritten = FileCopyUtils.copy(source.toFile(), target.toFile());
99+
assertThat(bytesWritten).isEqualTo(7);
100+
assertThat(target).exists();
101+
assertThat(target).content().isEqualTo("content");
102+
}
103+
104+
@Test
105+
void copyFileToByteArray(@TempDir Path tempDir) throws IOException {
106+
Path source = tempDir.resolve("src");
107+
Files.write(source, "content".getBytes());
108+
assertThat(FileCopyUtils.copyToByteArray(source.toFile())).asString().isEqualTo("content");
109+
}
110+
111+
@Test
112+
void copyByteArrayToFile(@TempDir Path tempDir) throws IOException {
113+
Path target = tempDir.resolve("target");
114+
FileCopyUtils.copy("content".getBytes(), target.toFile());
115+
assertThat(target).exists();
116+
assertThat(target).content().isEqualTo("content");
117+
}
90118
}

0 commit comments

Comments
 (0)