Update OutputStreamToByteArrayUnitTest.java

This commit is contained in:
DiegoMarti2 2023-12-11 19:36:34 +02:00 committed by GitHub
parent 3ca05da859
commit 47baa5041b
1 changed files with 10 additions and 6 deletions

View File

@ -22,7 +22,7 @@ public class OutputStreamToByteArrayUnitTest {
outputStream.write(data.getBytes(StandardCharsets.UTF_8)); outputStream.write(data.getBytes(StandardCharsets.UTF_8));
} }
byte[] writtenData = FileUtils.readFileToByteArray(new File(filePath.toUri())); byte[] writtenData = FileUtils.readFileToByteArray(filePath.toFile());
String result = new String(writtenData, StandardCharsets.UTF_8); String result = new String(writtenData, StandardCharsets.UTF_8);
assertEquals(data, result); assertEquals(data, result);
} }
@ -34,15 +34,19 @@ public class OutputStreamToByteArrayUnitTest {
String fileName = "file.txt"; String fileName = "file.txt";
Path filePath = tempDir.resolve(fileName); Path filePath = tempDir.resolve(fileName);
try (DrainableOutputStream drainableOutputStream = new DrainableOutputStream(new FileOutputStream(filePath.toFile()))) { DrainableOutputStream drainableOutputStream = new DrainableOutputStream(new FileOutputStream(filePath.toFile()));
try {
drainableOutputStream.write(data.getBytes(StandardCharsets.UTF_8)); drainableOutputStream.write(data.getBytes(StandardCharsets.UTF_8));
} finally {
drainableOutputStream.close();
}
byte[] writtenData = drainableOutputStream.toByteArray(); byte[] writtenData = drainableOutputStream.toByteArray();
assertEquals(data, new String(writtenData, StandardCharsets.UTF_8)); assertEquals(data, new String(writtenData, StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
}
} }
public class DrainableOutputStream extends FilterOutputStream { public class DrainableOutputStream extends FilterOutputStream {
private final ByteArrayOutputStream buffer; private final ByteArrayOutputStream buffer;