如何在java中将DataInput转换为DataInputStream?我需要知道DataInput的大小。
发布于 2011-09-28 09:50:31
由于根据定义,流实际上没有开始或结束,因此没有愚蠢的方法来知道有多少可用,您只需以固定大小的块从流中读取。这听起来似乎比使用readFully()更适合使用普通的老式.read():
DataInputStream dis = new DataInputStream(...);
byte[] buf = new byte[1024];
int lastRead = 0;
do {
lastRead = dis.read(buf);
//do something with 'buf' here
} while (lastRead > 0);发布于 2013-07-11 15:44:27
当你想知道要读取多少字节时,你会遇到困难。最简单的解决方案是将其转换为ByteArrayInputStream,并使用它的available()方法来了解有多少字节可供读取。
下面的例子对我很有效
DataInput in = (...);
ByteArrayInputStream bis = (ByteArrayInputStream) in;
byte[] buffer = new byte[bis.available()];
in.readFully(buffer);
//use buffer as your wishhttps://stackoverflow.com/questions/7576908
复制相似问题