"available" of DataInputStream from Socket
Asked Answered
L

3

4

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 ?

Lanoralanose answered 16/8, 2012 at 7:41 Comment(1)
It is perfectly reliable when using sockets. It just doesn't do what you expected. Not the same thing. It returns 0 when there is no data available to be read without blocking. See the Javadoc.Ratiocination
A
5

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));
}
Acknowledgment answered 16/8, 2012 at 7:52 Comment(3)
What do you mean "in between" ? I also tried PushBackInputStream but I wonder is it possible to share same InputStream I get from Socket with DataInputStream and PushBackInputStream ? I would be grateful if you provide a code snippet...Lanoralanose
I think it should work like that, but it's a long time since I actually used PushbackInputStream.Acknowledgment
well your code sample is definitely better... thought it would not follow cause your post was already some minutes old and posted mine.. voted up your answerJailhouse
J
2

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.

Jailhouse answered 16/8, 2012 at 8:13 Comment(0)
R
1

But available() keeps returning 0, although there is readable data in the stream

If available() returns zero, either:

  1. The input stream you are using doesn't support available() and so it just returns zero. That isn't the case here, as you are using a DataInputStream wrapped directly around the socket's input stream, and that configuration does support available(), OR ...

  2. There is no readable data in the stream. That appears to be the case here. In fact the only possible way you can know there is readable data in the stream without actually reading it is to call available() and get a positive result. There is no other way of telling.

There are few correct uses of availabe(), and this isn't one of them. Why should you fall out of that loop just because there isn't any data in the socket receive buffer? The only way you should get out of that loop is by getting an end of stream condition.

I should be reading first few bytes from stream to actually see if data is available to parse.

That doesn't even make sense. If you can read anything from the stream, there is data available, and if you can't, there isn't.

Just read, block, and react correctly to EOS, in its various manifestations.

Ratiocination answered 16/8, 2012 at 10:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.