snowy_coder

usbdrivedetector win8+ fix

Jul 27th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.17 KB | None | 0 0
  1. import com.sun.javafx.PlatformUtil;
  2. import net.samuelcampos.usbdrivedetector.USBStorageDevice;
  3. import net.samuelcampos.usbdrivedetector.detectors.AbstractStorageDeviceDetector;
  4. import net.samuelcampos.usbdrivedetector.process.CommandExecutor;
  5.  
  6. import javax.swing.filechooser.FileSystemView;
  7. import java.io.File;
  8. import java.io.IOException;
  9. import java.lang.reflect.Field;
  10. import java.lang.reflect.Modifier;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13.  
  14. public class Windows8DeviceDetector extends AbstractStorageDeviceDetector {
  15.     private static final String FIXED_WMI_COMMAND = "c:\\Windows\\System32\\wbem\\wmic logicaldisk where drivetype=2 get deviceid";
  16.  
  17.     @Override
  18.     public List<USBStorageDevice> getStorageDevicesDevices() {
  19.         final ArrayList<USBStorageDevice> listDevices = new ArrayList<>();
  20.  
  21.         try (CommandExecutor commandExecutor = new CommandExecutor(FIXED_WMI_COMMAND)) {
  22.             commandExecutor.processOutput(outputLine -> {
  23.                 if (!outputLine.isEmpty() && !"DeviceID".equals(outputLine)) {
  24.                     final String rootPath = outputLine + File.separatorChar;
  25.                     USBStorageDevice device = getUSBDevice(rootPath, getDeviceName(rootPath));
  26.                     if (device != null) {
  27.                         listDevices.add(device);
  28.                     }
  29.                 }
  30.             });
  31.  
  32.         } catch (IOException e) {
  33.             e.printStackTrace();
  34.         }
  35.  
  36.         return listDevices;
  37.     }
  38.  
  39.     public static USBStorageDevice getUSBDevice(final String rootPath, final String deviceName) {
  40.         final File root = new File(rootPath);
  41.         if (!root.isDirectory()) return null;
  42.  
  43.         try {
  44.             return new USBStorageDevice(root, deviceName);
  45.         } catch (IllegalArgumentException e) {
  46.             e.printStackTrace();
  47.         }
  48.  
  49.         return null;
  50.     }
  51.  
  52.     private String getDeviceName(final String rootPath) {
  53.         final File f = new File(rootPath);
  54.         final FileSystemView v = FileSystemView.getFileSystemView();
  55.         String name = v.getSystemDisplayName(f);
  56.  
  57.         if (name != null) {
  58.             int idx = name.lastIndexOf('(');
  59.             if (idx != -1) {
  60.                 name = name.substring(0, idx);
  61.             }
  62.  
  63.             name = name.trim();
  64.             if (name.isEmpty()) {
  65.                 name = null;
  66.             }
  67.         }
  68.         return name;
  69.     }
  70.  
  71.     public static void fixIfRequired() {
  72.         if (PlatformUtil.isWindows() && Float.parseFloat(System.getProperty("os.version")) >= 6.2) {
  73.             try {
  74.                 Field field = AbstractStorageDeviceDetector.class.getDeclaredField("instance");
  75.                 field.setAccessible(true);
  76.  
  77.                 Field modifiersField = Field.class.getDeclaredField("modifiers");
  78.                 modifiersField.setAccessible(true);
  79.                 modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
  80.  
  81.  
  82.                 field.set(null, new WindowsFix.Windows8DeviceDetector());
  83.             } catch (Exception e) {
  84.                 throw new IllegalStateException("Failed to install fix for win8+ device detection");
  85.             }
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment