Parse ByteBuffer looking for whitespace
I have a By开发者_运维知识库teBuffer containing some data (chars to be exact). How can I parse it to get only starting bytes up to first whitespace character ?
The simplest way it to step threw the ByteBuffer until you get a whitespace. e.g.
ByteBuffer buffer =
StringBuilder sb = new StringBuilder();
char ch;
while(buffer.remaining() > 0 && !Character.isWhitespace(ch = (char) buffer.get()))
sb.append(ch);
There are more efficient ways but that is perhaps the simplest.
Use the getChar()
method and it will pull out the next char for you. Check it and once you see a whitespace character. Stop reading it.
精彩评论