com.github.bloodshura.ignitium.ntv.pattern.HexPattern Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ignitium-native Show documentation
Show all versions of ignitium-native Show documentation
An API for working with native system APIs, with a higher-level interface.
The newest version!
package com.github.bloodshura.ignitium.ntv.pattern;
import com.github.bloodshura.ignitium.collection.view.XView;
import com.github.bloodshura.ignitium.math.BaseConverter;
import com.github.bloodshura.ignitium.tokenizer.SimpleTokenizer;
import com.github.bloodshura.ignitium.util.XApi;
import javax.annotation.Nonnull;
public class HexPattern extends AbstractSequentialPattern {
public HexPattern(@Nonnull byte... pattern) {
super(true, pattern);
}
public HexPattern(@Nonnull int... pattern) {
super(true, toPattern(pattern));
}
public HexPattern(@Nonnull String pattern) {
super(true, toPattern(pattern));
}
private static byte[] toPattern(int[] intPattern) {
byte[] pattern = new byte[intPattern.length];
for (int i = 0; i < intPattern.length; i++) {
pattern[i] = (byte) intPattern[i];
}
return pattern;
}
private static byte[] toPattern(String pattern) {
SimpleTokenizer tokenizer = new SimpleTokenizer();
XView tokens = tokenizer.tokenize(pattern);
byte[] hexPattern = new byte[tokens.size()];
int index = 0;
for (String token : tokens) {
if (token.equals("??")) {
hexPattern[index] = 0x0;
} else if (token.length() == 2) {
try {
// Can't use Byte::parseByte 'cause it won't allow "unsigned byte" values (> 127), so we use
// Short::parseShort and simply downcast it to byte
hexPattern[index] = (byte) Short.parseShort(token, BaseConverter.HEXADECIMAL);
} catch (NumberFormatException exception) {
XApi.raise("Found unrecognized value \"" + token + "\" on input pattern (value must consist of 2 hexadecimal digits)");
}
} else {
XApi.raise("Found unrecognized value \"" + token + "\" on input pattern (value must consist of 2 hexadecimal digits)");
}
index++;
}
return hexPattern;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy