将DataInput转换为DataInputStream? [英] Convert DataInput to DataInputStream?

查看:383
本文介绍了将DataInput转换为DataInputStream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

由于流定义确实没有开始或结束,因此不知道有多少可用的万无一失的方法,你只需要从固定大小的块中读取流。这几乎听起来像你会更好地使用普通的旧.read()而不是readFully():

$ $ $ $ $ $ DataInputStream dis =新的DataInputStream(...);
byte [] buf = new byte [1024];
int lastRead = 0;

do {
lastRead = dis.read(buf);
//用'buf'在这里做一些事

} while(lastRead> 0);


How can I convert DataInput to DataInputStream in java? I need to know the size of the DataInput.

解决方案

Since a stream, by definition, really has no begining or end and thus no fool proof way of knowing how much is available, you just have to read from the stream in fixed sized chunks. It almost sounds like you'd be better off with plain old .read() rather than 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);

这篇关于将DataInput转换为DataInputStream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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