将DataInput转换为DataInputStream?

swa*_*ive 6 java hadoop

如何在java中将DataInput转换为DataInputStream?我需要知道DataInput的大小.

cla*_*977 1

由于根据定义,流实际上没有开始或结束,因此没有万无一失的方法来知道有多少可用,因此您只需以固定大小的块从流中读取。听起来你最好使用普通的旧 .read() 而不是 readFully():

    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);
Run Code Online (Sandbox Code Playgroud)