Пример #1
0
  public static List<DiskDeviceInfo> getDefaultDiskDeviceInfos() {
    DiskDeviceInfo diskDeviceInfo = new DiskDeviceInfo(0);
    diskDeviceInfo.setName("default");

    List<DiskDeviceInfo> infos = new ArrayList<DiskDeviceInfo>();

    infos.add(diskDeviceInfo);

    return infos;
  }
Пример #2
0
  private static List<DiskDeviceInfo> getUnixDiskDeviceInfos() {
    List<DiskDeviceInfo> infos = new ArrayList<DiskDeviceInfo>();

    File file = new File(UNIX_DISK_DEVICE_PATH);
    if (!file.exists()) {
      System.out.println("No partition file:" + file.getAbsolutePath());
      return getDefaultDiskDeviceInfos();
    }

    BufferedReader reader = null;
    try {
      reader =
          new BufferedReader(new InputStreamReader(new FileInputStream(UNIX_DISK_DEVICE_PATH)));
      String line = null;

      int count = 0;
      Set<String> deviceNames = new TreeSet<String>();
      while ((line = reader.readLine()) != null) {
        if (count > 0 && !line.trim().isEmpty()) {
          String[] tokens = line.trim().split(" +");
          if (tokens.length == 4) {
            String deviceName = getDiskDeviceName(tokens[3]);
            deviceNames.add(deviceName);
          }
        }
        count++;
      }

      int id = 0;
      for (String eachDeviceName : deviceNames) {
        DiskDeviceInfo diskDeviceInfo = new DiskDeviceInfo(id++);
        diskDeviceInfo.setName(eachDeviceName);

        // TODO set additional info
        // /sys/block/sda/queue
        infos.add(diskDeviceInfo);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (reader != null) {
        try {
          reader.close();
        } catch (IOException e) {
        }
      }
    }

    return infos;
  }