Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"available" of DataInputStream from Socket

I have this code on the client side :

DataInputStream dis = new DataInputStream(socketChannel.socket().getInputStream());
while(dis.available()){
     SomeOtherClass.method(dis);
}

But available() keeps returning 0, although there is readable data in the stream. So after the actual data to be read is finished, empty data is passed to the other class to be read and this causes corruption.

After a little search; I found that available() is not reliable when using with sockets, and that I should be reading first few bytes from stream to actually see if data is available to parse.

But in my case; I have to pass the DataInputStream reference I get from the socket to some other class that I cannot change.

Is it possible to read a few bytes from DataInputStream without corrupting it, or any other suggestions ?

like image 351
uahakan Avatar asked Feb 19 '23 11:02

uahakan


2 Answers

Putting a PushbackInputStream in between allows you to read some bytes without corrupting the data.

EDIT: Untested code example below. This is from memory.

static class MyWrapper extends PushbackInputStream {
    MyWrapper(InputStream in) {
        super(in);
    }

    @Override
    public int available() throws IOException {
        int b = super.read();
        // do something specific?
        super.unread(b);
        return super.available();
    }
}

public static void main(String... args) {
    InputStream originalSocketStream = null;
    DataInputStream dis = new DataInputStream(new MyWrapper(originalSocketStream));
}
like image 126
Eero Aaltonen Avatar answered Feb 26 '23 17:02

Eero Aaltonen


This should work:

PushbackInputStream pbi = new PushbackInputStream(socketChannel.socket().getInputStream(), 1);
int singleByte;
DataInputStream dis = new DataInputStream(pbi);
while((singleByte = pbi.read()) != -1) {
    pbi.unread(singleByte);
    SomeOtherClass.method(dis);
}

But please note that this code will behave different from the example with available (if availabe would work) because available does not block but read may block.

like image 39
Markus Kreusch Avatar answered Feb 26 '23 18:02

Markus Kreusch