Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ByteBuffer buffer = ByteBuffer.allocate(64);
- // This ArrayList contains all extracted Strings
- ArrayList<String> complete = new ArrayList<String>();
- // This is the test string
- byte[] messages = "Hi\r\nHow are".getBytes("UTF-8");
- buffer.put(messages);
- buffer.flip();
- int i;
- for (i = 0; i < buffer.limit(); i++) {
- if (buffer.get(i) == '\n') {
- // Between 0 and i + 1 there is a complete message
- byte[] temp = new byte[i + 1];
- buffer.get(temp, 0, i + 1);
- complete.add(new String(temp, "UTF-8"));
- // Shift remaining bytes to 0
- buffer.compact();
- buffer.flip();
- // Start to look for another message
- i = 0;
- }
- }
- buffer.limit(64);
- buffer.position(i);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement