Specification for CharBuffer.compact() says
"..the character at index p = position() is copied to index zero, the character at index p + 1 is copied to index one, and so forth until the character at index limit() - 1 is copied to index n = limit() - p. The buffer's position is then set to n and its limit is set to its capacity..."
Say we have :
String s = "abcdefghij";
CharBuffer c = CharBuffer.wrap(c.toCharArray());
c.position(4);
c.limit(7);
Over here character at index 4 is "e" and character at index 7 is "h".
therefore:
c.compact();
should copy :
"e" to index 0
"f" to index 1
"g" to index 2
since "h" is the first character where limit starts, therefore it wont be copied.
Now index of g is 2 but according to the specs it should be limit()-p i.e.
7-4 = 3. Which is not the case.
"..the character at index p = position() is copied to index zero, the character at index p + 1 is copied to index one, and so forth until the character at index limit() - 1 is copied to index n = limit() - p. The buffer's position is then set to n and its limit is set to its capacity..."
Say we have :
String s = "abcdefghij";
CharBuffer c = CharBuffer.wrap(c.toCharArray());
c.position(4);
c.limit(7);
Over here character at index 4 is "e" and character at index 7 is "h".
therefore:
c.compact();
should copy :
"e" to index 0
"f" to index 1
"g" to index 2
since "h" is the first character where limit starts, therefore it wont be copied.
Now index of g is 2 but according to the specs it should be limit()-p i.e.
7-4 = 3. Which is not the case.