All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.rmuhamedgaliev.service.CPU.impl.CPUInformationImpl Maven / Gradle / Ivy

Go to download

Library for getting information about local computer implemented on java. Used 2 Linux commands, other implemented on Java.

There is a newer version: 0.0.0.4
Show newest version
package io.github.rmuhamedgaliev.service.CPU.impl;

import io.github.rmuhamedgaliev.service.CPU.CPUInformation;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.*;

/**
 * Developer Rinat Muhamedgaliev
 * Date: 4/25/13
 */
@Service(value = "cpuInformation")
public class CPUInformationImpl implements CPUInformation{

    @Override
    public Integer getCountOfCPUCores() {
        return Runtime.getRuntime().availableProcessors();
    }

    @Override
    public int getCountOfPhysicalCPU() {
        List countCPU = parseCPUInfoFile("physical id");
        Collections.sort(countCPU);
        return Integer.parseInt(countCPU.get(countCPU.size() - 1)) + 1;
    }

    @Override
    public int getCountOfLogicalCPU() {
        List countCPU = parseCPUInfoFile("processor");
        Collections.sort(countCPU);
        return Integer.parseInt(countCPU.get(countCPU.size() - 1)) + 1;
    }

    private synchronized List parseCPUInfoFile(String criteria) {
        Charset utf8 = Charset.forName("UTF-8");
        List resultParse = new ArrayList<>();
        try {
            BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream("/proc/cpuinfo"), utf8));
            String line;
            while ((line = input.readLine()) != null){
                StringTokenizer parser = new StringTokenizer(line, ":");
                while (parser.hasMoreTokens()) {
                    String s = parser.nextToken().trim();
                     if (s.equals(criteria)){
                        resultParse.add(parser.nextToken().trim());
                        break;
                     }
                }
            }
            input.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultParse;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy