org.apache.commons.vfs2.VFS Java Examples

The following examples show how to use org.apache.commons.vfs2.VFS. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BakeWatcher.java    From jbake with MIT License 6 votes vote down vote up
/**
 * Starts watching the file system for changes to trigger a bake.
 *
 * @param config JBakeConfiguration settings
 */
public void start(JBakeConfiguration config) {
    try {
        FileSystemManager fsMan = VFS.getManager();
        FileObject listenPath = fsMan.resolveFile(config.getContentFolder().toURI());
        FileObject templateListenPath = fsMan.resolveFile(config.getTemplateFolder().toURI());
        FileObject assetPath = fsMan.resolveFile(config.getAssetFolder().toURI());

        logger.info("Watching for (content, template, asset) changes in [{}]", config.getSourceFolder().getPath());
        DefaultFileMonitor monitor = new DefaultFileMonitor(new CustomFSChangeListener(config));
        monitor.setRecursive(true);
        monitor.addFile(listenPath);
        monitor.addFile(templateListenPath);
        monitor.addFile(assetPath);
        monitor.start();
    } catch (FileSystemException e) {
        logger.error("Problems watching filesystem changes", e);
    }
}
 
Example #2
Source File: BaseParsingTest.java    From hop with Apache License 2.0 6 votes vote down vote up
/**
 * Initialize transform info. Method is final against redefine in descendants.
 */
@Before
public final void beforeCommon() throws Exception {
  HopEnvironment.init();
  PluginRegistry.addPluginType( CompressionPluginType.getInstance() );
  PluginRegistry.init( false );

  transformMeta = new TransformMeta();
  transformMeta.setName( "test" );

  pipeline = new LocalPipelineEngine();
  pipeline.setLogChannel( log );
  pipeline.setRunning( true );
  pipelineMeta = new PipelineMeta() {
    @Override
    public TransformMeta findTransform( String name ) {
      return transformMeta;
    }
  };

  fs = VFS.getManager();
  inPrefix = '/' + this.getClass().getPackage().getName().replace( '.', '/' ) + "/files/";
}
 
Example #3
Source File: DefaultFileContentTest.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
@Test
public void testMarkingWorks() throws Exception {
    final File temp = File.createTempFile("temp-file-name", ".tmp");
    final FileSystemManager fileSystemManager = VFS.getManager();

    try (FileObject file = fileSystemManager.resolveFile(temp.getAbsolutePath())) {
        try (OutputStream outputStream = file.getContent().getOutputStream()) {
            outputStream.write(expected.getBytes());
            outputStream.flush();
        }
        try (InputStream stream = file.getContent().getInputStream()) {
            if (stream.markSupported()) {
                for (int i = 0; i < 10; i++) {
                    stream.mark(0);
                    final byte[] data = new byte[100];
                    stream.read(data, 0, 7);
                    stream.read();
                    Assert.assertEquals(expected, new String(data).trim());
                    stream.reset();
                }
            }
        }
    }
}
 
Example #4
Source File: Shell.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
private Shell() throws IOException {
    final String providers = System.getProperty("providers");
    final URL providersUrl = (providers != null) ? Shell.class.getResource("/" + providers) : null;

    if (providersUrl != null) {
        mgr = new StandardFileSystemManager();
        System.out.println("Custom providers configuration used: " + providersUrl);
        ((StandardFileSystemManager) mgr).setConfiguration(providersUrl);
        ((StandardFileSystemManager) mgr).init();
    } else {
        mgr = VFS.getManager();
    }

    cwd = mgr.toFileObject(new File(System.getProperty("user.dir")));
    reader = new BufferedReader(new InputStreamReader(System.in, Charset.defaultCharset()));
}
 
Example #5
Source File: BaseParsingTest.java    From hop with Apache License 2.0 6 votes vote down vote up
/**
 * Initialize transform info. Method is final against redefine in descendants.
 */
@Before
public final void beforeCommon() throws Exception {
  HopEnvironment.init();
  PluginRegistry.addPluginType( CompressionPluginType.getInstance() );
  PluginRegistry.init( false );

  transformMeta = new TransformMeta();
  transformMeta.setName( "test" );

  pipeline = new LocalPipelineEngine();
  pipeline.setLogChannel( log );
  pipeline.setRunning( true );
  pipelineMeta = new PipelineMeta() {
    @Override
    public TransformMeta findTransform( String name ) {
      return transformMeta;
    }
  };

  fs = VFS.getManager();
  inPrefix = '/' + this.getClass().getPackage().getName().replace( '.', '/' ) + "/files/";
}
 
