use of org.apache.commons.vfs2.FileContent in project mycore by MyCoRe-Org.
the class MCRCStoreVFS method doStoreContent.
@Override
protected String doStoreContent(MCRFile file, MCRContentInputStream source) throws Exception {
StringBuilder storageId = new StringBuilder();
String[] slots = buildSlotPath();
// Recursively create directory name
for (String slot : slots) {
storageId.append(slot).append("/");
}
String fileId = buildNextID(file);
storageId.append(fileId);
FileObject targetObject = fsManager.resolveFile(getBase(), storageId.toString());
FileContent targetContent = targetObject.getContent();
try (OutputStream out = targetContent.getOutputStream()) {
IOUtils.copy(source, out);
} finally {
targetContent.close();
}
return storageId.toString();
}
use of org.apache.commons.vfs2.FileContent in project ant-ivy by apache.
the class VfsRepository method get.
/**
* Transfer a VFS Resource from the repository to the local file system.
*
* @param srcVfsURI
* a <code>String</code> identifying the VFS resource to be fetched
* @param destination
* a <code>File</code> identifying the destination file
* @throws IOException on failure
* @see "Supported File Systems in the jakarta-commons-vfs documentation"
*/
public void get(String srcVfsURI, File destination) throws IOException {
VfsResource src = new VfsResource(srcVfsURI, getVFSManager());
fireTransferInitiated(src, TransferEvent.REQUEST_GET);
try {
FileContent content = src.getContent();
if (content == null) {
throw new IllegalArgumentException("invalid vfs uri " + srcVfsURI + ": no content found");
}
FileUtil.copy(content.getInputStream(), destination, progress);
} catch (IOException | RuntimeException ex) {
fireTransferError(ex);
throw ex;
}
}
use of org.apache.commons.vfs2.FileContent in project pentaho-kettle by pentaho.
the class TextFileInputTest method testClose.
@Test
public void testClose() throws Exception {
TextFileInputMeta mockTFIM = createMetaObject(null);
String virtualFile = createVirtualFile("pdi-17267.txt", null);
TextFileInputData mockTFID = createDataObject(virtualFile, ";", null);
mockTFID.lineBuffer = new ArrayList<>();
mockTFID.lineBuffer.add(new TextFileLine(null, 0l, null));
mockTFID.lineBuffer.add(new TextFileLine(null, 0l, null));
mockTFID.lineBuffer.add(new TextFileLine(null, 0l, null));
mockTFID.filename = "";
FileContent mockFileContent = mock(FileContent.class);
InputStream mockInputStream = mock(InputStream.class);
when(mockFileContent.getInputStream()).thenReturn(mockInputStream);
FileObject mockFO = mock(FileObject.class);
when(mockFO.getContent()).thenReturn(mockFileContent);
TextFileInputReader tFIR = new TextFileInputReader(mock(IBaseFileInputStepControl.class), mockTFIM, mockTFID, mockFO, mock(LogChannelInterface.class));
assertEquals(3, mockTFID.lineBuffer.size());
tFIR.close();
// After closing the file, the buffer must be empty!
assertEquals(0, mockTFID.lineBuffer.size());
}
use of org.apache.commons.vfs2.FileContent in project pentaho-kettle by pentaho.
the class KettleVFS method getOutputStream.
public static OutputStream getOutputStream(FileObject fileObject, boolean append) throws IOException {
FileObject parent = fileObject.getParent();
if (parent != null) {
if (!parent.exists()) {
throw new IOException(BaseMessages.getString(PKG, "KettleVFS.Exception.ParentDirectoryDoesNotExist", getFriendlyURI(parent)));
}
}
try {
fileObject.createFile();
FileContent content = fileObject.getContent();
return content.getOutputStream(append);
} catch (FileSystemException e) {
//
if (fileObject instanceof LocalFile) {
try {
String filename = getFilename(fileObject);
return new FileOutputStream(new File(filename), append);
} catch (Exception e2) {
// throw the original exception: hide the retry.
throw e;
}
} else {
throw e;
}
}
}
use of org.apache.commons.vfs2.FileContent in project pentaho-platform by pentaho.
the class ApacheVFSOutputHandler method getFileOutputContentItem.
@Override
public IContentItem getFileOutputContentItem() {
String contentRef = getContentRef();
try {
// $NON-NLS-1$
String contentName = getHandlerId().substring(4) + ":" + contentRef;
FileSystemManager fsManager = getFileSystemManager();
if (fsManager == null) {
logError(Messages.getInstance().getString("ApacheVFSOutputHandler.ERROR_0001_CANNOT_GET_VFSMGR"));
return null;
}
FileObject file = fsManager.resolveFile(contentName);
if (file == null) {
logError(Messages.getInstance().getString("ApacheVFSOutputHandler.ERROR_0002_CANNOT_GET_VF", contentName));
return null;
}
if (!file.isWriteable()) {
logError(Messages.getInstance().getString("ApacheVFSOutputHandler.ERROR_0003_CANNOT_WRITE", contentName));
return null;
}
FileContent fileContent = file.getContent();
if (fileContent == null) {
logError(Messages.getInstance().getString("ApacheVFSOutputHandler.ERROR_0004_CANNOT_GET_CTX", contentName));
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), // $NON-NLS-1$
t);
}
return null;
}
Aggregations