Advertisement
Guest User

Untitled

a guest
Sep 28th, 2011
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.67 KB | None | 0 0
  1. ByteBuffer buffer = ByteBuffer.allocate(64);
  2. // This ArrayList contains all extracted Strings
  3. ArrayList<String> complete = new ArrayList<String>();
  4. // This is the test string
  5. byte[] messages = "Hi\r\nHow are".getBytes("UTF-8");
  6. buffer.put(messages);
  7.  
  8. buffer.flip();
  9. int i;
  10. for (i = 0; i < buffer.limit(); i++) {
  11.     if (buffer.get(i) == '\n') {
  12.         // Between 0 and i + 1 there is a complete message
  13.         byte[] temp = new byte[i + 1];
  14.         buffer.get(temp, 0, i + 1);
  15.         complete.add(new String(temp, "UTF-8"));
  16.         // Shift remaining bytes to 0
  17.         buffer.compact();
  18.         buffer.flip();
  19.         // Start to look for another message
  20.         i = 0;
  21.     }
  22. }
  23. buffer.limit(64);
  24. buffer.position(i);
  25.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement