Example usage for org.apache.commons.vfs FileContent getOutputStream

List of usage examples for org.apache.commons.vfs FileContent getOutputStream

Introduction

In this page you can find the example usage for org.apache.commons.vfs FileContent getOutputStream.

Prototype

OutputStream getOutputStream() throws FileSystemException;

Source Link

Document

Returns an output stream for writing the file's content.

Usage

From source file:com.qmetric.document.watermark.DefaultPdfStamperFactory.java

@Override
public PdfStamper newPdfStamper(final PdfReader pdfReader, final FileContent fileContent) {
    try {//  w w  w.  j  a v  a 2 s  . c om
        final PdfStamper pdf = new PdfStamper(pdfReader, fileContent.getOutputStream());

        if (pdfReader.isEncrypted()) {
            pdf.setEncryption(null, ownerPassword, pdfReader.getPermissions(), pdfReader.getCryptoMode());
        }

        return pdf;
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    } catch (com.itextpdf.text.DocumentException e) {
        throw new RuntimeException(e);
    }
}

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

/**
 *  ?  ??  ? ? ./*from ww  w.j  a v a 2  s.c o m*/
 * ?    ? ?    ?? ?.
 * @throws Exception
 */
@Test
public void testAccessFile() throws Exception {

    FileSystemManager manager = VFS.getManager();

    FileObject baseDir = manager.resolveFile(System.getProperty("user.dir"));
    FileObject file = manager.resolveFile(baseDir, "testfolder/file1.txt");

    //  ? 
    file.delete(Selectors.SELECT_FILES);
    assertFalse(file.exists());

    // ? ?
    file.createFile();
    assertTrue(file.exists());

    FileContent fileContent = file.getContent();
    assertEquals(0, fileContent.getSize());

    // ? 
    String string = "test.";
    OutputStream os = fileContent.getOutputStream();

    try {
        os.write(string.getBytes());
        os.flush();
    } catch (Exception e) {
        throw new Exception(e);
    } finally {
        os.close();
    }
    assertNotSame(0, fileContent.getSize());

    // ? ?
    StringBuffer sb = new StringBuffer();
    FileObject writtenFile = manager.resolveFile(baseDir, "testfolder/file1.txt");
    FileContent writtenContents = writtenFile.getContent();
    InputStream is = writtenContents.getInputStream();

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));

        for (String line = ""; (line = reader.readLine()) != null; sb.append(line))
            ;

    } catch (Exception e) {
        throw new Exception(e);
    } finally {
        is.close();
    }

    // ? ?
    assertEquals(sb.toString(), string);
}

From source file:org.jclouds.vfs.provider.blobstore.test.BlobStoreProviderTestCase.java

private void writeFile(FileObject base, String name, String value) throws FileSystemException, IOException {
    FileObject file = base.resolveFile(name);
    FileContent content = file.getContent();
    CharStreams.write(value, CharStreams
            .newWriterSupplier(Utils.newOutputStreamSupplier(content.getOutputStream()), Charsets.UTF_8));
    content.close();/* ww w  .  j av a2 s.  com*/
}

From source file:org.kalypso.simulation.grid.GridJobSubmitter.java

