Iterating over a Bytebuffer of chars in java

Home / Uncategorized / Iterating over a Bytebuffer of chars in java

Im having issue finding a solution to this problem. I have a Bytebuffer of chars(lines of text to be exact) and I need to find a sub-string in that buffer. The easiest solution would be to convert that ByteBuffer to a string and then work with that string however that solution requires me to double(at least) the amount of memory I use in the stack(or heap) of my program and I dont want that, I would rather do something along the lines of each line convert it to string and then work with that. Is there and easy way doing that? thanks.

You could read from the ByteBuffer some bytes in a buffer until finding a breakline character or a character that doesn’t make part of the String to find.
You could use this method :java.nio.ByteBuffer.get(byte[] dst, int offset, int length)

Then convert the bytes into a String (new String(bytes, yourEncoding)) and check if it contains the String to match.
Otherwise read the following bytes and repeat the same processing.
Read more

Leave a Reply

Your email address will not be published. Required fields are marked *