ImageFormatCheckerUtils: add helper to find pattern at offset
@@ -39,14 +39,26 @@ public class ImageFormatCheckerUtils {
* @return true if byteArray starts with pattern
*/
public static boolean startsWithPattern(final byte[] byteArray, final byte[] pattern) {
+ return hasPatternAt(byteArray, pattern, 0);
+ }
+
+ /**
+ * Checks if byteArray interpreted as sequence of bytes starts with pattern starting at position
+ * equal to offset.
+ *
+ * @param byteArray the byte array to be checked
+ * @param pattern the pattern to check
+ * @return true if byteArray starts with pattern
+ */
+ public static boolean hasPatternAt(final byte[] byteArray, final byte[] pattern, int offset) {
Preconditions.checkNotNull(byteArray);
Preconditions.checkNotNull(pattern);
- if (pattern.length > byteArray.length) {
+ if (offset + pattern.length > byteArray.length) {
return false;
}
for (int i = 0; i < pattern.length; ++i) {
- if (byteArray[i] != pattern[i]) {
+ if (byteArray[offset + i] != pattern[i]) {
return false;
}
}
