开发者

Java; NIO - reading large amounts of data from a SocketChannel [closed]

开发者 https://www.devze.com 2023-03-15 12:52 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I am writing a very simple networking library that I can use for my other projects in the future. Right now, I am having trouble handling reading operations. I have a class that is responsible for handling events from SocketChannels, and it also contains the input and output buffers. To initialize a buffer, y开发者_运维问答ou need to define the size of it. So by default, all the buffers are 1024 bytes in size.

The problem that I have is, if I read a packet that is larger than 1024 bytes, I will get an exception. This could be fixed by allocating an even larger buffer by default (2048 bytes, instead of 1024, for example) but that seems like the easy way out, and I particularly don't like that solution.

The solution that I came up with was creating a very large, direct, static buffer (it is Short.MAX_VALUE large). All data from SocketChannels would be read into the large buffer, and then copied over into the smaller buffers (and the buffers are expanded, if they cannot fit the data).

I am just worried that there might be a large cost of constantly clearing and putting data into the large 'carry-over' buffer. I would love it if there was something like SocketChannel.available(), but the only closest alternative is Socket.getInputStream().available(), but that method blocks. That way I would expand the input buffer (not the large one) if there is more available data than the buffer can fit. Unfortunately, I cannot - so the only solution that I can think of is the one I mentioned above.

I have come here to ask ... do any of you wise people have a possibly better solution to the problem? Also, I don't want to use an external library - it's just personal preference.

Thanks a lot in advance!


My apologies. After talking to a close friend, I have realized that there was no problem to begin with. It's quite a long explanation so I shall not explain it. My apologies.


Note to poster: please never delete the question text. It serves as guidance for others.


I have implemented an NIO socket layer such that I have a dynamic list of buffers that are all sized the same (whatever size you would like). For writing data, once a buffer is filled up, a new buffer is added to the list and data continues to be written to the new buffer. When it is decided (by your code, supposedly) that data is ready to be written to the socket (based on the Selector's callback), I write each of the buffers out until all the buffers have been written to the socket.

I think you can do a similar thing for reading from a SocketChannel (though I didn't implement it in my application because of the different requirements for reading). Whether or not this is a better solution for your application is up to you.

I'm not entirely sure I understand why you will get an exception...why do you try to read more than the size of the buffer? Whatever logic reads from the socket should ensure that you don't read more than the size of the buffer.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号