Example #6
Source File: BaseParsingTest.java    From hop with Apache License 2.0 6 votes vote down vote up
/**
 * Initialize transform info. Method is final against redefine in descendants.
 */
@Before
public final void beforeCommon() throws Exception {
  HopEnvironment.init();
  PluginRegistry.addPluginType( CompressionPluginType.getInstance() );
  PluginRegistry.init( false );

  transformMeta = new TransformMeta();
  transformMeta.setName( "test" );

  pipeline = new LocalPipelineEngine();
  pipeline.setLogChannel( log );
  pipeline.setRunning( true );
  pipelineMeta = new PipelineMeta() {
    @Override
    public TransformMeta findTransform( String name ) {
      return transformMeta;
    }
  };

  fs = VFS.getManager();
  inPrefix = '/' + this.getClass().getPackage().getName().replace( '.', '/' ) + "/files/";
}
 
Example #7
Source File: Http5FilesCacheTestCase.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Tests https://issues.apache.org/jira/browse/VFS-426
 */
@Test
public void testQueryStringUrls() throws FileSystemException {
    final String noQueryStringUrl = "http5://commons.apache.org/vfs";
    final String queryStringUrl = "http5://commons.apache.org/vfs?query=string";
    final String queryStringUrl2 = "http5://commons.apache.org/vfs?query=string&more=stuff";

    final FileSystemManager fileSystemManager = VFS.getManager();

    final FileObject noQueryFile = fileSystemManager.resolveFile(noQueryStringUrl);
    Assert.assertEquals(noQueryStringUrl, noQueryFile.getURL().toExternalForm());

    final FileObject queryFile = fileSystemManager.resolveFile(queryStringUrl);
    Assert.assertEquals(queryStringUrl, queryFile.getURL().toExternalForm()); // failed for VFS-426

    final FileObject queryFile2 = fileSystemManager.resolveFile(queryStringUrl2);
    Assert.assertEquals(queryStringUrl2, queryFile2.getURL().toExternalForm()); // failed for VFS-426
}
 
Example #8
Source File: Bzip2TestCase.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
@Test
public void testBZip2() throws IOException {
    final File testResource = getTestResource("bla.txt.bz2");
    try (final FileObject bz2FileObject = VFS.getManager().resolveFile("bz2://" + testResource)) {
        Assert.assertTrue(bz2FileObject.exists());
        Assert.assertTrue(bz2FileObject.isFolder());
        try (final FileObject fileObjectDir = bz2FileObject.resolveFile("bla.txt")) {
            Assert.assertTrue(fileObjectDir.exists());
            Assert.assertTrue(bz2FileObject.isFolder());
            try (final FileObject fileObject = fileObjectDir.resolveFile("bla.txt")) {
                Assert.assertTrue(fileObject.exists());
                Assert.assertFalse(fileObject.isFolder());
                Assert.assertTrue(fileObject.isFile());
                try (final FileContent content = fileObject.getContent()) {
                    Assert.assertEquals(CompressedFileFileObject.SIZE_UNDEFINED, content.getSize());
                    // blows up, Commons Compress?
                    final String string = content.getString(StandardCharsets.UTF_8);
                    Assert.assertEquals(26, string.length());
                    Assert.assertEquals("Hallo, dies ist ein Test.\n", string);
                }
            }
        }
    }
}
 
Example #9
Source File: JarUtils.java    From spring-boot-jar-resources with Apache License 2.0 6 votes vote down vote up
@SneakyThrows
private static File copyToDir(FileObject jarredFile, File destination, boolean retryIfImaginary) {
    switch (jarredFile.getType()) {
        case FILE:
            return copyFileToDir(jarredFile, destination);
        case FOLDER:
            return copyDirToDir(jarredFile, destination);
        case IMAGINARY:
            if (retryIfImaginary) {
                log.debug("Imaginary file found, retrying extraction");
                VFS.getManager().getFilesCache().removeFile(jarredFile.getFileSystem(), jarredFile.getName());
                FileObject newJarredFile = VFS.getManager().resolveFile(jarredFile.getName().getURI());
                return copyToDir(newJarredFile, destination, false);
            } else {
                log.debug("Imaginary file found after retry, abandoning retry");
            }

        default:
            throw new IllegalStateException("File Type not supported: " + jarredFile.getType());
    }
}
 
