要从java中的DataInputStream读取的未知缓冲区大小 [英] Unknown buffer size to be read from a DataInputStream in java

查看:84
本文介绍了要从java中的DataInputStream读取的未知缓冲区大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下声明:

DataInputStream = new DataInputStream(process.getInputStream());

我想打印此输入流的内容,但我不知道此流的大小。我该怎么读这个流并打印出来?

I would like to print the contents of this input stream but I dont know the size of this stream. How should I read this stream and print it?

推荐答案

所有Streams都很常见,提前知道长度。使用标准 InputStream 通常的解决方案是简单地调用读取直到 -1 被退回。

It is common to all Streams, that the length is not known in advance. Using a standard InputStream the usual solution is to simply call read until -1 is returned.

但我认为你已经用 DataInputStream包装了一个标准的 InputStream 有充分理由:解析二进制数据。 (注意:扫描仪仅用于文本数据。)

But I assume, that you have wrapped a standard InputStream with a DataInputStream for a good reason: To parse binary data. (Note: Scanner is for textual data only.)

JavaDoc for DataInputStream 向您展示,此类有两种不同的方式来指示EOF - 每种方法都返回 -1 或抛出 EOFException 。一条经验法则是:

The JavaDoc for DataInputStream shows you, that this class has two different ways to indicate EOF - each method either returns -1 or throws an EOFException. A rule of thumb is:


  • InputStream 继承的每个方法都使用return -1 convention,

  • 每个方法 NOT 继承自 InputStream 抛出 EOFException

  • Every method which is inherited from InputStream uses the "return -1" convention,
  • Every method NOT inherited from InputStream throws the EOFException.

如果使用 readShort 例如,读取直到抛出异常,如果使用read(),则执行此操作直到 -1 返回。

If you use readShort for example, read until an exception is thrown, if you use "read()", do so until -1 is returned.

提示:在开头小心非常并从 DataInputStream中查找您使用的每个方法 - 经验法则可以打破。

Tip: Be very careful in the beginning and lookup each method you use from DataInputStream - a rule of thumb can break.

这篇关于要从java中的DataInputStream读取的未知缓冲区大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