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

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

Introduction

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

Prototype

OutputStream getOutputStream(boolean bAppend) throws FileSystemException;

Source Link

Document

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

Usage

From source file:org.datacleaner.util.VfsResource.java

@Override
public OutputStream append() throws ResourceException {
    try {/*from w  ww .ja  v  a2s . c o  m*/
        final FileContent content = _fileObject.getContent();
        final OutputStream out = content.getOutputStream(true);
        return out;
    } catch (Exception e) {
        throw new ResourceException(this, e);
    }
}

From source file:org.efaps.db.store.VFSStoreResource.java

/**
 * The method writes the context (from the input stream) to a temporary file
 * (same file URL, but with extension {@link #EXTENSION_TEMP}).
 *
 * @param _in   input stream defined the content of the file
 * @param _size length of the content (or negative meaning that the length
 *              is not known; then the content gets the length of readable
 *              bytes from the input stream)
 * @param _fileName name of the file/*from ww  w  .  j a  va  2  s.  c  o  m*/
 * @return size of the created temporary file object
 * @throws EFapsException on error
 */
@Override
public long write(final InputStream _in, final long _size, final String _fileName) throws EFapsException {
    try {
        long size = _size;
        final FileObject tmpFile = this.manager.resolveFile(this.manager.getBaseFile(),
                this.storeFileName + VFSStoreResource.EXTENSION_TEMP);
        if (!tmpFile.exists()) {
            tmpFile.createFile();
        }
        final FileContent content = tmpFile.getContent();
        OutputStream out = content.getOutputStream(false);
        if (getCompress().equals(Compress.GZIP)) {
            out = new GZIPOutputStream(out);
        } else if (getCompress().equals(Compress.ZIP)) {
            out = new ZipOutputStream(out);
        }

        // if size is unkown!
        if (_size < 0) {
            int length = 1;
            size = 0;
            while (length > 0) {
                length = _in.read(this.buffer);
                if (length > 0) {
                    out.write(this.buffer, 0, length);
                    size += length;
                }
            }
        } else {
            Long length = _size;
            while (length > 0) {
                final int readLength = length.intValue() < this.buffer.length ? length.intValue()
                        : this.buffer.length;
                _in.read(this.buffer, 0, readLength);
                out.write(this.buffer, 0, readLength);
                length -= readLength;
            }
        }
        if (getCompress().equals(Compress.GZIP) || getCompress().equals(Compress.ZIP)) {
            out.close();
        }
        tmpFile.close();
        setFileInfo(_fileName, size);
        return size;
    } catch (final IOException e) {
        VFSStoreResource.LOG.error("write of content failed", e);
        throw new EFapsException(VFSStoreResource.class, "write.IOException", e);
    }

}

From source file:org.eobjects.analyzer.util.VfsResource.java

@Override
public void append(Action<OutputStream> appendCallback) throws ResourceException {
    try {//from   w  w w  .  j a  v a2  s. c o m
        FileContent content = _fileObject.getContent();
        OutputStream out = content.getOutputStream(true);
        try {
            appendCallback.run(out);
        } finally {
            FileHelper.safeClose(out);
        }
    } catch (Exception e) {
        throw new ResourceException(this, e);
    }
}

From source file:org.pentaho.di.core.logging.Log4jFileAppenderTest.java

@Before
public void before() throws IOException {
    outputStream = mock(OutputStream.class);
    FileContent fileContent = mock(FileContent.class);
    when(fileContent.getOutputStream(anyBoolean())).thenReturn(outputStream);
    FileObject file = mock(FileObject.class);
    when(file.getContent()).thenReturn(fileContent);
    log4jFileAppender = new Log4jFileAppender(file);
}

From source file:org.wso2.carbon.connector.FileCreate.java

/**
 * Create a file with Apache commons/* ww w .  j  av a2 s .com*/
 *
 * @param source         Location of the file/folder
 * @param content        Content in a file
 * @param encoding       Encoding type
 * @param messageContext The message context that is generated for processing the file
 * @return Return the status
 */
private boolean createFile(String source, String content, String encoding, MessageContext messageContext) {
    boolean resultStatus = false;
    StandardFileSystemManager manager;
    try {
        OutputStream out = null;
        manager = FileConnectorUtils.getManager();
        if (manager != null) {
            FileObject sourceFile = manager.resolveFile(source, FileConnectorUtils.init(messageContext));
            try {
                if (FileConnectorUtils.isFolder(sourceFile)) {
                    sourceFile.createFolder();
                } else {
                    if (StringUtils.isEmpty(content)) {
                        sourceFile.createFile();
                    } else {
                        FileContent fileContent = sourceFile.getContent();
                        out = fileContent.getOutputStream(true);
                        if (StringUtils.isEmpty(encoding)) {
                            IOUtils.write(content, out, DEFAULT_ENCODING);
                        } else {
                            IOUtils.write(content, out, encoding);
                        }
                    }
                }
                resultStatus = true;
                if (log.isDebugEnabled()) {
                    log.debug("File creation completed with " + source);
                }
            } finally {
                try {
                    if (sourceFile != null) {
                        sourceFile.close();
                    }
                } catch (FileSystemException e) {
                    log.error("Error while closing FileObject: " + e.getMessage(), e);
                }
                try {
                    if (out != null) {
                        out.close();
                    }
                } catch (IOException e) {
                    log.error("Error while closing OutputStream: " + e.getMessage(), e);
                }
                manager.close();
            }
        }
    } catch (IOException e) {
        handleException("Unable to create a file/folder.", e, messageContext);
    }
    return resultStatus;
}