Gemalto is now part of the Thales Group, find out more.

You are here

Thales IoT Developer Community

Smart parsing

Tutorial, March 4, 2014 - 9:20am, 2709 views

Here are an example how to parse on a byte array. This is performance and memory optimized.

* Class to parse on byte[] or String.
* @author Markus Enck
public class ParserTools {
       * Returns the index within the specified byte[] "buffer" of
       * the first occurrence of the specified byte[] "sequence". The
       * first byte[] has index 0. The index is the begin of the
       * byte[] "sequence". This method is memory and performance
       * optimized compared to doing same operation on a String
       * @param buffer         byte[] to parse on
       * @param sequence       byte[] containing the sequence
       * @param offset         offset
       * @return return the index of the sequence in the buffer or
       *          -1 if sequence isn't found
       public static int indexOf(byte[] buffer, byte[] sequence, int offset){
             int index = -1;
             for (int i = offset; i < buffer.length;) {
              for (int j = 0; j < sequence.length && i < buffer.length; j++){
                    if (buffer[i++] != sequence[j])
                    else if (j == sequence.length - 1) {
                    return i - ++j;
             return index;
Markus's picture

Short test: find a pattern in a byte array with a length of 34185bytes, match at byte nr. 982

1) convert to String and parse: 12ms
2) use the method above: 8ms

Performance increase of 33%

I started out with nothing... I still have most of it.