Example usage for org.apache.commons.vfs2 FileObject getContent

List of usage examples for org.apache.commons.vfs2 FileObject getContent

Introduction

In this page you can find the example usage for org.apache.commons.vfs2 FileObject getContent.

Prototype

FileContent getContent() throws FileSystemException;

Source Link

Document

Returns this file's content.

Usage

From source file:ShowProperties.java

public static void main(String[] args) throws FileSystemException {
    if (args.length == 0) {
        System.err.println("Please pass the name of a file as parameter.");
        System.err.println("e.g. java org.apache.commons.vfs2.example.ShowProperties LICENSE.txt");
        return;/*from  w ww  .  j a  v  a 2 s  .  c  o  m*/
    }
    for (int i = 0; i < args.length; i++) {
        try {
            FileSystemManager mgr = VFS.getManager();
            System.out.println();
            System.out.println("Parsing: " + args[i]);
            FileObject file = mgr.resolveFile(args[i]);
            System.out.println("URL: " + file.getURL());
            System.out.println("getName(): " + file.getName());
            System.out.println("BaseName: " + file.getName().getBaseName());
            System.out.println("Extension: " + file.getName().getExtension());
            System.out.println("Path: " + file.getName().getPath());
            System.out.println("Scheme: " + file.getName().getScheme());
            System.out.println("URI: " + file.getName().getURI());
            System.out.println("Root URI: " + file.getName().getRootURI());
            System.out.println("Parent: " + file.getName().getParent());
            System.out.println("Type: " + file.getType());
            System.out.println("Exists: " + file.exists());
            System.out.println("Readable: " + file.isReadable());
            System.out.println("Writeable: " + file.isWriteable());
            System.out.println("Root path: " + file.getFileSystem().getRoot().getName().getPath());
            if (file.exists()) {
                if (file.getType().equals(FileType.FILE)) {
                    System.out.println("Size: " + file.getContent().getSize() + " bytes");
                } else if (file.getType().equals(FileType.FOLDER) && file.isReadable()) {
                    FileObject[] children = file.getChildren();
                    System.out.println("Directory with " + children.length + " files");
                    for (int iterChildren = 0; iterChildren < children.length; iterChildren++) {
                        System.out.println("#" + iterChildren + ": " + children[iterChildren].getName());
                        if (iterChildren > 5) {
                            break;
                        }
                    }
                }
                System.out.println("Last modified: "
                        + DateFormat.getInstance().format(new Date(file.getContent().getLastModifiedTime())));
            } else {
                System.out.println("The file does not exist");
            }
            file.close();
        } catch (FileSystemException ex) {
            ex.printStackTrace();
        }
    }
}

From source file:hadoopInstaller.io.MD5Calculator.java

/**
 * /*from w ww.j  ava2 s . c  o  m*/
 * @param file
 *            the file object to calculate md5 for. (single file, not
 *            folder)
 * @return a string with the calculated md5 in lowercase.
 * @throws NoSuchAlgorithmException
 * @throws {@link
 *             FileSystemException}
 * @throws IOException
 */
public static String calculateFor(FileObject file) throws NoSuchAlgorithmException, IOException {
    try (InputStream is = file.getContent().getInputStream();
            DigestInputStream dis = new DigestInputStream(is, MessageDigest.getInstance("MD5"));) { //$NON-NLS-1$
        byte[] in = new byte[1024];
        while ((dis.read(in)) > 0) {
            // Read until there's nothing left.
        }
        return javax.xml.bind.DatatypeConverter.printHexBinary(dis.getMessageDigest().digest()).toLowerCase();
    }
}

From source file:com.google.code.docbook4j.XSLTUtils.java

