Example usage for org.apache.commons.vfs FileContentInfo getContentEncoding

List of usage examples for org.apache.commons.vfs FileContentInfo getContentEncoding

Introduction

In this page you can find the example usage for org.apache.commons.vfs FileContentInfo getContentEncoding.

Prototype

public String getContentEncoding();

Source Link

Document

the content encoding

Usage

From source file:org.sonatype.gshell.commands.text.CatCommand.java

public Object execute(final CommandContext context) throws Exception {
    assert context != null;
    IO io = context.getIo();/*from w ww .j a  v  a2  s  . c o  m*/

    //
    // TODO: Support multi-path cat, and the special '-' token (which is the default if no paths are given)
    //

    FileObject file = resolveFile(context, path);

    new FileObjectAssert(file).exists();
    ensureFileHasContent(file);

    org.apache.commons.vfs.FileContent content = file.getContent();
    FileContentInfo info = content.getContentInfo();
    log.debug("Content type: {}", info.getContentType());
    log.debug("Content encoding: {}", info.getContentEncoding());

    //
    // TODO: Only cat files which we think are text, or warn if its not, allow flag to force
    //

    log.debug("Displaying file: {}", file.getName());

    BufferedReader reader = new BufferedReader(new InputStreamReader(content.getInputStream()));
    try {
        cat(reader, io);
    } finally {
        Closer.close(reader);
    }

    FileObjects.close(file);

    return Result.SUCCESS;
}

From source file:org.sonatype.gshell.commands.vfs.FileInfoCommand.java

public Object execute(final CommandContext context) throws Exception {
    assert context != null;
    IO io = context.getIo();// w w  w  .  j a  v  a  2s .c o m

    FileObject file = resolveFile(context, path);

    io.println("URL: {}", file.getURL());
    io.println("Name: {}", file.getName());
    io.println("BaseName: {}", file.getName().getBaseName());
    io.println("Extension: {}", file.getName().getExtension());
    io.println("Path: {}", file.getName().getPath());
    io.println("Scheme: {}", file.getName().getScheme());
    io.println("URI: {}", file.getName().getURI());
    io.println("Root URI: {}", file.getName().getRootURI());
    io.println("Parent: {}", file.getName().getParent());
    io.println("Type: {}", file.getType());
    io.println("Exists: {}", file.exists());
    io.println("Readable: {}", file.isReadable());
    io.println("Writeable: {}", file.isWriteable());
    io.println("Root path: {}", file.getFileSystem().getRoot().getName().getPath());

    if (file.exists()) {
        FileContent content = file.getContent();
        FileContentInfo contentInfo = content.getContentInfo();
        io.println("Content type: {}", contentInfo.getContentType());
        io.println("Content encoding: {}", contentInfo.getContentEncoding());

        try {
            // noinspection unchecked
            Map<String, Object> attrs = content.getAttributes();
            if (attrs != null && !attrs.isEmpty()) {
                io.println("Attributes:");
                for (Map.Entry<String, Object> entry : attrs.entrySet()) {
                    io.println("    {}='{}'", entry.getKey(), entry.getValue());
                }
            }
        } catch (FileSystemException e) {
            io.println("File attributes are NOT supported");
        }

        try {
            Certificate[] certs = content.getCertificates();
            if (certs != null && certs.length != 0) {
                io.println("Certificate:");
                for (Certificate cert : certs) {
                    io.println("    {}", cert);
                }
            }
        } catch (FileSystemException e) {
            io.println("File certificates are NOT supported");
        }

        if (file.getType().equals(FileType.FILE)) {
            io.println("Size: {} bytes", content.getSize());
        } else if (file.getType().hasChildren() && file.isReadable()) {
            FileObject[] children = file.getChildren();
            io.println("Directory with {} files", children.length);

            for (int iterChildren = 0; iterChildren < children.length; iterChildren++) {
                io.println("#{}:{}", iterChildren, children[iterChildren].getName());
                if (iterChildren > 5) {
                    break;
                }
            }
        }

        io.println("Last modified: {}",
                DateFormat.getInstance().format(new Date(content.getLastModifiedTime())));
    } else {
        io.println("The file does not exist");
    }

    FileObjects.close(file);

    return Result.SUCCESS;
}