|
| 1 | +package com.baeldung.writebytearray; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import java.io.File; |
| 6 | +import java.io.FileOutputStream; |
| 7 | +import java.io.IOException; |
| 8 | +import java.nio.file.Files; |
| 9 | +import java.nio.file.Paths; |
| 10 | +import java.nio.file.StandardOpenOption; |
| 11 | + |
| 12 | +import org.apache.commons.io.FileUtils; |
| 13 | +import org.junit.BeforeClass; |
| 14 | +import org.junit.Rule; |
| 15 | +import org.junit.Test; |
| 16 | +import org.junit.rules.TemporaryFolder; |
| 17 | + |
| 18 | +import com.google.common.io.ByteSink; |
| 19 | +import com.google.common.io.MoreFiles; |
| 20 | + |
| 21 | +public class WriteByteArrayUnitTest { |
| 22 | + private static byte[] dataForWriting; |
| 23 | + |
| 24 | + @Rule |
| 25 | + public TemporaryFolder tempFolder = new TemporaryFolder(); |
| 26 | + |
| 27 | + @BeforeClass |
| 28 | + public static void setup() throws IOException { |
| 29 | + dataForWriting = Files.readAllBytes(Paths.get("src/test/resources/example-image.jpg")); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void whenUsingFileOutputStream_thenByteArrayIsWritten() throws IOException { |
| 34 | + File outputFile = tempFolder.newFile("example-fos.jpg"); |
| 35 | + try (FileOutputStream outputStream = new FileOutputStream(outputFile)) { |
| 36 | + outputStream.write(dataForWriting); |
| 37 | + assertThat(outputFile).hasBinaryContent(dataForWriting); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void whenUsingNioFiles_thenByteArrayIsWritten() throws IOException { |
| 43 | + File outputFile = tempFolder.newFile("example-nio-files.jpg"); |
| 44 | + Files.write(outputFile.toPath(), dataForWriting); |
| 45 | + assertThat(outputFile).hasBinaryContent(dataForWriting); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void whenUsingGuavaFiles_thenByteArrayIsWritten() throws IOException { |
| 50 | + File outputFile = tempFolder.newFile("example-guava-files.jpg"); |
| 51 | + com.google.common.io.Files.write(dataForWriting, outputFile); |
| 52 | + assertThat(outputFile).hasBinaryContent(dataForWriting); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void whenUsingGuavaByteSink_thenByteArrayIsWritten() throws IOException { |
| 57 | + File outputFile = tempFolder.newFile("example-guava-bs.jpg"); |
| 58 | + ByteSink byteSink = com.google.common.io.Files.asByteSink(outputFile); |
| 59 | + byteSink.write(dataForWriting); |
| 60 | + assertThat(outputFile).hasBinaryContent(dataForWriting); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void whenUsingGuavaByteSinkMoreFiles_thenByteArrayIsWritten() throws IOException { |
| 65 | + File outputFile = tempFolder.newFile("example-guava-bs.jpg"); |
| 66 | + ByteSink byteSink = MoreFiles.asByteSink(outputFile.toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE); |
| 67 | + byteSink.write(dataForWriting); |
| 68 | + assertThat(outputFile).hasBinaryContent(dataForWriting); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void whenUsingCommonsIo_thenByteArrayIsWritten() throws IOException { |
| 73 | + File outputFile = tempFolder.newFile("example-file-utils.jpg"); |
| 74 | + FileUtils.writeByteArrayToFile(outputFile, dataForWriting); |
| 75 | + assertThat(outputFile).hasBinaryContent(dataForWriting); |
| 76 | + } |
| 77 | +} |
0 commit comments