Example #10
Source File: UrlTests.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Tests FindFiles with a file name that has a hash sign in it.
 */
public void testHashFindFiles() throws Exception {
    final FileSystemManager fsManager = VFS.getManager();

    final FileObject[] foList = getBaseFolder().findFiles(Selectors.SELECT_FILES);

    boolean hashFileFound = false;
    for (final FileObject fo : foList) {
        if (fo.getURL().toString().contains("test-hash")) {
            hashFileFound = true;

            assertEquals(fo.toString(), UriParser.decode(fo.getURL().toString()));
        }
    }

    if (!hashFileFound) {
        fail("Test hash file containing 'test-hash' not found");
    }
}
 
Example #11
Source File: ParseClipboard.java    From otroslogviewer with Apache License 2.0 6 votes vote down vote up
private void loadLogFileAsContent(String data, TabWithName target) throws IOException {
  final FileObject tempFileWithClipboard = VFS.getManager().resolveFile("clipboard://clipboard_"+System.currentTimeMillis());
  tempFileWithClipboard.createFile();
  final OutputStream outputStream = tempFileWithClipboard.getContent().getOutputStream();
  outputStream.write(data.getBytes());
  outputStream.flush();
  outputStream.close();
  final LogImporter logImporter = logParserComboBox.getItemAt(logParserComboBox.getSelectedIndex());
  if (target.getLogDataCollector().isPresent()){
    final LogViewPanelI logViewPanelI = target.getLogDataCollector().get();
    getOtrosApplication().getLogLoader().startLoading(new VfsSource(tempFileWithClipboard),logImporter,logViewPanelI);
  } else {
    final String tabTitle = new SimpleDateFormat("HH:mm:ss").format(new Date());
    new TailLogActionListener(getOtrosApplication(), logImporter)
      .openFileObjectInTailMode(tempFileWithClipboard, "Clipboard " + tabTitle);
  }


}
 
Example #12
Source File: Http4FilesCacheTestCase.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Tests https://issues.apache.org/jira/browse/VFS-426
 */
@Test
public void testQueryStringUrls() throws FileSystemException {
    final String noQueryStringUrl = "http4://commons.apache.org/vfs";
    final String queryStringUrl = "http4://commons.apache.org/vfs?query=string";
    final String queryStringUrl2 = "http4://commons.apache.org/vfs?query=string&more=stuff";

    final FileSystemManager fileSystemManager = VFS.getManager();

    final FileObject noQueryFile = fileSystemManager.resolveFile(noQueryStringUrl);
    Assert.assertEquals(noQueryStringUrl, noQueryFile.getURL().toExternalForm());

    final FileObject queryFile = fileSystemManager.resolveFile(queryStringUrl);
    Assert.assertEquals(queryStringUrl, queryFile.getURL().toExternalForm()); // failed for VFS-426

    final FileObject queryFile2 = fileSystemManager.resolveFile(queryStringUrl2);
    Assert.assertEquals(queryStringUrl2, queryFile2.getURL().toExternalForm()); // failed for VFS-426
}
 
Example #13
Source File: SingleInstanceRequestResponseDelegate.java    From otroslogviewer with Apache License 2.0 6 votes vote down vote up
public static void openFilesFromStartArgs(final OtrosApplication otrosApplication, List<String> filesList, String path) {
  ArrayList<FileObject> fileObjects = new ArrayList<>();
  for (String file : filesList) {
    try {
      FileObject fo = VFS.getManager().resolveFile(new File(path), file);
      fileObjects.add(fo);
    } catch (FileSystemException e) {
      LOGGER.error("Cant resolve " + file + " in path " + path, e);
    }
  }
  final FileObject[] files = fileObjects.toArray(new FileObject[fileObjects.size()]);
  SwingUtilities.invokeLater(() -> {
    JFrame applicationJFrame = null;
    if (otrosApplication != null) {
      applicationJFrame = otrosApplication.getApplicationJFrame();
      OtrosSwingUtils.frameToFront(applicationJFrame);
    }
    if (files.length > 1) {
      new TailMultipleFilesIntoOneView(otrosApplication).openFileObjectsIntoOneView(files, applicationJFrame);
    } else if (files.length == 1) {
      //open log as one file
      LOGGER.debug("WIll open {}", files[0]);
      new TailLogWithAutoDetectActionListener(otrosApplication).openFileObjectInTailMode(files[0], Utils.getFileObjectShortName(files[0]));
    }
  });
}
 
