@Override public FileSystem load(@Nonnull URI key) throws Exception { try { return FileSystems.getFileSystem(key); } catch (FileSystemNotFoundException e) { try { return FileSystems.newFileSystem(key, Collections.emptyMap()); } catch (FileSystemAlreadyExistsException f) { return FileSystems.getFileSystem(key); } } } });
private static FileSystem getFileSystem(URI uri) throws IOException { try { return FileSystems.getFileSystem(uri); } catch (FileSystemNotFoundException e) { return FileSystems.newFileSystem(uri, Collections.<String, String>emptyMap()); } }
private void initFileSystem(URI uri) throws IOException { try { FileSystems.getFileSystem(uri); } catch (FileSystemNotFoundException e) { FileSystems.newFileSystem(uri, Collections.emptyMap()); } catch (IllegalArgumentException e) { FileSystems.getDefault(); } }
public static void main(String... args) throws IOException { FileSystem fs = FileSystems.getFileSystem(URI.create("gs://bucket")); byte[] data = "hello world".getBytes(StandardCharsets.UTF_8); Path path = fs.getPath("/object"); Files.write(path, data); List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8); } }
private static void checkGcs() { FileSystem fs = FileSystems.getFileSystem(URI.create("gs://domain-registry-alpha")); System.out.println("Success! We can instantiate a gs:// filesystem."); System.out.println("isOpen: " + fs.isOpen()); System.out.println("isReadOnly: " + fs.isReadOnly()); } }
try { try { fileSystem = FileSystems.getFileSystem(uri); } catch (FileSystemNotFoundException e) { fileSystem = FileSystems.newFileSystem(uri, Collections.emptyMap(), classLoader);
@Test public void testResolve_willWorkWithRecursiveCopy() throws IOException { // See: http://stackoverflow.com/a/10068306 try (FileSystem fsSource = FileSystems.getFileSystem(URI.create("gs://hello")); FileSystem fsTarget = FileSystems.getFileSystem(URI.create("gs://cat"))) { Path targetPath = fsTarget.getPath("/some/folder/"); Path relSrcPath = fsSource.getPath("file.txt"); assertThat((Object) targetPath.resolve(relSrcPath)) .isEqualTo(fsTarget.getPath("/some/folder/file.txt")); } }
@Test public void testRelativize_willWorkWithRecursiveCopy() throws IOException { // See: http://stackoverflow.com/a/10068306 try (FileSystem fsSource = FileSystems.getFileSystem(URI.create("gs://hello")); FileSystem fsTarget = FileSystems.getFileSystem(URI.create("gs://cat"))) { Path targetPath = fsTarget.getPath("/some/folder/"); Path sourcePath = fsSource.getPath("/sloth/"); Path file = fsSource.getPath("/sloth/file.txt"); assertThat((Object) targetPath.resolve(sourcePath.relativize(file))) .isEqualTo(fsTarget.getPath("/some/folder/file.txt")); } }
@Test public void testExists_false() throws IOException { try (FileSystem fs = FileSystems.getFileSystem(URI.create("gs://bucket"))) { assertThat(Files.exists(fs.getPath("/angel"))).isFalse(); } }
protected String readSchemaFileAsString(String filePath) throws IOException { try { URI uri = this.getClass().getClassLoader().getResource(filePath).toURI(); String[] array = uri.toString().split("!"); Path path; if (array.length > 1) { LOG.info("Creating fs for {}", array[0]); FileSystem fs; try { fs = FileSystems.newFileSystem(URI.create(array[0]), new HashMap<String, String>()); } catch (FileSystemAlreadyExistsException e) { fs = FileSystems.getFileSystem(URI.create(array[0])); } path = fs.getPath(array[1]); } else { path = Paths.get(uri); } return new String(Files.readAllBytes(path)); } catch (URISyntaxException e) { LOG.error("Can't generate configs {}", e); } return null; }
@Test public void testIsDirectory() throws Exception { try (FileSystem fs = FileSystems.getFileSystem(URI.create("gs://doodle"))) { assertThat(Files.isDirectory(fs.getPath(""))).isTrue(); assertThat(Files.isDirectory(fs.getPath("/"))).isTrue(); assertThat(Files.isDirectory(fs.getPath("."))).isTrue(); assertThat(Files.isDirectory(fs.getPath("./"))).isTrue(); assertThat(Files.isDirectory(fs.getPath("cat/.."))).isTrue(); assertThat(Files.isDirectory(fs.getPath("hello/cat/.."))).isTrue(); assertThat(Files.isDirectory(fs.getPath("cat/../"))).isTrue(); assertThat(Files.isDirectory(fs.getPath("hello/cat/../"))).isTrue(); } }
@Test public void testEquals() throws IOException { try (FileSystem bucket1 = CloudStorageFileSystem.forBucket("bucket"); FileSystem bucket2 = FileSystems.getFileSystem(URI.create("gs://bucket")); FileSystem doge1 = CloudStorageFileSystem.forBucket("doge"); FileSystem doge2 = FileSystems.getFileSystem(URI.create("gs://doge"))) { new EqualsTester() .addEqualityGroup(bucket1, bucket2) .addEqualityGroup(doge1, doge2) .testEquals(); } }
@Test public void testNullness() throws Exception { try (FileSystem fs = FileSystems.getFileSystem(URI.create("gs://blood"))) { NullPointerTester tester = new NullPointerTester(); tester.ignore(CloudStorageFileSystemProvider.class.getMethod("equals", Object.class)); tester.setDefault(URI.class, URI.create("gs://blood")); tester.setDefault(Path.class, fs.getPath("and/one")); tester.setDefault(OpenOption.class, CREATE); tester.setDefault(CopyOption.class, COPY_ATTRIBUTES); tester.testAllPublicStaticMethods(CloudStorageFileSystemProvider.class); tester.testAllPublicInstanceMethods(new CloudStorageFileSystemProvider()); } }
@Test public void testNullness() throws IOException, NoSuchMethodException, SecurityException { try (FileSystem fs = FileSystems.getFileSystem(URI.create("gs://bucket"))) { NullPointerTester tester = new NullPointerTester() .ignore(CloudStorageFileSystem.class.getMethod("equals", Object.class)) .setDefault(CloudStorageConfiguration.class, CloudStorageConfiguration.DEFAULT) .setDefault(StorageOptions.class, LocalStorageHelper.getOptions()); tester.testAllPublicStaticMethods(CloudStorageFileSystem.class); tester.testAllPublicInstanceMethods(fs); } }
private static FileSystem getFileSystem(URI uri) throws IOException { try { return FileSystems.getFileSystem(uri); } catch (FileSystemNotFoundException e) { return FileSystems.newFileSystem(uri, Collections.<String, String>emptyMap()); } }
public FileSystem getOrCreateFileSystem(URI uri) throws IOException { FileSystem fileSystem = null; try { fileSystem = FileSystems.getFileSystem(uri); } catch (FileSystemNotFoundException e) { Map<String, String> env = new HashMap<>(); env.put("create", "true"); fileSystem = FileSystems.newFileSystem(uri, env, null); LOGGER.debug("Created VFS for " + uri); } return fileSystem; }
private void ensureFileSystemExists(URL url) throws URISyntaxException, IOException { try { FileSystems.getFileSystem(url.toURI()); } catch (FileSystemNotFoundException notFound) { FileSystems.newFileSystem(url.toURI(), Collections.singletonMap("create","false")); } } }
try { fileSystem = FileSystems.getFileSystem(location); } catch (FileSystemNotFoundException e) {
private static void eachLine( String location, URI uri, EachLine eachLine ) throws Exception { try { FileSystem fileSystem = FileSystems.getFileSystem( uri ); Path fsPath = fileSystem.getPath( location ); BufferedReader buf = Files.newBufferedReader( fsPath, DEFAULT_CHARSET ); eachLine( buf, eachLine ); } catch ( ProviderNotFoundException ex ) { eachLine( uri.toURL().openStream(), eachLine ); } }
private static void eachLine( String location, URI uri, EachLine eachLine ) throws Exception { try { FileSystem fileSystem = FileSystems.getFileSystem( uri ); Path fsPath = fileSystem.getPath( location ); BufferedReader buf = Files.newBufferedReader( fsPath, DEFAULT_CHARSET ); eachLine( buf, eachLine ); } catch ( ProviderNotFoundException ex ) { eachLine( uri.toURL().openStream(), eachLine ); } }