Reputation: 3521
Can anyone please explain exactly what this code and its components are doing? I am not very familiar with using processes or Android native code. It would be great if someone could explain how this code works:
private static MatchResult matchSystemFile(final String pSystemFile, final String pPattern, final int pHorizon) throws SystemUtilsException {
InputStream in = null;
try {
final Process process = new ProcessBuilder(new String[] { "/system/bin/cat", pSystemFile }).start();
in = process.getInputStream();
final Scanner scanner = new Scanner(in);
final boolean matchFound = scanner.findWithinHorizon(pPattern, pHorizon) != null;
if(matchFound) {
return scanner.match();
} else {
throw new SystemUtilsException();
}
} catch (final IOException e) {
throw new SystemUtilsException(e);
} finally {
StreamUtils.close(in);
}
}
private static int readSystemFileAsInt(final String pSystemFile) throws SystemUtilsException {
InputStream in = null;
try {
final Process process = new ProcessBuilder(new String[] { "/system/bin/cat", pSystemFile }).start();
in = process.getInputStream();
final String content = StreamUtils.readFully(in);
return Integer.parseInt(content);
} catch (final IOException e) {
throw new SystemUtilsException(e);
} catch (final NumberFormatException e) {
throw new SystemUtilsException(e);
} finally {
StreamUtils.close(in);
}
}
I need to understand this part, How process is taking two strings , I am unable to understand how this code works on two files(to me it is looking like /system/bin/cat and pSystemFile string are paths to files) and extracts required information.
final Process process = new ProcessBuilder(new String[] { "/system/bin/cat", pSystemFile }).start();
in = process.getInputStream();
final Scanner scanner = new Scanner(in);
final boolean matchFound = scanner.findWithinHorizon(pPattern, pHorizon) != null;
if(matchFound) {
return scanner.match();
}
This code is taken from AndEngines Utils.
regards, Aqif Hamid
Upvotes: 1
Views: 137
Reputation: 115398
Both methods accept file name as a parameter and run cat
utility passing the file path to it. Then both methods read the external process' output: first using Scanner
, second StreamUtils
that reads all at once and then parses the content as integer.
Upvotes: 1