memcmp in Java, hooray. @param a First non-null byte array to compare. @param b Second non-null byte array to compare. @return 0 if the two arrays are identical, otherwise the difference between the first two different bytes, otherwise the different between their lengths.
(final byte[] a, final byte[] b)
| 534 | * their lengths. |
| 535 | */ |
| 536 | public static int memcmp(final byte[] a, final byte[] b) { |
| 537 | final int length = Math.min(a.length, b.length); |
| 538 | if (a == b) { // Do this after accessing a.length and b.length |
| 539 | return 0; // in order to NPE if either a or b is null. |
| 540 | } |
| 541 | for (int i = 0; i < length; i++) { |
| 542 | if (a[i] != b[i]) { |
| 543 | return (a[i] & 0xFF) - (b[i] & 0xFF); // "promote" to unsigned. |
| 544 | } |
| 545 | } |
| 546 | return a.length - b.length; |
| 547 | } |
| 548 | |
| 549 | /** |
| 550 | * {@code memcmp(3)} with a given offset and length. |
no outgoing calls
no test coverage detected