private static void listFilesystems() { System.out.println("Installed filesystem providers:"); for (FileSystemProvider p : FileSystemProvider.installedProviders()) { System.out.println(" " + p.getScheme()); } } }
public static FileSystem openZip(Path in) throws IOException { for (FileSystemProvider p : FileSystemProvider.installedProviders()) { String s = p.getScheme(); if ("jar".equals(s) || "zip".equalsIgnoreCase(s)) { return p.newFileSystem(in, new HashMap<String, Object>()); } } throw new IOException("cant find zipfs support"); }
/** * Infers the content type contained in the provided {@code Path}. * * @param path path to check * @return string with content type */ public static String detectContentType(Path path) { String result = null; try { for (FileTypeDetector detector : FileTypeDetectors.INSTALLED_DETECTORS) { result = detector.probeContentType(path); if (result != null) { break; } } } catch (IOException e) { LOGGER.log(Level.FINEST, e, () -> "Failed to find content type for " + path); } return result; }
public static String externalize(Path path) { if (path.getFileSystem().provider().getScheme().equals("file")) { return path.toString(); } else { return path.toUri().toString(); } }
private static void updateZip(File zip, File entry, String entryName) throws IOException { Map<String, String> env = new HashMap<>(); String uriPath = "jar:" + zip.toURI().toString(); URI uri = URI.create(uriPath); try (FileSystem fs = FileSystems.newFileSystem(uri, env)) { fs.provider().checkAccess(fs.getPath(entryName), AccessMode.READ); Path target = fs.getPath(entryName); Path source = entry.toPath(); Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { throw e; } }
@Override public FileSystem newFileSystem(Path path, Map<String, ?> env) throws IOException { FileSystemProvider realProvider = path.getFileSystem().provider(); return realProvider.newFileSystem(path, env); }
private DirectoryStream<Path> directoryStream(Path dir) throws IOException { // create a matcher and return a filter that uses it. return dir.getFileSystem().provider().newDirectoryStream(dir, filter); } }
return new XnioFileChannel(path.getFileSystem().provider().newFileChannel(path, openOptions)); } catch (NoSuchFileException e) { throw new FileNotFoundException(e.getMessage());
@Override public InputStream newInputStream(Path path, OpenOption... options) throws IOException { initStorage(); InputStream result = super.newInputStream(path, options); CloudStoragePath cloudPath = CloudStorageUtil.checkPath(path); int blockSize = cloudPath.getFileSystem().config().blockSize(); for (OpenOption option : options) { if (option instanceof OptionBlockSize) { blockSize = ((OptionBlockSize) option).size(); } } return new BufferedInputStream(result, blockSize); }
.getFileSystem() .provider() .getFileAttributeView(nioPath, PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS).readAttributes();
when(path.getFileSystem()).thenReturn(mock(FileSystem.class)); when(path.getFileSystem().provider()).thenReturn(mock(FileSystemProvider.class)); when(path.getFileSystem().provider().readAttributes(path, BasicFileAttributes.class)) .thenReturn(attributes);
public static FileSystem createZip(Path output) throws IOException { Map<String, Object> env = new HashMap<>(); env.put("create", "true"); Files.deleteIfExists(output); createParentDirectories(output); for (FileSystemProvider p : FileSystemProvider.installedProviders()) { String s = p.getScheme(); if ("jar".equals(s) || "zip".equalsIgnoreCase(s)) { return p.newFileSystem(output, env); } } throw new IOException("cant find zipfs support"); }
private static void listFilesystems() { System.out.println("Installed filesystem providers:"); for (FileSystemProvider p : FileSystemProvider.installedProviders()) { System.out.println(" " + p.getScheme()); } }
@Implementation protected final AssetFileDescriptor openFd(String fileName) throws IOException { Path path = findAssetFile(fileName); if (path.getFileSystem().provider().getScheme().equals("jar")) { path = getFileFromZip(path); } ParcelFileDescriptor parcelFileDescriptor = ParcelFileDescriptor.open(path.toFile(), ParcelFileDescriptor.MODE_READ_ONLY); return new AssetFileDescriptor(parcelFileDescriptor, 0, Files.size(path)); }
/** * Ensures configured directory {@code path} exists. * @throws IOException if {@code path} exists, but is not a directory, not accessible, or broken symbolic link. */ static void ensureDirectoryExists(Path path) throws IOException { // this isn't atomic, but neither is createDirectories. if (Files.isDirectory(path)) { // verify access, following links (throws exception if something is wrong) // we only check READ as a sanity test path.getFileSystem().provider().checkAccess(path.toRealPath(), AccessMode.READ); } else { // doesn't exist, or not a directory try { Files.createDirectories(path); } catch (FileAlreadyExistsException e) { // convert optional specific exception so the context is clear IOException e2 = new NotDirectoryException(path.toString()); e2.addSuppressed(e); throw e2; } } }
private static FileSystem createZip(Path output) throws IOException { Map<String, Object> env = new HashMap<>(); env.put("create", "true"); Files.deleteIfExists(output); Path parent = output.getParent(); if (parent != null && !Files.exists(parent)) { Files.createDirectories(parent); } for (FileSystemProvider p : FileSystemProvider.installedProviders()) { String s = p.getScheme(); if ("jar".equals(s) || "zip".equalsIgnoreCase(s)) { return p.newFileSystem(output, env); } } throw new IOException("cant find zipfs support"); }
private static FileSystemProvider getSystemJimfsProvider() { try { for (FileSystemProvider provider : FileSystemProvider.installedProviders()) { if (provider.getScheme().equals(URI_SCHEME)) { return provider; FileSystemProvider.class, SystemJimfsFileSystemProvider.class.getClassLoader()); for (FileSystemProvider provider : loader) { if (provider.getScheme().equals(URI_SCHEME)) { return provider;
@Test public void second() throws Exception { TempDirectory tempDirectory = new TempDirectory("test"); try { Path jarPath = tempDirectory.create("some-jar").resolve("some.jar"); try (JarOutputStream out = new JarOutputStream(new FileOutputStream(jarPath.toFile()))) { out.putNextEntry(new JarEntry("README.txt")); out.write("hi!".getBytes()); } FileSystemProvider jarFSP = FileSystemProvider.installedProviders().stream() .filter(p -> p.getScheme().equals("jar")).findFirst().get(); Path fakeJarFile = Paths.get(jarPath.toUri()); // if Thread.interrupted() was true, this would fail in AbstractInterruptibleChannel: jarFSP.newFileSystem(fakeJarFile, new HashMap<>()); } finally { tempDirectory.destroy(); } fail("failed for the right reason"); } }