Thales IoT Developer Community
Smart parsing
Tutorial, March 4, 2014 - 9:20am, 4095 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]) break; else if (j == sequence.length - 1) { return i - ++j; } } } return index; } }
....
I started out with nothing... I still have most of it.