Merge pull request #15389 from DiegoMarti2/master

baeldung-articles : BAEL-6892
This commit is contained in:
Liam Williams 2023-12-13 22:59:36 +00:00 committed by GitHub
commit 9d3f885141
1 changed files with 62 additions and 0 deletions

View File

@ -0,0 +1,62 @@
package com.baeldung.outputstreamtobytearray;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class OutputStreamToByteArrayUnitTest {
@Test
public void givenFileOutputStream_whenUsingFileUtilsToReadTheFile_thenReturnByteArray(@TempDir Path tempDir) throws IOException {
String data = "Welcome to Baeldung!";
String fileName = "file.txt";
Path filePath = tempDir.resolve(fileName);
try (FileOutputStream outputStream = new FileOutputStream(filePath.toFile())) {
outputStream.write(data.getBytes(StandardCharsets.UTF_8));
}
byte[] writtenData = FileUtils.readFileToByteArray(filePath.toFile());
String result = new String(writtenData, StandardCharsets.UTF_8);
assertEquals(data, result);
}
@Test
public void givenSystemOut_whenUsingDrainableOutputStream_thenReturnByteArray() throws IOException {
String data = "Welcome to Baeldung!\n";
DrainableOutputStream drainableOutputStream = new DrainableOutputStream(System.out);
try (drainableOutputStream) {
drainableOutputStream.write(data.getBytes(StandardCharsets.UTF_8));
}
byte[] writtenData = drainableOutputStream.toByteArray();
assertEquals(data, new String(writtenData, StandardCharsets.UTF_8));
}
public class DrainableOutputStream extends FilterOutputStream {
private final ByteArrayOutputStream buffer;
public DrainableOutputStream(OutputStream out) {
super(out);
this.buffer = new ByteArrayOutputStream();
}
@Override
public void write(byte b[]) throws IOException {
buffer.write(b);
super.write(b);
}
public byte[] toByteArray() {
return buffer.toByteArray();
}
}
}