Java Code Examples for org.apache.commons.vfs2.FileObject#getContent()

The following examples show how to use org.apache.commons.vfs2.FileObject#getContent() . 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: OldVFSNotebookRepo.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
private Note getNote(FileObject noteDir) throws IOException {
  if (!isDirectory(noteDir)) {
    throw new IOException(noteDir.getName().toString() + " is not a directory");
  }

  FileObject noteJson = noteDir.resolveFile("note.json", NameScope.CHILD);
  if (!noteJson.exists()) {
    throw new IOException(noteJson.getName().toString() + " not found");
  }
  
  FileContent content = noteJson.getContent();
  InputStream ins = content.getInputStream();
  String json = IOUtils.toString(ins, conf.getString(ConfVars.ZEPPELIN_ENCODING));
  ins.close();

  return Note.fromJson(json);
}
 
Example 2
Source File: CustomRamProviderTest.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
private InputStream createNonEmptyFile() throws FileSystemException, IOException {
    final FileObject root = manager.resolveFile("ram://file");
    root.createFile();

    final FileContent content = root.getContent();
    final OutputStream output = this.closeOnTearDown(content.getOutputStream());
    output.write(1);
    output.write(2);
    output.write(3);
    output.flush();
    output.close();

    return this.closeOnTearDown(content.getInputStream());
}
 
Example 3
Source File: HopVfs.java    From hop with Apache License 2.0 5 votes vote down vote up
public static OutputStream getOutputStream( FileObject fileObject, boolean append ) throws IOException {
  FileObject parent = fileObject.getParent();
  if ( parent != null ) {
    if ( !parent.exists() ) {
      throw new IOException( BaseMessages.getString(
        PKG, "HopVFS.Exception.ParentDirectoryDoesNotExist", getFriendlyURI( parent ) ) );
    }
  }
  try {
    fileObject.createFile();
    FileContent content = fileObject.getContent();
    return content.getOutputStream( append );
  } catch ( FileSystemException e ) {
    // Perhaps if it's a local file, we can retry using the standard
    // File object. This is because on Windows there is a bug in VFS.
    //
    if ( fileObject instanceof LocalFile ) {
      try {
        String filename = getFilename( fileObject );
        return new FileOutputStream( new File( filename ), append );
      } catch ( Exception e2 ) {
        throw e; // throw the original exception: hide the retry.
      }
    } else {
      throw e;
    }
  }
}
 
Example 4
Source File: PubmedArchiveCollectionReader.java    From bluima with Apache License 2.0 5 votes vote down vote up
private NextArticle getNextArticle() throws IOException {

		while (directoryIterator.hasNext()) {
			File f = directoryIterator.next();
			LOG.debug("extracting " + f.getAbsolutePath());

			try {
				FileObject archive = fsManager.resolveFile("tgz:file://"
						+ f.getAbsolutePath());

				// List the children of the archive file
				FileObject[] children = archive.getChildren()[0].getChildren();
				for (int i = 0; i < children.length; i++) {
					FileObject fo = children[i];
					if (fo.isReadable() && fo.getType() == FileType.FILE
							&& fo.getName().getExtension().equals("nxml")) {
						FileContent fc = fo.getContent();
						Article article = archiveArticleParser.parse(fc
								.getInputStream());

						NextArticle nextArticle = new NextArticle();
						nextArticle.article = article;
						nextArticle.file = f.getAbsolutePath();

						return nextArticle;
					}
				}
			} catch (Exception e) {
				LOG.error("Error extracting " + f.getAbsolutePath() + ", " + e);
			}
		}
		return null;
	}
 
Example 5
Source File: PubmedCentralCollectionReader.java    From bluima with Apache License 2.0 5 votes vote down vote up
private NextArticle getNextArticle() throws IOException {

        while (directoryIterator.hasNext()) {
            File f = directoryIterator.next();
            // LOG.debug("extracting " + f.getAbsolutePath());

            try {
                FileObject archive = fsManager.resolveFile("tgz:file://"
                        + f.getAbsolutePath());

                // List the children of the archive file
                FileObject[] children = archive.getChildren()[0].getChildren();
                for (int i = 0; i < children.length; i++) {
                    FileObject fo = children[i];
                    if (fo.isReadable() && fo.getType() == FileType.FILE
                            && fo.getName().getExtension().equals("nxml")) {
                        FileContent fc = fo.getContent();
                        Article article = archiveArticleParser.parse(fc
                                .getInputStream());

                        NextArticle nextArticle = new NextArticle();
                        nextArticle.article = article;
                        nextArticle.file = f.getAbsolutePath();

                        return nextArticle;
                    }
                }
            } catch (Exception e) {
                LOG.error("Error extracting " + f.getAbsolutePath() + ", " + e);
            }
        }
        return null;
    }
 
Example 6
Source File: ProviderReadTests.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
public void testGetContentInfo() throws Exception {
    final FileObject file = resolveFile1Txt();
    assertTrue(file.exists());
    final FileContent content = file.getContent();
    assertNotNull(content);
    final FileContentInfo contentInfo = content.getContentInfo();
    assertNotNull(contentInfo);
}
 
Example 7
Source File: Http5GetContentInfoTest.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Tests VFS-427 NPE on Http5FileObject.getContent().getContentInfo().
 *
 * @throws FileSystemException thrown when the getContentInfo API fails.
 */
@Test
public void testGetContentInfo() throws FileSystemException {
    final FileSystemManager fsManager = VFS.getManager();
    final FileObject fo = fsManager.resolveFile("http5://www.apache.org/licenses/LICENSE-2.0.txt");
    final FileContent content = fo.getContent();
    Assert.assertNotNull(content);
    // Used to NPE before fix:
    content.getContentInfo();
}
 
Example 8
Source File: AbstractProviderTestCase.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Asserts that the content of a file is the same as expected. Checks the length reported by getSize() is correct,
 * then reads the content as a byte stream and compares the result with the expected content. Assumes files are
 * encoded using UTF-8.
 */
protected void assertSameContent(final String expected, final FileObject file) throws Exception {
    // Check the file exists, and is a file
    assertTrue(file.exists());
    assertSame(FileType.FILE, file.getType());
    assertTrue(file.isFile());

    // Get file content as a binary stream
    final byte[] expectedBin = expected.getBytes("utf-8");

    // Check lengths
    final FileContent content = file.getContent();
    assertEquals("same content length", expectedBin.length, content.getSize());

    // Read content into byte array
    final InputStream instr = content.getInputStream();
    final ByteArrayOutputStream outstr;
    try {
        outstr = new ByteArrayOutputStream(expectedBin.length);
        final byte[] buffer = new byte[256];
        int nread = 0;
        while (nread >= 0) {
            outstr.write(buffer, 0, nread);
            nread = instr.read(buffer);
        }
    } finally {
        instr.close();
    }

    // Compare
    assertArrayEquals("same binary content", expectedBin, outstr.toByteArray());
}
 
Example 9
Source File: KettleVFS.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public static OutputStream getOutputStream( FileObject fileObject, boolean append ) throws IOException {
  FileObject parent = fileObject.getParent();
  if ( parent != null ) {
    if ( !parent.exists() ) {
      throw new IOException( BaseMessages.getString(
        PKG, "KettleVFS.Exception.ParentDirectoryDoesNotExist", getFriendlyURI( parent ) ) );
    }
  }
  try {
    fileObject.createFile();
    FileContent content = fileObject.getContent();
    return content.getOutputStream( append );
  } catch ( FileSystemException e ) {
    // Perhaps if it's a local file, we can retry using the standard
    // File object. This is because on Windows there is a bug in VFS.
    //
    if ( fileObject instanceof LocalFile ) {
      try {
        String filename = getFilename( fileObject );
        return new FileOutputStream( new File( filename ), append );
      } catch ( Exception e2 ) {
        throw e; // throw the original exception: hide the retry.
      }
    } else {
      throw e;
    }
  }
}
 
Example 10
Source File: DefaultURLStreamHandler.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
@Override
protected URLConnection openConnection(final URL url) throws IOException {
    final FileObject entry = context.resolveFile(url.toExternalForm(), fileSystemOptions);
    return new DefaultURLConnection(url, entry.getContent());
}
 
Example 11
Source File: LRUFilesCacheTests.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
public void testFilesCache() throws Exception {
    final FileObject scratchFolder = getWriteFolder();
    Assert.assertNotNull("scratchFolder", scratchFolder);

    // releaseable
    final FileObject dir1 = scratchFolder.resolveFile("dir1");

    // avoid cache removal
    final FileObject dir2 = scratchFolder.resolveFile("dir2");
    dir2.getContent();

    // releaseable
    @SuppressWarnings("unused")
    final FileObject dir3 = scratchFolder.resolveFile("dir3");

    // releaseable
    @SuppressWarnings("unused")
    final FileObject dir4 = scratchFolder.resolveFile("dir4");

    // releaseable
    @SuppressWarnings("unused")
    final FileObject dir5 = scratchFolder.resolveFile("dir5");

    // releaseable
    @SuppressWarnings("unused")
    final FileObject dir6 = scratchFolder.resolveFile("dir6");

    // releaseable
    @SuppressWarnings("unused")
    final FileObject dir7 = scratchFolder.resolveFile("dir7");

    // releaseable
    @SuppressWarnings("unused")
    final FileObject dir8 = scratchFolder.resolveFile("dir8");

    // check if the cache still holds the right instance
    final FileObject dir2_2 = scratchFolder.resolveFile("dir2");
    assertSame(dir2, dir2_2);

    // check if the cache still holds the right instance
    final FileObject dir1_2 = scratchFolder.resolveFile("dir1");
    assertNotSame(dir1, dir1_2);
}
 
Example 12
Source File: SSHData.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public static Connection OpenConnection( String serveur, int port, String username, String password,
    boolean useKey, String keyFilename, String passPhrase, int timeOut, VariableSpace space, String proxyhost,
    int proxyport, String proxyusername, String proxypassword ) throws KettleException {
  Connection conn = null;
  char[] content = null;
  boolean isAuthenticated = false;
  try {
    // perform some checks
    if ( useKey ) {
      if ( Utils.isEmpty( keyFilename ) ) {
        throw new KettleException( BaseMessages.getString( SSHMeta.PKG, "SSH.Error.PrivateKeyFileMissing" ) );
      }
      FileObject keyFileObject = KettleVFS.getFileObject( keyFilename );

      if ( !keyFileObject.exists() ) {
        throw new KettleException( BaseMessages.getString( SSHMeta.PKG, "SSH.Error.PrivateKeyNotExist", keyFilename ) );
      }

      FileContent keyFileContent = keyFileObject.getContent();

      CharArrayWriter charArrayWriter = new CharArrayWriter( (int) keyFileContent.getSize() );

      try ( InputStream in = keyFileContent.getInputStream() ) {
        IOUtils.copy( in, charArrayWriter );
      }

      content = charArrayWriter.toCharArray();
    }
    // Create a new connection
    conn = createConnection( serveur, port );

    /* We want to connect through a HTTP proxy */
    if ( !Utils.isEmpty( proxyhost ) ) {
      /* Now connect */
      // if the proxy requires basic authentication:
      if ( !Utils.isEmpty( proxyusername ) ) {
        conn.setProxyData( new HTTPProxyData( proxyhost, proxyport, proxyusername, proxypassword ) );
      } else {
        conn.setProxyData( new HTTPProxyData( proxyhost, proxyport ) );
      }
    }

    // and connect
    if ( timeOut == 0 ) {
      conn.connect();
    } else {
      conn.connect( null, 0, timeOut * 1000 );
    }
    // authenticate
    if ( useKey ) {
      isAuthenticated =
        conn.authenticateWithPublicKey( username, content, space.environmentSubstitute( passPhrase ) );
    } else {
      isAuthenticated = conn.authenticateWithPassword( username, password );
    }
    if ( isAuthenticated == false ) {
      throw new KettleException( BaseMessages.getString( SSHMeta.PKG, "SSH.Error.AuthenticationFailed", username ) );
    }
  } catch ( Exception e ) {
    // Something wrong happened
    // do not forget to disconnect if connected
    if ( conn != null ) {
      conn.close();
    }
    throw new KettleException( BaseMessages.getString( SSHMeta.PKG, "SSH.Error.ErrorConnecting", serveur, username ), e );
  }
  return conn;
}
 
