Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.sun.javafx.PlatformUtil;
- import net.samuelcampos.usbdrivedetector.USBStorageDevice;
- import net.samuelcampos.usbdrivedetector.detectors.AbstractStorageDeviceDetector;
- import net.samuelcampos.usbdrivedetector.process.CommandExecutor;
- import javax.swing.filechooser.FileSystemView;
- import java.io.File;
- import java.io.IOException;
- import java.lang.reflect.Field;
- import java.lang.reflect.Modifier;
- import java.util.ArrayList;
- import java.util.List;
- public class Windows8DeviceDetector extends AbstractStorageDeviceDetector {
- private static final String FIXED_WMI_COMMAND = "c:\\Windows\\System32\\wbem\\wmic logicaldisk where drivetype=2 get deviceid";
- @Override
- public List<USBStorageDevice> getStorageDevicesDevices() {
- final ArrayList<USBStorageDevice> listDevices = new ArrayList<>();
- try (CommandExecutor commandExecutor = new CommandExecutor(FIXED_WMI_COMMAND)) {
- commandExecutor.processOutput(outputLine -> {
- if (!outputLine.isEmpty() && !"DeviceID".equals(outputLine)) {
- final String rootPath = outputLine + File.separatorChar;
- USBStorageDevice device = getUSBDevice(rootPath, getDeviceName(rootPath));
- if (device != null) {
- listDevices.add(device);
- }
- }
- });
- } catch (IOException e) {
- e.printStackTrace();
- }
- return listDevices;
- }
- public static USBStorageDevice getUSBDevice(final String rootPath, final String deviceName) {
- final File root = new File(rootPath);
- if (!root.isDirectory()) return null;
- try {
- return new USBStorageDevice(root, deviceName);
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- }
- return null;
- }
- private String getDeviceName(final String rootPath) {
- final File f = new File(rootPath);
- final FileSystemView v = FileSystemView.getFileSystemView();
- String name = v.getSystemDisplayName(f);
- if (name != null) {
- int idx = name.lastIndexOf('(');
- if (idx != -1) {
- name = name.substring(0, idx);
- }
- name = name.trim();
- if (name.isEmpty()) {
- name = null;
- }
- }
- return name;
- }
- public static void fixIfRequired() {
- if (PlatformUtil.isWindows() && Float.parseFloat(System.getProperty("os.version")) >= 6.2) {
- try {
- Field field = AbstractStorageDeviceDetector.class.getDeclaredField("instance");
- field.setAccessible(true);
- Field modifiersField = Field.class.getDeclaredField("modifiers");
- modifiersField.setAccessible(true);
- modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
- field.set(null, new WindowsFix.Windows8DeviceDetector());
- } catch (Exception e) {
- throw new IllegalStateException("Failed to install fix for win8+ device detection");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment