Similar to a String.IndexOf, but uses pure bytes. @param src - the source bytes to be searched @param srcOff - offset on the source buffer @param find - the string to be found within src @return - the index of the first matching byte. -1 if the find array is not found
(byte[] src, int srcOff, byte[] find)
| 624 | * @return - the index of the first matching byte. -1 if the find array is not found |
| 625 | */ |
| 626 | public static int firstIndexOf(byte[] src, int srcOff, byte[] find) { |
| 627 | int result = -1; |
| 628 | if (find.length > src.length) { |
| 629 | return result; |
| 630 | } |
| 631 | if (find.length == 0) { |
| 632 | return result; |
| 633 | } |
| 634 | if (srcOff >= src.length) { |
| 635 | throw new ArrayIndexOutOfBoundsException(); |
| 636 | } |
| 637 | boolean found = false; |
| 638 | int srclen = src.length; |
| 639 | int findlen = find.length; |
| 640 | byte first = find[0]; |
| 641 | int pos = srcOff; |
| 642 | while (!found) { |
| 643 | // find the first byte |
| 644 | while (pos < srclen) { |
| 645 | if (first == src[pos]) { |
| 646 | break; |
| 647 | } |
| 648 | pos++; |
| 649 | } |
| 650 | if (pos >= srclen) { |
| 651 | return -1; |
| 652 | } |
| 653 | |
| 654 | // we found the first character |
| 655 | // match the rest of the bytes - they have to match |
| 656 | if ((srclen - pos) < findlen) { |
| 657 | return -1; |
| 658 | } |
| 659 | // assume it does exist |
| 660 | found = true; |
| 661 | for (int i = 1; ((i < findlen) && found); i++) { |
| 662 | found = (find[i] == src[pos + i]); |
| 663 | } |
| 664 | if (found) { |
| 665 | result = pos; |
| 666 | } else if ((srclen - pos) < findlen) { |
| 667 | return -1; // no more matches possible |
| 668 | } else { |
| 669 | pos++; |
| 670 | } |
| 671 | } |
| 672 | return result; |
| 673 | } |
| 674 | |
| 675 | |
| 676 | /** |
no outgoing calls
no test coverage detected