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, you 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.