Compares the remaining bytes of this buffer to another byte buffer's remaining bytes. @param otherBuffer another byte buffer. @return a negative value if this is less than other; 0 if this equals to other; a positive value if this is greater than {@code ot
(ByteBuffer otherBuffer)
| 303 | * if {@code other} is not a byte buffer. |
| 304 | */ |
| 305 | public int compareTo(ByteBuffer otherBuffer) { |
| 306 | int compareRemaining = (remaining() < otherBuffer.remaining()) ? remaining() |
| 307 | : otherBuffer.remaining(); |
| 308 | int thisPos = position; |
| 309 | int otherPos = otherBuffer.position; |
| 310 | byte thisByte, otherByte; |
| 311 | while (compareRemaining > 0) { |
| 312 | thisByte = get(thisPos); |
| 313 | otherByte = otherBuffer.get(otherPos); |
| 314 | if (thisByte != otherByte) { |
| 315 | return thisByte < otherByte ? -1 : 1; |
| 316 | } |
| 317 | thisPos++; |
| 318 | otherPos++; |
| 319 | compareRemaining--; |
| 320 | } |
| 321 | return remaining() - otherBuffer.remaining(); |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Returns a duplicated buffer that shares its content with this buffer. |