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

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

Introduction

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

Prototype

public FileContent getContent() throws FileSystemException;

Source Link

Document

Returns this file's content.

Usage

From source file:net.sf.vfsjfilechooser.utils.VFSUtils.java

/**
 * Returns a buffered output stream from a file
 * @param fileObject A file object//from  ww w .j  a v a  2 s .  c o  m
 * @return an OutputStream from the file object
 * @throws FileSystemException An exception while getting the file
 */
public static OutputStream getOutputStream(FileObject fileObject) throws FileSystemException {
    return new BufferedOutputStream(fileObject.getContent().getOutputStream());
}

From source file:com.nary.Debug.java

/**
 * This method read a specified class object from a specifed file
 * and return this object./*from w  w  w  . j  a va2 s  .c o  m*/
 *
 * @param _filename the spcifed file
 */

public static Object loadClass(FileObject fileobject) {
    InputStream fis = null;
    try {
        fis = fileobject.getContent().getInputStream();
        return loadClass(fis, false);
    } catch (Exception E) {
        return null;
    } finally {
        try {
            if (fis != null)
                fis.close();
        } catch (Exception ignored) {
        }
    }
}

From source file:com.panet.imeta.core.vfs.KettleVFS.java

public static OutputStream getOutputStream(FileObject fileObject, boolean append) throws IOException {
    FileObject parent = fileObject.getParent();
    if (parent != null) {
        if (!parent.exists()) {
            throw new IOException(
                    Messages.getString("KettleVFS.Exception.ParentDirectoryDoesNotExist", getFilename(parent)));
        }/*www.  jav a2s.c  o m*/
    }
    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;
        }
    }
}

From source file:egovframework.rte.fdl.filehandling.EgovFileUtil.java

/**
 * <p>//from  www.  j av a  2  s . c  o  m
 * ? ? ? ?? .
 * </p>
 * @param srcFile
 *        <code>FileObject</code>
 * @param destFile
 *        <code>FileObject</code>
 * @throws IOException
 */
public static void copyContent(final FileObject srcFile, final FileObject destFile) throws IOException {
    final OutputStream outstr = destFile.getContent().getOutputStream();
    try {
        writeContent(srcFile, outstr);
    } finally {
        outstr.close();
    }
}

From source file:egovframework.rte.fdl.filehandling.EgovFileUtil.java

/**
 * <p>//from   w  w  w  .j  a  v a2s . co m
 * byte ?? ? ?.
 * <p>
 * @param file
 *        <code>FileObject</code>
 * @return ? 
 * @throws IOException
 */
public static byte[] getContent(final FileObject file) throws IOException {
    final FileContent content = file.getContent();
    final int size = (int) content.getSize();
    final byte[] buf = new byte[size];

    final InputStream in = content.getInputStream();
    try {
        int read = 0;
        for (int pos = 0; pos < size && read >= 0; pos += read) {
            read = in.read(buf, pos, size - pos);
        }
    } finally {
        in.close();
    }

    return buf;
}

From source file:egovframework.rte.fdl.filehandling.EgovFileUtil.java

/**
 * <p>//  w  ww . ja  va2 s.  c o  m
 * ? ?? OutputStream  .
 * </p>
 * @param file
 * @param outstr
 * @throws IOException
 */
public static void writeContent(final FileObject file, final OutputStream outstr) throws IOException {
    final InputStream instr = file.getContent().getInputStream();
    try {
        final byte[] buffer = new byte[1024];
        while (true) {
            final int nread = instr.read(buffer);
            if (nread < 0) {
                break;
            }
            outstr.write(buffer, 0, nread);
        }
    } finally {
        instr.close();
    }
}

From source file:egovframework.rte.fdl.filehandling.EgovFileUtil.java

/**
 * <p>/* ww  w .j a v  a2  s  .c  om*/
 * ?? ?  ? .
 * </p>
 * @param filepath
 *        <code>String</code>
 * @return
 * @throws Exception
 */
public static long touch(final String filepath) throws Exception {

    long currentTime = 0;
    final FileObject file = manager.resolveFile(basefile, filepath);

    if (!file.exists()) {
        file.createFile();
    }

    file.getContent().setLastModifiedTime(currentTime = System.currentTimeMillis());

    return currentTime;
}

From source file:com.newatlanta.appengine.junit.vfs.gae.GaeVfsTestCase.java

public static void assertEquals(File file, FileObject fileObject) throws Exception {
    assertEqualPaths(file, fileObject);/*  ww w . ja v a 2  s  . c om*/
    assertEquals(file.canRead(), fileObject.isReadable());
    assertEquals(file.canWrite(), fileObject.isWriteable());
    assertEquals(file.exists(), fileObject.exists());
    if (file.getParentFile() == null) {
        assertNull(fileObject.getParent());
    } else {
        assertEqualPaths(file.getParentFile(), fileObject.getParent());
    }
    assertEquals(file.isDirectory(), fileObject.getType().hasChildren());
    assertEquals(file.isFile(), fileObject.getType().hasContent());
    assertEquals(file.isHidden(), fileObject.isHidden());
    if (file.isFile()) {
        assertEquals(file.length(), fileObject.getContent().getSize());
    }
    if (file.isDirectory()) { // same children
        File[] childFiles = file.listFiles();
        FileObject[] childObjects = fileObject.getChildren();
        assertEquals(childFiles.length, childObjects.length);
        for (int i = 0; i < childFiles.length; i++) {
            assertEqualPaths(childFiles[i], childObjects[i]);
        }
    }
}

From source file:com.thinkberg.moxo.vfs.s3.S3FileProviderTest.java

public void testCreateFile() throws IOException {
    FileObject object = VFS.getManager().resolveFile(ROOT + "/newfile.txt");
    object.getContent().getOutputStream().write(0xfc);
    object.close();/*from w  w w  . jav  a 2  s. com*/
}

From source file:be.ibridge.kettle.core.XMLHandler.java

/**
 * Load a file into an XML document/*from w  w  w .  j a  va2s.  c  om*/
 * @param filename The filename to load into a document
 * @return the Document if all went well, null if an error occured!
 */
public static final Document loadXMLFile(FileObject fileObject) throws KettleXMLException {
    DocumentBuilderFactory dbf;
    DocumentBuilder db;
    Document doc;

    try {
        // Check and open XML document
        dbf = DocumentBuilderFactory.newInstance();
        db = dbf.newDocumentBuilder();
        try {
            doc = db.parse(fileObject.getContent().getInputStream());
        } catch (FileNotFoundException ef) {
            throw new KettleXMLException(ef);
        }

        return doc;
    } catch (Exception e) {
        throw new KettleXMLException("Error reading information from file", e);
    }
}