Example 13
Source File: AbstractReader.java    From bluima with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        FileSystemManager fsManager = VFS.getManager();
        final Pattern pmidP = Pattern
                .compile("<PMID Version=\"\\d+\">(.*?)</PMID>");

        DirectoryIterator it = new DefaultDirectoryIterator();
        it.setDirectory(new File(
        // "/Volumes/HDD2/ren_scratch/Dropbox/dev_shared/tmp3/"));
                "/Volumes/simulation/nip/pubmed_gzip/medline14/"));
        it.setExtensionFilter("xml.gz");
        it.setRecursive(false);
        Iterator<File> fit = it.iterator();

        while (fit.hasNext()) {
            File f = fit.next();

            // "medline14n0456.xml.gz" --> 456
            int archiveId = parseInt(f.getName().substring(10, 14));
            if (archiveId > 755) {

                try {
                    FileObject archive = fsManager.resolveFile("gz:file://"
                            + f.getAbsolutePath());
                    FileObject fo = archive.getChildren()[0];
                    LOG.debug("extracted file {} from archive {}",
                            fo.getName(), f.getName());
                    if (fo.isReadable() && fo.getType() == FileType.FILE) {
                        FileContent fc = fo.getContent();

                        String articles = IOUtils.toString(fc.getInputStream(),
                                "UTF-8");
                        String[] split = articles.split("<MedlineCitation");

                        for (int i = 1; i < split.length - 1; i++) {

                            String article = "<MedlineCitation" + split[i];
                            if (!article.startsWith("<MedlineCitationSet>")) {

                                Matcher matcher = pmidP.matcher(article);
                                if (matcher.find()) {
                                    int pmid = Integer.parseInt(matcher
                                            .group(1));
                                    String filePath = StructuredDirectory
                                            .getFilePath(pmid, "xml");
                                    File file = new File(
                                    // "/Volumes/HDD2/ren_scratch/Dropbox/dev_shared/tmp3/xml/"
                                            "/Volumes/simulation/nip/pubmed_xml/"
                                                    + filePath);
                                    file.getParentFile().mkdirs();

                                    // System.out.println(pmid);
                                    FileOutputStream out = new FileOutputStream(
                                            file);
                                    IOUtils.write(article, out);
                                    IOUtils.closeQuietly(out);
                                } else {
                                    System.err
                                            .println("could not extract pmid from "
                                                    + article);
                                }
                            }
                        }
                    }
                } catch (Throwable e) {
                    LOG.error("error with " + f.getName(), e);
                }
            }
        }
    }
 
Example 14
Source File: ProviderWriteTests.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
/**
 * Tests file write to and from the same file system type
 */
