|
1 | | -package com.baeldung.outputstreamtobytearray; |
2 | | - |
3 | | -import org.apache.commons.io.FileUtils; |
4 | | -import org.junit.jupiter.api.Test; |
5 | | -import org.junit.jupiter.api.io.TempDir; |
6 | | - |
7 | | -import java.io.*; |
8 | | -import java.nio.charset.StandardCharsets; |
9 | | -import java.nio.file.Path; |
10 | | - |
11 | | -import static org.junit.jupiter.api.Assertions.assertEquals; |
12 | | - |
13 | | -public class OutputStreamToByteArrayUnitTest { |
14 | | - |
15 | | - @Test |
16 | | - public void givenFileOutputStream_whenUsingFileUtilsToReadTheFile_thenReturnByteArray(@TempDir Path tempDir) throws IOException { |
17 | | - String data = "Welcome to Baeldung!"; |
18 | | - String fileName = "file.txt"; |
19 | | - Path filePath = tempDir.resolve(fileName); |
20 | | - |
21 | | - try (FileOutputStream outputStream = new FileOutputStream(filePath.toFile())) { |
22 | | - outputStream.write(data.getBytes(StandardCharsets.UTF_8)); |
23 | | - } |
24 | | - |
25 | | - byte[] writtenData = FileUtils.readFileToByteArray(filePath.toFile()); |
26 | | - String result = new String(writtenData, StandardCharsets.UTF_8); |
27 | | - assertEquals(data, result); |
28 | | - } |
29 | | - |
30 | | - |
31 | | - @Test |
32 | | - public void givenSystemOut_whenUsingDrainableOutputStream_thenReturnByteArray() throws IOException { |
33 | | - String data = "Welcome to Baeldung!\n"; |
34 | | - |
35 | | - DrainableOutputStream drainableOutputStream = new DrainableOutputStream(System.out); |
36 | | - try (drainableOutputStream) { |
37 | | - drainableOutputStream.write(data.getBytes(StandardCharsets.UTF_8)); |
38 | | - } |
39 | | - |
40 | | - byte[] writtenData = drainableOutputStream.toByteArray(); |
41 | | - assertEquals(data, new String(writtenData, StandardCharsets.UTF_8)); |
42 | | - } |
43 | | - |
44 | | - public class DrainableOutputStream extends FilterOutputStream { |
45 | | - private final ByteArrayOutputStream buffer; |
46 | | - |
47 | | - public DrainableOutputStream(OutputStream out) { |
48 | | - super(out); |
49 | | - this.buffer = new ByteArrayOutputStream(); |
50 | | - } |
51 | | - |
52 | | - @Override |
53 | | - public void write(byte b[]) throws IOException { |
54 | | - buffer.write(b); |
55 | | - super.write(b); |
56 | | - } |
57 | | - |
58 | | - public byte[] toByteArray() { |
59 | | - return buffer.toByteArray(); |
60 | | - } |
61 | | - } |
62 | | -} |
| 1 | +package com.baeldung.outputstreamtobytearray; |
| 2 | + |
| 3 | +import org.apache.commons.io.FileUtils; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | +import org.junit.jupiter.api.io.TempDir; |
| 6 | + |
| 7 | +import java.io.*; |
| 8 | +import java.nio.charset.StandardCharsets; |
| 9 | +import java.nio.file.Path; |
| 10 | + |
| 11 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 12 | + |
| 13 | +public class OutputStreamToByteArrayUnitTest { |
| 14 | + |
| 15 | + @Test |
| 16 | + public void givenFileOutputStream_whenUsingFileUtilsToReadTheFile_thenReturnByteArray(@TempDir Path tempDir) throws IOException { |
| 17 | + String data = "Welcome to Baeldung!"; |
| 18 | + String fileName = "file.txt"; |
| 19 | + Path filePath = tempDir.resolve(fileName); |
| 20 | + |
| 21 | + try (FileOutputStream outputStream = new FileOutputStream(filePath.toFile())) { |
| 22 | + outputStream.write(data.getBytes(StandardCharsets.UTF_8)); |
| 23 | + } |
| 24 | + |
| 25 | + byte[] writtenData = FileUtils.readFileToByteArray(filePath.toFile()); |
| 26 | + String result = new String(writtenData, StandardCharsets.UTF_8); |
| 27 | + assertEquals(data, result); |
| 28 | + } |
| 29 | + |
| 30 | + |
| 31 | + @Test |
| 32 | + public void givenSystemOut_whenUsingDrainableOutputStream_thenReturnByteArray() throws IOException { |
| 33 | + String data = "Welcome to Baeldung!\n"; |
| 34 | + |
| 35 | + DrainableOutputStream drainableOutputStream = new DrainableOutputStream(System.out); |
| 36 | + try { |
| 37 | + drainableOutputStream.write(data.getBytes(StandardCharsets.UTF_8)); |
| 38 | + } finally { |
| 39 | + drainableOutputStream.close(); |
| 40 | + } |
| 41 | + |
| 42 | + byte[] writtenData = drainableOutputStream.toByteArray(); |
| 43 | + assertEquals(data, new String(writtenData, StandardCharsets.UTF_8)); |
| 44 | + } |
| 45 | + |
| 46 | + public class DrainableOutputStream extends FilterOutputStream { |
| 47 | + private final ByteArrayOutputStream buffer; |
| 48 | + |
| 49 | + public DrainableOutputStream(OutputStream out) { |
| 50 | + super(out); |
| 51 | + this.buffer = new ByteArrayOutputStream(); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void write(byte b[]) throws IOException { |
| 56 | + buffer.write(b); |
| 57 | + super.write(b); |
| 58 | + } |
| 59 | + |
| 60 | + public byte[] toByteArray() { |
| 61 | + return buffer.toByteArray(); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments