简体   繁体   中英

Understanding some Java code - I need a little explaination

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM