I want to test that the bytes I write to OutputStream
(a file OuputStream) is same as I read from same InputStream
.
Test looks like
@Test
public void testStreamBytes() throws PersistenceException, IOException, ClassNotFoundException {
String uniqueId = "TestString";
final OutputStream outStream = fileService.getOutputStream(uniqueId);
new ObjectOutputStream(outStream).write(uniqueId.getBytes());
final InputStream inStream = fileService.getInputStream(uniqueId);
}
I realized that InputStream
doesn't have getBytes()
.
How can I test something like
assertEquals(inStream.getBytes(), uniqueId.getBytes())
Thank you
You could use ByteArrayOutputStream
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[16384];
while ((nRead = inStream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
and check using:
assertEquals(buffer.toByteArray(), uniqueId.getBytes());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With