Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert DataInput to DataInputStream?

Tags:

java

hadoop

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

like image 890
TTT Avatar asked Sep 27 '11 23:09

TTT


People also ask

What is the output data type of DataInputStream ()?

That is why it is called DataInputStream – because it reads data (numbers) instead of just bytes. An application uses a data output stream to write data that can later be read by a data input stream. Data input streams and data output streams represent Unicode strings in a format that is a slight modification of UTF-8.

What is the difference between DataInputStream and InputStream?

An inputStream is the base class to read bytes from a stream (network or file). It provides the ability to read bytes from the stream and detect the end of the stream. DataInputStream is a kind of InputStream to read data directly as primitive data types.


1 Answers

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);
like image 198
claymore1977 Avatar answered Oct 14 '22 22:10

claymore1977