1

I'm writing a server for my app, which must get data from client and do smth. The communication is done using SocketChannel, but there is a problem: i only can read previously specified number of bytes from it (as of javadoc for channel.read(ByteBuffer dst))

An attempt is made to read up to r bytes from the channel, where r is the number of bytes remaining in the buffer

Is there any way to get size of data, that is currently in the channel and read all it into the byte[]?

TEXHIK
  • 1,369
  • 1
  • 11
  • 34

2 Answers2

0

there is a problem: i only can read previously specified number of bytes from it

That's not correct.

(as of javadoc for channel.read(ByteBuffer dst))

An attempt is made to read up to r bytes from the channel, where r is the number of bytes remaining in the buffer

You've missed the words 'up to'. There is no problem. If one byte is available, one byte will be read, regardless of the amount of room left in the ByteBuffer.

Is there any way to get size of data, that is currently in the channel and read all it into the byte[]?

That's exactly what happens. The amount of data 'currently in the channel' == the amount of data that will be read provided the room left in the ByteBuffer >= the size of the data in the socket receive buffer, and that will be the value returned by read().

user207421
  • 305,947
  • 44
  • 307
  • 483
  • I have not missed this words, but in this way, I should to create a buffer with `Integer.MAX_VALUE` capacity if I want 100% read all data from channel, and it is not a good idea. But I already done it by sending data size in first 4 bytes. – TEXHIK Nov 01 '15 at 13:44
  • So your question was incorrectly expressed. 'All data currently in the channel' can only mean 'all the data currently in the socket receive buffer'. If you mean instead 'all the data that will ever be sent over the channel' it is of course potentially infinite. – user207421 Nov 01 '15 at 23:04
-1

I think is there is none. You have to manage this by yourself. Are you using serialized objects or did you create your own protocol?

Kaputnik120
  • 120
  • 10
  • I'm sending an object, serialized to `byte[]` by default java serialization. I know, that i can send arrray size, but may be there is a way to not do it myself? – TEXHIK Oct 30 '15 at 19:47
  • Have look at my blog, I had to do this while sending serialized objects via UDP: https://ulrichbuschbaum.wordpress.com/2015/04/06/sending-an-object-via-udp-multicast-using-java/ – Kaputnik120 Oct 30 '15 at 19:57
  • Your blog contains numerous errors, starting with the bizarre claim that UDP's lack of streaming is due to Java, and the code ignoring the length returned by `receive()`, and the sender joining the multicast group when it doesn't need to. Nothing there that answers this question. – user207421 Oct 31 '15 at 07:31
  • You misunderstood this. It's due to the protocol, not Java. Perhaps I should have explained this more clearly. :) – Kaputnik120 Oct 31 '15 at 09:06
  • I misunderstood nothing. I know what it's due to. That's how I knew it was an error. When you state 'In Java there’s a little problem regarding the usage of UDP ... ' you are uttering a mistake. Certainly you need to express it more clearly. – user207421 Oct 31 '15 at 09:24