I´m searching for an efficient way to read bytes from a socket channel using Java NIO. The task is quite easy, I have a solution, though I´m searching for a cleaner and more efficient way to solve this. Here´s the scenario:
My solution reads the data byte per byte and compares every byte to my marker (which is has the value 10 in UTF-8 code pages). Here´s the code:
ByteBuffer res = ByteBuffer.allocate(512);
boolean completed = false;
try {
while (true) {
ByteBuffer tmp = ByteBuffer.allocate(1);
if(soc.read(tmp) == -1) {
break;
}
// set marker back to index 0
tmp.rewind();
byte cur = tmp.get();
res.put(cur);
// have we read newline?
if (cur == 10) {
doSomething(res);
res.clear();
}
}
} catch(Exception ex) {
handle(ex);
}
Even though this does the job, there might be a better way, that doesn't need those per byte comparisons after every iteration.
Thanks for your help!
The way I would do it is to read as much as is available, such as 32 KB, and once you have read this, you copy the data byte-by-byte to another buffer, e.g. a StringBuilder. If there is data left in the buffer from the last time you read, you can continue using the buffer until it it is all consumed, at which point you read more data.
Note: each system call is expensive. It could take 2-5 micro-seconds. This doesn't sound like much unless you call it millions of times and it will add seconds to reading 1 MB.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With