public static final String toBase64(String baseDir, String location) {

    try {//from  w ww .j  a  va2s.  c  om

        FileObject fo = FileObjectUtils.resolveFile(location, baseDir);

        byte[] data = IOUtils.toByteArray(fo.getContent().getInputStream());

        StringBuffer sb = new StringBuffer();
        sb.append("data:");
        sb.append(determineMimeType(location));
        sb.append(";base64,");
        sb.append(Base64.encode(data));

        fo.close();
        return sb.toString();

    } catch (Exception e) {
        log.error("Error reading image file: " + location, e);
    }

    return location;
}

From source file:fi.mystes.synapse.mediator.vfs.VFSTestHelper.java

public static TestFile createFile(String path, String content) throws IOException {
    FileSystemManager fsManager = VFS.getManager();
    FileObject file = fsManager.resolveFile(path);
    file.getContent().getOutputStream().write(content.getBytes());
    file.getContent().close();/* w  w  w  .  j  a  va  2 s .  c  o m*/
    return new TestFileImpl(path, content, file.getContent().getSize());
}

From source file:de.innovationgate.utils.XStreamUtils.java

/**
 * Loads an object from a UTF-8 encoded VFS file object
 * @param xstream The XStream instance to use
 * @param file The file to read from/*from  w  w w .j  a  v a 2  s .  c  om*/
 * @return The loaded object
 * @throws IOException
 */
public static Object loadUtf8FromFileObject(XStream xstream, FileObject file) throws IOException {
    InputStream in = file.getContent().getInputStream();
    return loadUtf8FromInputStream(xstream, in, true);
}

From source file:de.innovationgate.utils.XStreamUtils.java

/**
 * Serializes an object to a UTF-8 encoded VFS file object
 * @param obj The object to write//w w  w  .  jav  a2s.c  om
 * @param xstream The XStream instance to use
 * @param file The file to write
 * @throws IOException
 */
public static void writeUtf8ToFileObject(Object obj, XStream xstream, FileObject file) throws IOException {
    OutputStream out = file.getContent().getOutputStream();
    writeUtf8ToOutputStream(obj, xstream, out);
}

From source file:de.innovationgate.wgpublisher.design.OverlayData.java

public static OverlayData load(FileObject overlayDataFile) throws Exception {
    InputStream in = new BufferedInputStream(overlayDataFile.getContent().getInputStream());
    OverlayData overlayData = OverlayData.read(in);
    in.close();// w  w w.j  a v  a2s  .c o  m
    return overlayData;

}

From source file:fi.mystes.synapse.mediator.vfs.VFSTestHelper.java

public static void assertFileContentEquals(String path, String expectedContent) throws IOException {
    FileObject file = VFS.getManager().resolveFile(path);
    InputStream in = file.getContent().getInputStream();
    int length = 0;
    byte[] buffer = new byte[256];
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    while ((length = in.read(buffer)) > 0) {
        out.write(buffer, 0, length);//from w w w  .  j  ava  2  s .  c o  m
    }

    assertEquals("Unexpected file content", expectedContent, out.toString());
}

From source file:com.google.code.docbook4j.XSLTUtils.java

public static final String dumpCss(String baseDir, String location) {

    try {/*from w ww.  j a  v  a 2 s  .  c o  m*/

        FileObject fo = FileObjectUtils.resolveFile(location, baseDir);

        StringBuffer sb = new StringBuffer();
        sb.append("<!--\n");
        sb.append(IOUtils.toString(fo.getContent().getInputStream())).append("\n");
        sb.append("-->\n");

        fo.close();

        return sb.toString();

    } catch (Exception e) {
        log.error("Error reading css file: " + location, e);
    }

    return "";

}

From source file:fulcrum.xml.ParserTest.java

private static InputStream getInputStream(String location) {
    InputStream is = null;//from ww w . j a va 2s  .  c o m
    try {
        FileSystemManager fsManager = VFS.getManager();
        FileObject fileObj = fsManager.resolveFile(location);
        if (fileObj != null && fileObj.exists() && fileObj.isReadable()) {
            FileContent content = fileObj.getContent();
            is = content.getInputStream();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return is;
}