title: NSData find sequence of bytes
body: <p>I need to find the sequence of bytes in my image data. I have next code on java, but I need make the same in obj-c.
  Java:</p>
  
  <pre><code>private static int searchInBuffer(byte[] pBuf, int iBufferLen) {
      for(int i = 0; i&lt;iBufferLen - 7; i++) {
          if (pBuf[i] == 'l' &amp;&amp; pBuf[i + 1] == 'i' &amp;&amp; pBuf[i + 2] == 'n' &amp;&amp; pBuf[i + 3] == 'k')
          return (int)pBuf[i + 4];
      }
      return -1;
  }
  
  public static int checkFlagInJpeg(String pFullFileName) {
      int iRes = -1;
      try {
          File f = new File(pFullFileName);
          FileInputStream is = new FileInputStream(f);
          int iBufferSize = 6 * 1024, iCount = 15;
          byte buf[] = new byte[iBufferSize];
  
          while((is.available() &gt; 0) &amp;&amp; (iCount &gt;= 0)) {
              int iRead = is.read(buf),
              iFlag = searchInBuffer(buf, iRead);
              if (iFlag &gt; 0) {
                  iRes = iFlag;
                  break;
              }
              iCount--;
          }
          is.close();
      }
  }
  </code></pre>
  
  <p>Obj-C (my version):</p>
  
  <pre><code>    UIImage *image = [UIImage imageWithCGImage:[[[self.assets objectAtIndex:indexPath.row] defaultRepresentation] fullScreenImage]];
      NSData *imageData = UIImageJPEGRepresentation(image, 1.0f);
  
      NSUInteger length = MIN(6*1024, [imageData length]);
      Byte *buffer = (Byte *)malloc(length);
      memcpy(buffer, [imageData bytes], length);
  
      for (int i=0; i &lt; length - 1; i++) {
          if (buffer[i] == 'l' &amp;&amp; buffer[i + 1] == 'i' &amp;&amp; buffer[i + 2] == 'n' &amp;&amp; buffer[i + 3] == 'k')
              NSLog(@"%c", buffer[i + 4]);
      }
      free(buffer);
  </code></pre>
  
  <p>I'm still not sure, that I understand all aspects of work with bytes, so I need a help.</p>
  
  <p><strong>UPDATE:</strong>
  The problem was in getting image data. With help of Martin R. I combine to solutions in one and get next working code:</p>
  
  <pre><code>ALAssetRepresentation *repr = [[self.assets objectAtIndex:indexPath.row] defaultRepresentation];
          NSUInteger size = (NSUInteger) repr.size;
          NSMutableData *data = [NSMutableData dataWithLength:size];
  
          NSError *error;
          [repr getBytes:data.mutableBytes fromOffset:0 length:size error:&amp;error];
  
          NSData *pattern = [@"link" dataUsingEncoding:NSUTF8StringEncoding];
          NSRange range = [data rangeOfData:pattern options:0 range:NSMakeRange(0, data.length)];
  
          int iRes = -1;
          if (range.location != NSNotFound) {
              uint8_t flag;
              [data getBytes:&amp;flag range:NSMakeRange(range.location + range.length, 1)];
              iRes = flag;
          }
  
          NSLog(@"%i", iRes);
  </code></pre>
  
  <p>It's working perfect! Thank you again!</p>