Example #14
Source File: FTPRemoteDownloadSourceDelegate.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Override
public String archive(String fromPath) throws IOException {
  if (archiveURI == null) {
    throw new IOException("No archive directory defined - cannot archive");
  }

  String toPath = archiveURI.toString() + (fromPath.startsWith("/") ? fromPath.substring(1) : fromPath);

  FileObject toFile = VFS.getManager().resolveFile(toPath, archiveOptions);
  toFile.refresh();
  // Create the toPath's parent dir(s) if they don't exist
  toFile.getParent().createFolder();
  resolveChild(fromPath).moveTo(toFile);
  toFile.close();
  return toPath;
}
 
Example #15
Source File: ZipFileObjectTestCase.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that we can get a stream from one file in a zip file, then close another file from the same zip, then
 * process the initial input stream. If our internal reference counting is correct, the test passes.
 *
 * @throws IOException
 */
@Test
public void testReadingOneAfterClosingAnotherStream() throws IOException {
    final File newZipFile = createTempFile();
    final FileSystemManager manager = VFS.getManager();
    final FileObject zipFileObject1;
    final InputStream inputStream1;
    try (final FileObject zipFileObject = manager.resolveFile("zip:file:" + newZipFile.getAbsolutePath())) {
        // leave resources open (note that internal counters are updated)
        zipFileObject1 = zipFileObject.resolveFile(NESTED_FILE_1);
        inputStream1 = zipFileObject1.getContent().getInputStream();
        resolveReadAssert(zipFileObject, NESTED_FILE_2);
    }
    // The Zip file is "closed", but we read from the stream now, which currently fails.
    // Why aren't internal counters preventing the stream from closing?
    readAndAssert(zipFileObject1, inputStream1, "1");
    // clean up
    zipFileObject1.close();
    assertDelete(newZipFile);
}
 
Example #16
Source File: FileSystemManagerFactoryTestCase.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Sanity test.
 */
public void testDefaultInstance() throws Exception {
    // Locate the default manager
    final FileSystemManager manager = VFS.getManager();

    // Lookup a test jar file
    final File jarFile = getTestResource("test.jar");
    // File
    final FileObject file = manager.toFileObject(jarFile);
    check(manager, file);
    // URI
    final FileObject file2 = manager.resolveFile(jarFile.toURI());
    check(manager, file2);
    // URL
    final FileObject file3 = manager.resolveFile(jarFile.toURI().toURL());
    check(manager, file3);
}
 
Example #17
Source File: VFSFileSystem.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
@Override
public String getBasePath(final String path)
{
    if (UriParser.extractScheme(path) == null)
    {
        return super.getBasePath(path);
    }
    try
    {
        final FileSystemManager fsManager = VFS.getManager();
        final FileName name = fsManager.resolveURI(path);
        return name.getParent().getURI();
    }
    catch (final FileSystemException fse)
    {
        fse.printStackTrace();
        return null;
    }
}
 
Example #18
Source File: ZipFileObjectTestCase.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that we can get a stream from one file in a zip file, then close another file from the same zip, then
 * process the initial input stream.
 *
 * @throws IOException
 */
@Test
public void testReadingOneAfterClosingAnotherFile() throws IOException {
    final File newZipFile = createTempFile();
    final FileSystemManager manager = VFS.getManager();
    final FileObject zipFileObject1;
    final InputStream inputStream1;
    try (final FileObject zipFileObject = manager.resolveFile("zip:file:" + newZipFile.getAbsolutePath())) {
        // leave resources open
        zipFileObject1 = zipFileObject.resolveFile(NESTED_FILE_1);
        inputStream1 = zipFileObject1.getContent().getInputStream();
    }
    // The zip file is "closed", but we read from the stream now.
    readAndAssert(zipFileObject1, inputStream1, "1");
    // clean up
    zipFileObject1.close();
    assertDelete(newZipFile);
}
 