public void submitJob(final FileObject workingDir, final String executable, ISimulationMonitor monitor,
        String... arguments) throws SimulationException {
    if (monitor == null) {
        monitor = new NullSimulationMonitor();
    }/*w w  w .  ja va2  s .  c  o m*/

    // prepare streams
    FileObject stdoutFile = null;
    FileObject stderrFile = null;
    int returnCode = IStatus.ERROR;
    try {
        // stream stdout and stderr to files
        stdoutFile = workingDir.resolveFile("stdout");
        stderrFile = workingDir.resolveFile("stderr");

        // create process handle
        final String processFactoryId = "org.kalypso.simulation.gridprocess";
        // TODO: refactor so tempdir is created inside process
        final String tempDirName = workingDir.getName().getBaseName();
        final IProcess process = KalypsoCommonsExtensions.createProcess(processFactoryId, tempDirName,
                executable, arguments);
        // process.setProgressMonitor( new SimulationMonitorAdaptor( monitor ) );
        process.environment().put("OMP_NUM_THREADS", "4");

        final FileContent stdOutContent = stdoutFile.getContent();
        final OutputStream stdOut = stdOutContent.getOutputStream();
        final FileContent stdErrContent = stderrFile.getContent();
        final OutputStream stdErr = stdErrContent.getOutputStream();

        // stage-in files
        final FileSystemManager manager = workingDir.getFileSystem().getFileSystemManager();
        for (final URI einput : m_externalInputs.keySet()) {
            final FileObject inputFile = manager.resolveFile(getUriAsString(einput));
            final String destName = m_externalInputs.get(einput);
            if (destName != null) {
                final FileObject destFile = workingDir.resolveFile(destName);
                VFSUtil.copy(inputFile, destFile, null, true);
            } else {
                VFSUtil.copy(inputFile, workingDir, null, true);
            }
        }

        // start process
        returnCode = process.startProcess(stdOut, stdErr, null, null);
    } catch (final CoreException e) {
        // when process cannot be created
        throw new SimulationException("Could not create process.", e);
    } catch (final ProcessTimeoutException e) {
        e.printStackTrace();
    } catch (final FileNotFoundException e) {
        // can only happen when files cannot be created in tmpdir
        throw new SimulationException("Could not create temporary files for stdout and stderr.", e);
    } catch (final IOException e) {
        throw new SimulationException("Process I/O error.", e);
    } finally {
        // close files
        if (stdoutFile != null) {
            try {
                stdoutFile.getContent().close();
            } catch (final FileSystemException e) {
                // gobble
            }
        }
        if (stderrFile != null) {
            try {
                stderrFile.getContent().close();
            } catch (final FileSystemException e) {
                // gobble
            }
        }
    }

    // process failure handling
    if (returnCode != IStatus.OK) {
        String errString = "Process failed.";
        try {
            final FileContent content = stderrFile.getContent();
            final InputStream input2 = content.getInputStream();
            errString = errString + "\n" + IOUtils.toString(input2);
            content.close();
        } catch (final IOException e) {
            // ignore
        }
        monitor.setFinishInfo(returnCode, errString);
        throw new SimulationException(errString);
    } else {
        monitor.setFinishInfo(IStatus.OK, "Process finished successfully.");
    }
}

From source file:org.pentaho.platform.plugin.outputs.ApacheVFSOutputHandler.java

@Override
public IContentItem getFileOutputContentItem() {

    String contentRef = getContentRef();
    try {// w  w  w .j a  va2  s .c  o m
        String contentName = getHandlerId().substring(4) + ":" + contentRef; //$NON-NLS-1$
        FileSystemManager fsManager = VFS.getManager();
        if (fsManager == null) {
            Logger.error(ApacheVFSOutputHandler.class.getName(),
                    Messages.getInstance().getString("ApacheVFSOutputHandler.ERROR_0001_CANNOT_GET_VFSMGR")); //$NON-NLS-1$
            return null;
        }
        FileObject file = fsManager.resolveFile(contentName);
        if (file == null) {
            Logger.error(ApacheVFSOutputHandler.class.getName(), Messages.getInstance()
                    .getString("ApacheVFSOutputHandler.ERROR_0002_CANNOT_GET_VF", contentName)); //$NON-NLS-1$
            return null;
        }
        if (!file.isWriteable()) {
            Logger.error(ApacheVFSOutputHandler.class.getName(), Messages.getInstance()
                    .getString("ApacheVFSOutputHandler.ERROR_0003_CANNOT_WRITE", contentName)); //$NON-NLS-1$
            return null;
        }
        FileContent fileContent = file.getContent();
        if (fileContent == null) {
            Logger.error(ApacheVFSOutputHandler.class.getName(), Messages.getInstance()
                    .getString("ApacheVFSOutputHandler.ERROR_0004_CANNOT_GET_CTX", contentName)); //$NON-NLS-1$
            return null;
        }
        OutputStream outputStream = fileContent.getOutputStream();

        SimpleContentItem content = new SimpleContentItem(outputStream);
        return content;
    } catch (Throwable t) {
        Logger.error(ApacheVFSOutputHandler.class.getName(), Messages.getInstance()
                .getString("ApacheVFSOutputHandler.ERROR_0005_CANNOT_GET_HANDLER", contentRef), t); //$NON-NLS-1$
    }

    return null;
}

From source file:unitTests.dataspaces.AbstractLimitingFileObjectTest.java

@Test
public void testReadOnlyGetContentOutputStream() throws IOException {
    createRealFile();/*  w  w w  .j  ava  2  s  .co  m*/

    final FileContent content = readOnlyFile.getContent();
    try {
        content.getOutputStream();
        fail("Expected exception");
    } catch (FileSystemException x) {
    } finally {
        content.close();
    }
}

From source file:unitTests.dataspaces.AbstractLimitingFileObjectTest.java

@Test
public void testReadWriteGetContentOutputStream() throws IOException {
    createRealFile();/*from  www  . jav  a2 s.c  o m*/

    final FileContent content = readWriteFile.getContent();
    try {
        content.getOutputStream();
    } finally {
        content.close();
    }
}