List of usage examples for org.apache.commons.vfs FileContent getInputStream
InputStream getInputStream() throws FileSystemException;
From source file:com.panet.imeta.core.vfs.KettleVFS.java
public static InputStream getInputStream(FileObject fileObject) throws FileSystemException { FileContent content = fileObject.getContent(); return content.getInputStream(); }
From source file:egovframework.rte.fdl.filehandling.EgovFileUtil.java
/** * <p>/*w w w . j ava 2 s. c o 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:com.qmetric.document.watermark.DefaultPdfReaderFactory.java
@Override public PdfReader newPdfReader(final FileContent fileContent) throws IOException { return new PdfReader(fileContent.getInputStream(), ownerPassword); }
From source file:com.learningobjects.community.abgm.logic.ControllerJob.java
private void download(String sourceUrl, File destinationFile) throws IOException { logger.log(Level.INFO, "Downloading or copying file from " + sourceUrl + " to " + destinationFile); FileObject sourceFileObject = null; OutputStream outputStream = null; try {//from ww w .j ava2 s . c o m // special case sftp so that new hosts work out of the box. other options could go here too SftpFileSystemConfigBuilder sftpFileSystemConfigBuilder = SftpFileSystemConfigBuilder.getInstance(); FileSystemOptions fileSystemOptions = new FileSystemOptions(); sftpFileSystemConfigBuilder.setStrictHostKeyChecking(fileSystemOptions, "no"); // actually try to get the file FileSystemManager fsManager = VFS.getManager(); sourceFileObject = fsManager.resolveFile(sourceUrl, fileSystemOptions); FileContent sourceFileContent = sourceFileObject.getContent(); InputStream inputStream = sourceFileContent.getInputStream(); outputStream = new FileOutputStream(destinationFile); // do the copy - this is probably a dupe of commons io, and many others byte[] buffer = new byte[8192]; int length; while ((length = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, length); } } finally { if (outputStream != null) { outputStream.close(); } if (sourceFileObject != null) { sourceFileObject.close(); // this will close the fileContent object, too } } }
From source file:com.sonatype.nexus.plugin.groovyconsole.DefaultScriptStorage.java
private void updateScript(FileObject file) { FileName name = file.getName();//from w ww .ja v a 2 s .c om getLogger().info("New script file found: " + name); String script; try { FileContent content = file.getContent(); script = IOUtils.toString(content.getInputStream()); content.close(); } catch (IOException e) { getLogger().warn("Unable to read script file: " + name, e); return; } synchronized (scripts) { scripts.put(getName(name), script); } }
From source file:egovframework.rte.fdl.filehandling.FilehandlingServiceTest.java
/** * ? ?? ? ? ./* w ww. ja 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:mondrian.spi.impl.ApacheVfsVirtualFileHandler.java
public InputStream readVirtualFile(String url) throws FileSystemException { // Treat catalogUrl as an Apache VFS (Virtual File System) URL. // VFS handles all of the usual protocols (http:, file:) // and then some. FileSystemManager fsManager = VFS.getManager(); if (fsManager == null) { throw Util.newError("Cannot get virtual file system manager"); }//from w w w . j a v a2 s.co m // Workaround VFS bug. if (url.startsWith("file://localhost")) { url = url.substring("file://localhost".length()); } if (url.startsWith("file:")) { url = url.substring("file:".length()); } //work around for VFS bug not closing http sockets // (Mondrian-585) if (url.startsWith("http")) { try { return new URL(url).openStream(); } catch (IOException e) { throw Util.newError("Could not read URL: " + url); } } File userDir = new File("").getAbsoluteFile(); FileObject file = fsManager.resolveFile(userDir, url); FileContent fileContent = null; try { // Because of VFS caching, make sure we refresh to get the latest // file content. This refresh may possibly solve the following // workaround for defect MONDRIAN-508, but cannot be tested, so we // will leave the work around for now. file.refresh(); // Workaround to defect MONDRIAN-508. For HttpFileObjects, verifies // the URL of the file retrieved matches the URL passed in. A VFS // cache bug can cause it to treat URLs with different parameters // as the same file (e.g. http://blah.com?param=A, // http://blah.com?param=B) if (file instanceof HttpFileObject && !file.getName().getURI().equals(url)) { fsManager.getFilesCache().removeFile(file.getFileSystem(), file.getName()); file = fsManager.resolveFile(userDir, url); } if (!file.isReadable()) { throw Util.newError("Virtual file is not readable: " + url); } fileContent = file.getContent(); } finally { file.close(); } if (fileContent == null) { throw Util.newError("Cannot get virtual file content: " + url); } return fileContent.getInputStream(); }
From source file:mondrian.olap.Util.java
/** * Gets content via Apache VFS. File must exist and have content * * @param url String//ww w . j a va 2 s . c om * @return Apache VFS FileContent for further processing * @throws FileSystemException on error */ public static InputStream readVirtualFile(String url) throws FileSystemException { // Treat catalogUrl as an Apache VFS (Virtual File System) URL. // VFS handles all of the usual protocols (http:, file:) // and then some. FileSystemManager fsManager = VFS.getManager(); if (fsManager == null) { throw newError("Cannot get virtual file system manager"); } // Workaround VFS bug. if (url.startsWith("file://localhost")) { url = url.substring("file://localhost".length()); } if (url.startsWith("file:")) { url = url.substring("file:".length()); } // work around for VFS bug not closing http sockets // (Mondrian-585) if (url.startsWith("http")) { try { return new URL(url).openStream(); } catch (IOException e) { throw newError("Could not read URL: " + url); } } File userDir = new File("").getAbsoluteFile(); FileObject file = fsManager.resolveFile(userDir, url); FileContent fileContent = null; try { // Because of VFS caching, make sure we refresh to get the latest // file content. This refresh may possibly solve the following // workaround for defect MONDRIAN-508, but cannot be tested, so we // will leave the work around for now. file.refresh(); // Workaround to defect MONDRIAN-508. For HttpFileObjects, verifies // the URL of the file retrieved matches the URL passed in. A VFS // cache bug can cause it to treat URLs with different parameters // as the same file (e.g. http://blah.com?param=A, // http://blah.com?param=B) if (file instanceof HttpFileObject && !file.getName().getURI().equals(url)) { fsManager.getFilesCache().removeFile(file.getFileSystem(), file.getName()); file = fsManager.resolveFile(userDir, url); } if (!file.isReadable()) { throw newError("Virtual file is not readable: " + url); } fileContent = file.getContent(); } finally { file.close(); } if (fileContent == null) { throw newError("Cannot get virtual file content: " + url); } return fileContent.getInputStream(); }
From source file:org.apache.ivy.plugins.repository.vfs.VfsRepository.java
/** * Transfer a VFS Resource from the repository to the local file system. * //from www. j a v a 2s.c o m * @param srcVfsURI * a <code>String</code> identifying the VFS resource to be fetched * @param destination * a <code>File</code> identifying the destination file * @throws <code>IOException</code> 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 ex) { fireTransferError(ex); throw ex; } catch (RuntimeException ex) { fireTransferError(ex); throw ex; } }
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(); }/*from ww w. ja v a 2 s.co 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."); } }