Example #19
Source File: FileObjectResourceLoader.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public ResourceKey deserialize( final ResourceKey bundleKey, final String stringKey )
      throws ResourceKeyCreationException {
  // Parse the data
  final ResourceKeyData keyData = ResourceKeyUtils.parse( stringKey );

  // Validate the data
  if ( SCHEMA_NAME.equals( keyData.getSchema() ) == false ) {
    throw new ResourceKeyCreationException( "Serialized version of fileObject key does not contain correct schema" );
  }

  // Create a new fileObject based on the path provided
  try {
    final FileObject fileObject = VFS.getManager().resolveFile( keyData.getIdentifier() );
    return new ResourceKey( SCHEMA_NAME, fileObject, keyData.getFactoryParameters() );
  } catch ( FileSystemException fse ) {
    throw new ResourceKeyCreationException( "Serialized version of fileObject key does not result into a valid FileObject" );
  }
}
 
Example #20
Source File: VFSFileHandlerReloadingDetector.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the file that is monitored by this strategy. Note that the return
 * value can be <b>null </b> under some circumstances.
 *
 * @return the monitored file
 */
protected FileObject getFileObject()
{
    if (!getFileHandler().isLocationDefined())
    {
        return null;
    }

    try
    {
        final FileSystemManager fsManager = VFS.getManager();
        final String uri = resolveFileURI();
        if (uri == null)
        {
            throw new ConfigurationRuntimeException("Unable to determine file to monitor");
        }
        return fsManager.resolveFile(uri);
    }
    catch (final FileSystemException fse)
    {
        final String msg = "Unable to monitor " + getFileHandler().getURL().toString();
        log.error(msg);
        throw new ConfigurationRuntimeException(msg, fse);
    }
}
 
Example #21
Source File: DefaultFileContentTest.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Test VFS-724 should be done on a website which render a page with no content size. Note the getSize() is
 * currently the value sent back by the server then zero usually means no content length attached.
 */
@Test
public void testGetZeroContents() throws IOException {
    final FileSystemManager fsManager = VFS.getManager();
    try (final FileObject fo = fsManager.resolveFile(new File("."), "src/test/resources/test-data/size-0-file.bin");
            final FileContent content = fo.getContent()) {
        Assert.assertEquals(0, content.getSize());
        Assert.assertTrue(content.isEmpty());
        Assert.assertEquals(StringUtils.EMPTY, content.getString(StandardCharsets.UTF_8));
        Assert.assertEquals(StringUtils.EMPTY, content.getString(StandardCharsets.UTF_8.name()));
        Assert.assertArrayEquals(ArrayUtils.EMPTY_BYTE_ARRAY, content.getByteArray());
    }
}
 
Example #22
Source File: ZipFileObjectTestCase.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that we can read more than one file within a Zip file, especially after closing each FileObject.
 *
 * @throws IOException
 */
@Test
public void testReadingFilesInZipFile() throws IOException {
    final File newZipFile = createTempFile();
    final FileSystemManager manager = VFS.getManager();
    try (final FileObject zipFileObject = manager.resolveFile("zip:file:" + newZipFile.getAbsolutePath())) {
        try (final FileObject zipFileObject1 = zipFileObject.resolveFile(NESTED_FILE_1)) {
            try (final InputStream inputStream = zipFileObject1.getContent().getInputStream()) {
                readAndAssert(zipFileObject1, inputStream, "1");
            }
        }
        resolveReadAssert(zipFileObject, NESTED_FILE_2);
    }
    assertDelete(newZipFile);
}
 
