List of usage examples for org.apache.commons.io IOUtils copyLarge
public static long copyLarge(Reader input, Writer output) throws IOException
Reader
to a Writer
. From source file:org.n52.web.ctrl.TimeseriesDataController.java
@RequestMapping(value = "/{timeseriesId}/getData", method = GET, params = { RawFormats.RAW_FORMAT }) public void getRawTimeseriesData(HttpServletResponse response, @PathVariable String timeseriesId, @RequestParam MultiValueMap<String, String> query) { IoParameters map = createFromQuery(query); checkIfUnknownTimeseries(map, timeseriesId); RequestSimpleParameterSet parameters = createForSingleSeries(timeseriesId, map); if (!timeseriesDataService.supportsRawData()) { throw new BadRequestException( "Querying of raw procedure data is not supported by the underlying service!"); }/* www . ja v a2 s . co m*/ try (InputStream inputStream = timeseriesDataService.getRawDataService().getRawData(parameters)) { if (inputStream == null) { throw new ResourceNotFoundException("No raw data found for id '" + timeseriesId + "'."); } IOUtils.copyLarge(inputStream, response.getOutputStream()); } catch (IOException e) { throw new InternalServerException("Error while querying raw data", e); } }
From source file:org.n52.wps.server.database.FlatFileDatabase.java
@Override public String storeComplexValue(String id, InputStream resultInputStream, String type, String mimeType) { String resultId = JOINER.join(id, UUID.randomUUID().toString()); try {//from w w w. ja v a 2 s .c om File resultFile = generateComplexDataFile(resultId, mimeType, gzipComplexValues); File mimeTypeFile = generateComplexDataMimeTypeFile(resultId); File contentLengthFile = generateComplexDataContentLengthFile(resultId); LOGGER.debug("initiating storage of complex value for {} as {}", id, resultFile.getPath()); long contentLength = -1; OutputStream resultOutputStream = null; try { resultOutputStream = gzipComplexValues ? new GZIPOutputStream(new FileOutputStream(resultFile)) : new BufferedOutputStream(new FileOutputStream(resultFile)); contentLength = IOUtils.copyLarge(resultInputStream, resultOutputStream); } finally { IOUtils.closeQuietly(resultInputStream); IOUtils.closeQuietly(resultOutputStream); } OutputStream mimeTypeOutputStream = null; try { mimeTypeOutputStream = new BufferedOutputStream(new FileOutputStream(mimeTypeFile)); IOUtils.write(mimeType, mimeTypeOutputStream); } finally { IOUtils.closeQuietly(mimeTypeOutputStream); } OutputStream contentLengthOutputStream = null; try { contentLengthOutputStream = new BufferedOutputStream(new FileOutputStream(contentLengthFile)); IOUtils.write(Long.toString(contentLength), contentLengthOutputStream); } finally { IOUtils.closeQuietly(contentLengthOutputStream); } LOGGER.debug("completed storage of complex value for {} as {}", id, resultFile.getPath()); } catch (IOException e) { throw new RuntimeException("Error storing complex value for " + resultId, e); } return generateRetrieveResultURL(resultId); }
From source file:org.nd4j.util.ArchiveUtils.java
/** * Extracts files to the specified destination * * @param file the file to extract to//from ww w .ja v a2 s . c o m * @param dest the destination directory * @throws IOException */ public static void unzipFileTo(String file, String dest) throws IOException { File target = new File(file); if (!target.exists()) throw new IllegalArgumentException("Archive doesnt exist"); FileInputStream fin = new FileInputStream(target); int BUFFER = 2048; byte data[] = new byte[BUFFER]; if (file.endsWith(".zip") || file.endsWith(".jar")) { try (ZipInputStream zis = new ZipInputStream(fin)) { //get the zipped file list entry ZipEntry ze = zis.getNextEntry(); while (ze != null) { String fileName = ze.getName(); File newFile = new File(dest + File.separator + fileName); if (ze.isDirectory()) { newFile.mkdirs(); zis.closeEntry(); ze = zis.getNextEntry(); continue; } FileOutputStream fos = new FileOutputStream(newFile); int len; while ((len = zis.read(data)) > 0) { fos.write(data, 0, len); } fos.close(); ze = zis.getNextEntry(); log.debug("File extracted: " + newFile.getAbsoluteFile()); } zis.closeEntry(); } } else if (file.endsWith(".tar.gz") || file.endsWith(".tgz")) { BufferedInputStream in = new BufferedInputStream(fin); GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in); TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn); TarArchiveEntry entry; /* Read the tar entries using the getNextEntry method **/ while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) { log.info("Extracting: " + entry.getName()); /* If the entry is a directory, create the directory. */ if (entry.isDirectory()) { File f = new File(dest + File.separator + entry.getName()); f.mkdirs(); } /* * If the entry is a file,write the decompressed file to the disk * and close destination stream. */ else { int count; try (FileOutputStream fos = new FileOutputStream(dest + File.separator + entry.getName()); BufferedOutputStream destStream = new BufferedOutputStream(fos, BUFFER);) { while ((count = tarIn.read(data, 0, BUFFER)) != -1) { destStream.write(data, 0, count); } destStream.flush(); IOUtils.closeQuietly(destStream); } } } // Close the input stream tarIn.close(); } else if (file.endsWith(".gz")) { File extracted = new File(target.getParent(), target.getName().replace(".gz", "")); if (extracted.exists()) extracted.delete(); extracted.createNewFile(); try (GZIPInputStream is2 = new GZIPInputStream(fin); OutputStream fos = FileUtils.openOutputStream(extracted)) { IOUtils.copyLarge(is2, fos); fos.flush(); } } else { throw new IllegalStateException( "Unable to infer file type (compression format) from source file name: " + file); } target.delete(); }
From source file:org.onehippo.forge.content.exim.repository.jaxrs.AbstractContentEximService.java
/** * Transfer attachment content into the given {@code file}. * @param attachment attachment/* w w w .ja va2 s. c o m*/ * @param file destination file * @throws IOException if IO exception occurs */ protected void transferAttachmentToFile(Attachment attachment, File file) throws IOException { InputStream input = null; OutputStream output = null; try { input = attachment.getObject(InputStream.class); output = new FileOutputStream(file); IOUtils.copyLarge(input, output); } finally { IOUtils.closeQuietly(output); IOUtils.closeQuietly(input); } }
From source file:org.onehippo.forge.content.exim.repository.jaxrs.util.ZipCompressUtils.java
/** * Add ZIP entries to {@code zipOutput} by selecting all the descendant files under the {@code baseFolder}, * starting with the ZIP entry name {@code prefix}. * @param baseFolder base folder to find child files underneath * @param prefix the prefix of ZIP entry name * @param zipOutput ZipArchiveOutputStream instance * @throws IOException if IO exception occurs *///from w w w . j a v a 2 s. c om public static void addFileEntriesInFolderToZip(File baseFolder, String prefix, ZipArchiveOutputStream zipOutput) throws IOException { for (File file : baseFolder.listFiles()) { String entryName = (StringUtils.isEmpty(prefix)) ? file.getName() : (prefix + "/" + file.getName()); if (file.isFile()) { ZipArchiveEntry entry = new ZipArchiveEntry(entryName); entry.setSize(file.length()); InputStream input = null; try { zipOutput.putArchiveEntry(entry); input = new FileInputStream(file); IOUtils.copyLarge(input, zipOutput); } finally { IOUtils.closeQuietly(input); zipOutput.closeArchiveEntry(); } } else { addFileEntriesInFolderToZip(file, entryName, zipOutput); } } }
From source file:org.onehippo.forge.jcrshell.commands.PropStore.java
/** * {@inheritDoc}/*from w w w . j ava 2 s .c o m*/ * @throws RepositoryException */ @Override protected final boolean executeCommand(final String[] args) throws RepositoryException, IOException { Node node = JcrWrapper.getCurrentNode(); String propName = args[1]; String fileName = FsWrapper.getFullFileName(args[2]); if (!node.hasProperty(propName) || !isBinaryProperty(node.getProperty(propName))) { JcrShellPrinter.printWarnln("Property " + propName + " is not a binary property."); return false; } File file = new File(fileName); if (!file.exists() && !file.createNewFile()) { JcrShellPrinter.printWarnln("File " + args[2] + " cannot be created."); return false; } OutputStream os = new FileOutputStream(file); InputStream is = node.getProperty(propName).getStream(); try { IOUtils.copyLarge(is, os); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(os); } return true; }
From source file:org.opencastproject.fsresources.ResourceServlet.java
/** * {@inheritDoc}//from w w w . ja v a 2 s. co m * * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, * javax.servlet.http.HttpServletResponse) */ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { logger.debug("Looking for static resource '{}'", req.getRequestURI()); String path = req.getPathInfo(); String normalized = path == null ? "/" : path.trim().replaceAll("/+", "/").replaceAll("\\.\\.", ""); if (path == null) { path = "/"; } else { // Replace duplicate slashes with a single slash, and remove .. from the listing path = path.trim().replaceAll("/+", "/").replaceAll("\\.\\.", ""); } if (normalized != null && normalized.startsWith("/") && normalized.length() > 1) { normalized = normalized.substring(1); } File f = new File(root, normalized); boolean allowed = true; if (f.isFile() && f.canRead()) { allowed = checkDirectory(f.getParentFile()); if (!allowed) { resp.sendError(HttpServletResponse.SC_FORBIDDEN); return; } logger.debug("Serving static resource '{}'", f.getAbsolutePath()); FileInputStream in = new FileInputStream(f); try { IOUtils.copyLarge(in, resp.getOutputStream()); } finally { IOUtils.closeQuietly(in); } } else if (f.isDirectory() && f.canRead()) { allowed = checkDirectory(f); if (!allowed) { resp.sendError(HttpServletResponse.SC_FORBIDDEN); return; } logger.debug("Serving index page for '{}'", f.getAbsolutePath()); PrintWriter out = resp.getWriter(); resp.setContentType("text/html;charset=UTF-8"); out.write("<html>"); out.write("<head><title>File Index for " + normalized + "</title></head>"); out.write("<body>"); out.write("<table>"); SimpleDateFormat sdf = new SimpleDateFormat(); sdf.applyPattern(dateFormat); for (File child : f.listFiles()) { if (child.isDirectory() && !checkDirectory(child)) { continue; } StringBuffer sb = new StringBuffer(); sb.append("<tr><td>"); sb.append("<a href=\""); if (req.getRequestURL().charAt(req.getRequestURL().length() - 1) != '/') { sb.append(req.getRequestURL().append("/").append(child.getName())); } else { sb.append(req.getRequestURL().append(child.getName())); } sb.append("\">"); sb.append(child.getName()); sb.append("</a>"); sb.append("</td><td>"); sb.append(formatLength(child.length())); sb.append("</td><td>"); sb.append(sdf.format(child.lastModified())); sb.append("</td>"); sb.append("</tr>"); out.write(sb.toString()); } out.write("</table>"); out.write("</body>"); out.write("</html>"); } else { logger.debug("Error state for '{}', returning HTTP 404", f.getAbsolutePath()); resp.sendError(HttpServletResponse.SC_NOT_FOUND); } }
From source file:org.opencastproject.workspace.impl.WorkspaceImpl.java
/** * {@inheritDoc}//from w w w.ja v a2 s.c o m * * @see org.opencastproject.workspace.api.Workspace#get(java.net.URI) */ public File get(final URI uri) throws NotFoundException, IOException { final String urlString = uri.toString(); final File f = getWorkspaceFile(uri, false); // Does the file exist and is it up to date? Long workspaceFileLastModified = new Long(0); // make sure this is not null, otherwise the requested file can not be // copied if (f.isFile()) { workspaceFileLastModified = new Long(f.lastModified()); } if (wfrRoot != null && wfrUrl != null) { if (uri.toString().startsWith(wfrUrl)) { String localPath = uri.toString().substring(wfrUrl.length()); File wfrCopy = new File(PathSupport.concat(wfrRoot, localPath)); if (wfrCopy.isFile()) { // if the file exists in the workspace, but is older than the wfr copy, replace it if (workspaceFileLastModified < wfrCopy.lastModified()) { logger.debug("Replacing {} with an updated version from the file repository", f.getAbsolutePath()); if (linkingEnabled) { FileUtils.deleteQuietly(f); FileSupport.link(wfrCopy, f); } else { FileSupport.copy(wfrCopy, f); } } else { logger.debug("{} is up to date", f); } logger.debug("Getting {} directly from working file repository root at {}", uri, f); return new File(f.getAbsolutePath()); } } } String ifNoneMatch = null; if (f.isFile()) { ifNoneMatch = md5(f); } final HttpGet get = new HttpGet(urlString); if (ifNoneMatch != null) get.setHeader("If-None-Match", ifNoneMatch); return locked(f, new Function<File, File>() { @Override public File apply(File file) { InputStream in = null; OutputStream out = null; HttpResponse response = null; try { response = trustedHttpClient.execute(get); if (HttpServletResponse.SC_NOT_FOUND == response.getStatusLine().getStatusCode()) { throw new NotFoundException(uri + " does not exist"); } else if (HttpServletResponse.SC_NOT_MODIFIED == response.getStatusLine().getStatusCode()) { logger.debug("{} has not been modified.", urlString); return file; } else if (HttpServletResponse.SC_ACCEPTED == response.getStatusLine().getStatusCode()) { logger.debug("{} is not ready, try again in one minute.", urlString); String token = response.getHeaders("token")[0].getValue(); get.setParams(new BasicHttpParams().setParameter("token", token)); Thread.sleep(60000); while (true) { response = trustedHttpClient.execute(get); if (HttpServletResponse.SC_NOT_FOUND == response.getStatusLine().getStatusCode()) { throw new NotFoundException(uri + " does not exist"); } else if (HttpServletResponse.SC_NOT_MODIFIED == response.getStatusLine() .getStatusCode()) { logger.debug("{} has not been modified.", urlString); return file; } else if (HttpServletResponse.SC_ACCEPTED == response.getStatusLine() .getStatusCode()) { logger.debug("{} is not ready, try again in one minute.", urlString); Thread.sleep(60000); } else if (HttpServletResponse.SC_OK == response.getStatusLine().getStatusCode()) { logger.info("Downloading {} to {}", urlString, file.getAbsolutePath()); file.createNewFile(); in = response.getEntity().getContent(); out = new FileOutputStream(file); IOUtils.copyLarge(in, out); return file; } else { logger.warn( "Received unexpected response status {} while trying to download from {}", response.getStatusLine().getStatusCode(), urlString); FileUtils.deleteQuietly(file); return chuck(new NotFoundException( "Unexpected response status " + response.getStatusLine().getStatusCode())); } } } else if (HttpServletResponse.SC_OK == response.getStatusLine().getStatusCode()) { logger.info("Downloading {} to {}", urlString, file.getAbsolutePath()); file.createNewFile(); in = response.getEntity().getContent(); out = new FileOutputStream(file); IOUtils.copyLarge(in, out); return file; } else { logger.warn("Received unexpected response status {} while trying to download from {}", response.getStatusLine().getStatusCode(), urlString); FileUtils.deleteQuietly(file); return chuck(new NotFoundException( "Unexpected response status " + response.getStatusLine().getStatusCode())); } } catch (Exception e) { logger.warn("Could not copy {} to {}: {}", new String[] { urlString, file.getAbsolutePath(), e.getMessage() }); FileUtils.deleteQuietly(file); return chuck(new NotFoundException(e)); } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); trustedHttpClient.close(response); } } }); }
From source file:org.opendap.d1.DAPResourceHandler.java
/** * Using the PID, lookup the DAP URL, dereference it and stream the result back * to the client using the response object's output stream. This is used by both * sendObject and sendReplica.//from ww w .j a va 2 s.c o m * * @param extra The D1 PID text. * * @throws InvalidToken * @throws NotAuthorized * @throws NotImplemented * @throws ServiceFailure * @throws NotFound * @throws InsufficientResources * @throws SQLException * @throws DAPDatabaseException * @throws IOException */ private void dereferenceDapURL(String extra) throws InvalidToken, NotAuthorized, NotImplemented, ServiceFailure, NotFound, InsufficientResources, SQLException, DAPDatabaseException, IOException { Identifier pid = new Identifier(); pid.setValue(extra); // get(pid) throws if 'pid' is null (i.e., it will not return a null InputStream) InputStream in = DAPMNodeService.getInstance(request, db, logDb).get(pid); /* Here's how they did it in the metacat server; as with describe, optimizing access to the system metadata via the getSystemMetadata() call and then using that here would probably boost performance. jhrg 6/10/14 // set the headers for the content String mimeType = ObjectFormatInfo.instance().getMimeType(sm.getFormatId().getValue()); if (mimeType == null) { mimeType = "application/octet-stream"; } String extension = ObjectFormatInfo.instance().getExtension(sm.getFormatId().getValue()); String filename = id.getValue(); if (extension != null) { filename = id.getValue() + extension; } response.setContentType(mimeType); response.setHeader("Content-Disposition", "inline; filename=" + filename); */ String formatId = db.getFormatId(extra); String responseType = getResponseType(formatId); response.setContentType(responseType); response.setStatus(200); IOUtils.copyLarge(in, response.getOutputStream()); }
From source file:org.pdfgal.pdfgalweb.utils.impl.FileUtilsImpl.java
@Override public void prepareFileDownload(final HttpServletResponse response, final String uri, final String fileName) throws IOException { final FileInputStream fileInputStream = new FileInputStream(uri); response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); IOUtils.copyLarge(fileInputStream, response.getOutputStream()); fileInputStream.close();/*from www . ja v a2 s .co m*/ }