Find the directory for a FileStore

前端 未结 4 1257
清歌不尽
清歌不尽 2020-12-16 00:21

I\'m trying to find a way to detect when a flash drive has been plugged into my computer. So far, the solution I found was to poll FileSystem#getFileStores for changes. This

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-16 00:53

    This is what I have ended up doing. This is limited to Windows + UNIX but avoids using external tools or additional library calls. It steals the information Java already has in the FileStore objects

    LinuxFileStore definitely extends UnixFileStore, so it will work. Same deal for Solaris. Since Mac OS X is UNIX, it probably works there but I'm not sure because I couldn't see its subclass in any place I was looking.

    public class FileStoreHacks {
        /**
         * Stores the known hacks.
         */
        private static final Map, Hacks> hacksMap;
        static {
            ImmutableMap.Builder, Hacks> builder =
                ImmutableMap.builder();
    
            try {
                Class fileStoreClass =
                    Class.forName("sun.nio.fs.WindowsFileStore")
                        .asSubclass(FileStore.class);
                builder.put(fileStoreClass, new WindowsFileStoreHacks(fileStoreClass));
            } catch (ClassNotFoundException e) {
                // Probably not running on Windows.
            }
    
            try {
                Class fileStoreClass =
                    Class.forName("sun.nio.fs.UnixFileStore")
                        .asSubclass(FileStore.class);
                builder.put(fileStoreClass, new UnixFileStoreHacks(fileStoreClass));
            } catch (ClassNotFoundException e) {
                // Probably not running on UNIX.
            }
    
            hacksMap = builder.build();
        }
    
        private FileStoreHacks() {
        }
    
        /**
         * Gets the path from a file store. For some reason, NIO2 only has a method
         * to go in the other direction.
         *
         * @param store the file store.
         * @return the path.
         */
        public static Path getPath(FileStore store) {
            Hacks hacks = hacksMap.get(store.getClass());
            if (hacks == null) {
                return null;
            } else {
                return hacks.getPath(store);
            }
        }
    
        private static interface Hacks {
            Path getPath(FileStore store);
        }
    
        private static class WindowsFileStoreHacks implements Hacks {
            private final Field field;
    
            public WindowsFileStoreHacks(Class fileStoreClass) {
                try {
                    field = fileStoreClass.getDeclaredField("root");
                    field.setAccessible(true);
                } catch (NoSuchFieldException e) {
                    throw new IllegalStateException("file field not found", e);
                }
            }
    
            @Override
            public Path getPath(FileStore store) {
                try {
                    String root = (String) field.get(store);
                    return FileSystems.getDefault().getPath(root);
                } catch (IllegalAccessException e) {
                    throw new IllegalStateException("Denied access", e);
                }
            }
        }
    
        private static class UnixFileStoreHacks implements Hacks {
            private final Field field;
    
            private UnixFileStoreHacks(Class fileStoreClass) {
                try {
                    field = fileStoreClass.getDeclaredField("file");
                    field.setAccessible(true);
                } catch (NoSuchFieldException e) {
                    throw new IllegalStateException("file field not found", e);
                }
            }
    
            @Override
            public Path getPath(FileStore store) {
                try {
                    return (Path) field.get(store);
                } catch (IllegalAccessException e) {
                    throw new IllegalStateException("Denied access", e);
                }
            }
        }
    }
    

提交回复
热议问题