【发布时间】:2012-08-31 22:12:02
【问题描述】:
我想测试我写入OutputStream(文件输出流)的字节是否与我从同一InputStream 读取的字节相同。
测试看起来像
@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);
}
我意识到InputStream 没有getBytes()。
我如何测试类似的东西
assertEquals(inStream.getBytes(), uniqueId.getBytes())
谢谢
【问题讨论】:
-
补充:String#getBytes() 在将字符串编码为字节时假定系统默认字符集,不要忘记。没有办法“只从字符串中获取字节”,因为这只能通过使用字符集将每个字符编码为一个或多个字节来实现。
标签: java inputstream