public void testWriteSameFileSystem() throws Exception {
    final FileObject scratchFolder = createScratchFolder();

    // Create direct child of the test folder
    final FileObject fileSource = scratchFolder.resolveFile("file1.txt");
    assertFalse(fileSource.exists());

    // Create the source file
    final String expectedString = "Here is some sample content for the file.  Blah Blah Blah.";
    final OutputStream expectedOutputStream = fileSource.getContent().getOutputStream();
    try {
        expectedOutputStream.write(expectedString.getBytes("utf-8"));
    } finally {
        expectedOutputStream.close();
    }

    assertSameContent(expectedString, fileSource);

    // Make sure we can copy the new file to another file on the same filesystem
    final FileObject fileTarget = scratchFolder.resolveFile("file1copy.txt");
    assertFalse(fileTarget.exists());

    final FileContent contentSource = fileSource.getContent();
    //
    // Tests FileContent#write(FileContent)
    contentSource.write(fileTarget.getContent());
    assertSameContent(expectedString, fileTarget);
    //
    // Tests FileContent#write(OutputStream)
    OutputStream outputStream = fileTarget.getContent().getOutputStream();
    try {
        contentSource.write(outputStream);
    } finally {
        outputStream.close();
    }
    assertSameContent(expectedString, fileTarget);
    //
    // Tests FileContent#write(OutputStream, int)
    outputStream = fileTarget.getContent().getOutputStream();
    try {
        contentSource.write(outputStream, 1234);
    } finally {
        outputStream.close();
    }
    assertSameContent(expectedString, fileTarget);
}
 
Example 15
Source File: HopVfs.java    From hop with Apache License 2.0 4 votes vote down vote up
public static InputStream getInputStream( FileObject fileObject ) throws FileSystemException {
  FileContent content = fileObject.getContent();
  return content.getInputStream();
}
 
Example 16
Source File: KettleVFS.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public static InputStream getInputStream( FileObject fileObject ) throws FileSystemException {
  FileContent content = fileObject.getContent();
  return content.getInputStream();
}
 
Example 17
Source File: AgeFileFilter.java    From commons-vfs with Apache License 2.0 3 votes vote down vote up
/**
 * Tests if the specified {@code File} is newer than the specified time
 * reference.
 *
 * @param fileObject the {@code File} of which the modification date must
 *                   be compared, must not be {@code null}
 * @param timeMillis the time reference measured in milliseconds since the epoch
 *                   (00:00:00 GMT, January 1, 1970)
 * @return true if the {@code File} exists and has been modified after the
 *         given time reference.
 * @throws FileSystemException Thrown for file system errors.
 * @throws IllegalArgumentException if the file is {@code null}
 */
private static boolean isFileNewer(final FileObject fileObject, final long timeMillis) throws FileSystemException {
    if (fileObject == null) {
        throw new IllegalArgumentException("No specified file");
    }
    if (!fileObject.exists()) {
        return false;
    }
    try (final FileContent content = fileObject.getContent()) {
        final long lastModified = content.getLastModifiedTime();
        return lastModified > timeMillis;
    }
}
 
Example 18
Source File: FileObjectUtils.java    From commons-vfs with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the content of a file object, as a byte array.
 *
 * @param file Gets the contents of this file object.
 * @return The content as a byte array.
 * @throws IOException if the file content cannot be accessed.
 *
 * @since 2.6.0
 */
public static byte[] getContentAsByteArray(final FileObject file) throws IOException {
    try (final FileContent content = file.getContent()) {
        return content.getByteArray();
    }
}
 
Example 19
Source File: FileObjectUtils.java    From commons-vfs with Apache License 2.0 2 votes vote down vote up
/**
 * Writes the content of a file to an OutputStream.
 *
 * @param file The FileObject to write.
 * @param output The OutputStream to write to.
 * @throws IOException if an error occurs writing the file.
 * @see FileContent#write(OutputStream)
 * @since 2.6.0
 */
public static void writeContent(final FileObject file, final OutputStream output) throws IOException {
    try (final FileContent content = file.getContent()) {
        content.write(output);
    }
}
 
Example 20
Source File: FileObjectUtils.java    From commons-vfs with Apache License 2.0 2 votes vote down vote up
/**
 * Writes the content from a source file to a destination file.
 *
 * @param srcFile The source FileObject.
 * @param destFile The target FileObject
 * @throws IOException If an error occurs copying the file.
 * @see FileContent#write(FileObject)
 * @since 2.6.0
 */
public static void writeContent(final FileObject srcFile, final FileObject destFile) throws IOException {
    try (final FileContent content = srcFile.getContent()) {
        content.write(destFile);
    }
}