开发者

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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