【发布时间】:2023-03-25 03:30:02
【问题描述】:
我读了this post,但我没有关注。我见过this,但还没有看到使用ByteArrayOutputStream 将ByteArrayInputStream 转换为String 的正确示例。
要将ByteArrayInputStream 的内容作为String 检索,是推荐使用ByteArrayOutputstream 还是有更好的方法?
我正在考虑使用this example 并扩展ByteArrayInputStream 并利用Decorator 来增加运行时的功能。有兴趣将其作为使用ByteArrayOutputStream 的更好解决方案吗?
【问题讨论】:
-
您可能需要
InputStreamReader,如您提供的第二个链接中所述。ByteArrayOutputStream不会将字节转换为字符。 -
你真的有一个 ByteArrayInputStream(这意味着你有一个 byte[])还是你只有一个 InputStream?
-
@BrettOkken 我真的有一个 ByteArrayInputStream,它的构造函数被传递了一个字节数组(不同大小)
-
@DavidWallace 在第二个链接中使用 InputstreamReader 回复了帖子:
The problem with this is that it reads only up to and including the first line separator. It assumes that the string you're looking for does not contain any line separators. Often that's true, but if not, this won't really work.为什么我不继续那个例子。 -
但是你可以一直读到它为空。
标签: java string bytearrayoutputstream bytearrayinputstream