Example #23
Source File: DefaultFileSystemManagerTest.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Tests {@link DefaultFileSystemManager#close()}.
 *
 * @throws FileSystemException
 */
@Test
public void test_close() throws FileSystemException {
    try (FileSystemManager fileSystemManager = new DefaultFileSystemManager()) {
        VFS.setManager(fileSystemManager);
        VFS.setManager(null);
    }
    Assert.assertNotNull(VFS.getManager());
    Assert.assertFalse(VFS.getManager().resolveFile(Paths.get("DoesNotExist.not").toUri()).exists());
}
 
Example #24
Source File: Jira733TestCase.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
private void testZipParentLayer(final VfsConsumer<FileObject> consumer) throws Exception {
    final File file = new File("src/test/resources/test-data/test.zip");
    Assert.assertTrue(file.exists());
    final String nestedPath = "zip:" + file.getAbsolutePath() + "!/read-tests/file1.txt";
    try (final FileObject fileObject = VFS.getManager().resolveFile(nestedPath);
            final FileObject wrappedFileObject = new OnCallRefreshFileObject(fileObject)) {
        Assert.assertTrue(fileObject instanceof ZipFileObject);
        @SuppressWarnings({ "unused", "resource" })
        final
        ZipFileObject zipFileObject = (ZipFileObject) fileObject;
        Assert.assertNotNull("getParentLayer() 1", wrappedFileObject.getFileSystem().getParentLayer());
        consumer.accept(wrappedFileObject);
        Assert.assertNotNull("getParentLayer() 2", wrappedFileObject.getFileSystem().getParentLayer());
    }
}
 
Example #25
Source File: Jira733TestCase.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
@Test
public void testZipParentLayer() throws Exception {
    final File file = new File("src/test/resources/test-data/test.zip");
    final String nestedPath = "zip:" + file.getAbsolutePath() + "!/read-tests/file1.txt";
    try (final FileObject fileObject = VFS.getManager().resolveFile(nestedPath);
            final FileObject wrappedFileObject = new OnCallRefreshFileObject(fileObject)) {
        // VFS.getManager().getFilesCache().close();
        Assert.assertNotNull("getParentLayer() 1", wrappedFileObject.getFileSystem().getParentLayer());
        wrappedFileObject.exists();
        wrappedFileObject.getContent();
        Assert.assertNotNull("getParentLayer() 2", wrappedFileObject.getFileSystem().getParentLayer());
    }
}
 
Example #26
Source File: RegexFileFilterExample.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args) throws Exception {

        // Example, to retrieve and print all java files where the name matched
        // the regular expression in the current directory
        final FileSystemManager fsManager = VFS.getManager();
        final FileObject dir = fsManager.toFileObject(new File("."));
        final FileObject[] files = dir.findFiles(new FileFilterSelector(new RegexFileFilter(
                "ˆ.*[tT]est(-\\d+)?\\.java$")));
        for (FileObject file : files) {
            System.out.println(file);
        }

    }
 
Example #27
Source File: FileFileFilterExample.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args) throws Exception {

        // Example, how to print out a list of the real files within the current
        // directory
        final FileSystemManager fsManager = VFS.getManager();
        final FileObject dir = fsManager.toFileObject(new File("."));
        final FileObject[] files = dir.findFiles(new FileFilterSelector(FileFileFilter.FILE));
        for (FileObject file : files) {
            System.out.println(file);
        }
    }
 
Example #28
Source File: DefaultFileContentTest.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void testOutputStreamBufferSizeNegativeWithAppendFlag() throws Exception {
    final File temp = File.createTempFile("temp-file-name", ".tmp");
    final FileSystemManager fileSystemManager = VFS.getManager();

    try (FileObject file = fileSystemManager.resolveFile(temp.getAbsolutePath())) {
        file.getContent().getOutputStream(true, -1);
    }
}
 
Example #29
Source File: FileObjectUtilsTest.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
@Test
public void testgetContentAsString_CharsetNull() throws FileSystemException, IOException {
    Assert.assertEquals("This is a test file.",
        FileObjectUtils.getContentAsString(
            VFS.getManager().toFileObject(new File("src/test/resources/test-data/read-tests/file1.txt")),
            (Charset) null));
}
 
Example #30
Source File: VFSFileSystem.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
@Override
public OutputStream getOutputStream(final URL url) throws ConfigurationException
{
    try
    {
        final FileSystemOptions opts = getOptions(url.getProtocol());
        final FileSystemManager fsManager = VFS.getManager();
        final FileObject file = opts == null ? fsManager.resolveFile(url.toString())
                : fsManager.resolveFile(url.toString(), opts);
        // throw an exception if the target URL is a directory
        if (file == null || file.getType() == FileType.FOLDER)
        {
            throw new ConfigurationException("Cannot save a configuration to a directory");
        }
        final FileContent content = file.getContent();

        if (content == null)
        {
            throw new ConfigurationException("Cannot access content of " + url);
        }
        return content.getOutputStream();
    }
    catch (final FileSystemException fse)
    {
        throw new ConfigurationException("Unable to access " + url, fse);
    }
}