Java Code Examples for org.apache.commons.io.IOUtils#copyLarge()

The following examples show how to use org.apache.commons.io.IOUtils#copyLarge() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Test
public void pump_write_error() throws IOException {
  new MockUp<BufferOutputStream>() {
    @Mock
    void write(byte[] b) throws IOException {
      throw error;
    }
  };
  new Expectations(IOUtils.class) {
    {
      IOUtils.copyLarge((InputStream) any, (OutputStream) any);
      result = error;
    }
  };

  pump_error(null);
  Assert.assertTrue(inputStreamClosed);
  Assert.assertTrue(outputStreamClosed);

  inputStreamClosed = false;
  outputStreamClosed = false;
  pump_error(context);
  Assert.assertTrue(inputStreamClosed);
  Assert.assertTrue(outputStreamClosed);
}
 
Example 2
@Override
public long saveInputStream(String id, String root, String filePath, InputStream stream) throws IOException {
    // Do not create the files for resources with zero length bodies
    if ((stream == null)) {
        return 0L;
    }

    // form the file name
    File file = getFile(id, root, filePath);

    // delete the old
    if (file.exists()) {
        file.delete();
    }

    // add the new
    // make sure all directories are there
    File parent = file.getParentFile();
    if (parent != null) {
        parent.mkdirs();
    }

    // write the file
    return IOUtils.copyLarge(stream, new FileOutputStream(file));
}
 
Example 3
Source Project: incubator-gobblin   File: TunnelTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test(enabled = false)
public void mustDownloadLargeFiles()
    throws Exception {

  mockServer.when(HttpRequest.request().withMethod("CONNECT").withPath("www.us.apache.org:80"))
      .respond(HttpResponse.response().withStatusCode(200));
  mockServer.when(HttpRequest.request().withMethod("GET")
      .withPath("/dist//httpcomponents/httpclient/binary/httpcomponents-client-4.5.1-bin.tar.gz"))
      .forward(HttpForward.forward().withHost("www.us.apache.org").withPort(80));

  Tunnel tunnel = Tunnel.build("www.us.apache.org", 80, "localhost", PORT);
  try {
    IOUtils.copyLarge((InputStream) new URL("http://localhost:" + tunnel.getPort()
            + "/dist//httpcomponents/httpclient/binary/httpcomponents-client-4.5.1-bin.tar.gz")
            .getContent(new Class[]{InputStream.class}),
        new FileOutputStream(File.createTempFile("httpcomponents-client-4.5.1-bin", "tar.gz")));
  } finally {
    tunnel.close();
  }
}
 
Example 4
/** 将内容流出到output */
public Long readContent(StorageMapping mapping, OutputStream output) throws Exception {
	long length = -1L;
	FileSystemManager manager = this.getFileSystemManager();
	String prefix = this.getPrefix(mapping);
	String path = this.path();
	FileSystemOptions options = this.getOptions(mapping);
	try (FileObject fo = manager.resolveFile(prefix + PATHSEPARATOR + path, options)) {
		if (fo.exists() && fo.isFile()) {
			try (InputStream input = fo.getContent().getInputStream()) {
				length = IOUtils.copyLarge(input, output);
			}
		} else {
			throw new Exception(fo.getPublicURIString() + " not existed, object:" + this.toString() + ".");
		}
		manager.closeFileSystem(fo.getFileSystem());
	}
	return length;
}
 
Example 5
Source Project: jobson   File: JobResource.java    License: Apache License 2.0 5 votes vote down vote up
private Response generateBinaryDataResponse(JobId jobId, Optional<BinaryData> maybeBinaryData) {
    if (maybeBinaryData.isPresent()) {
        final BinaryData binaryData = maybeBinaryData.get();

        final StreamingOutput body = outputStream -> {
            try {
                IOUtils.copyLarge(binaryData.getData(), outputStream);
            } catch (IOException ex) {
                // This *usually* happens becuse the client closed the TCP
                // connection, which isn't *exceptional*.
            } finally {
                binaryData.getData().close();
            }
        };

        final Response.ResponseBuilder b =
                Response.ok(body, binaryData.getMimeType())
                        .header("Content-Length", binaryData.getSizeOf());

        if (binaryData.getSizeOf() > Constants.MAX_JOB_OUTPUT_SIZE_IN_BYTES_BEFORE_DISABLING_COMPRESSION)
            b.header("Content-Encoding", "identity");

        return b.build();
    } else {
        return Response.status(404).build();
    }
}
 
Example 6
protected void setDownloadResponse(String downloadFile, final HttpServletResponse response) {
    File file = new File(downloadFile);
    try (InputStream fileInputStream = new FileInputStream(file);
            OutputStream output = response.getOutputStream()) {
        response.reset();
        response.setContentType("application/octet-stream");
        response.setContentLength((int) (file.length()));
        response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");
        IOUtils.copyLarge(fileInputStream, output);
        output.flush();
    } catch (IOException e) {
        throw new InternalErrorException("Failed to download file: " + e.getMessage(), e);
    }
}
 
Example 7
Source Project: jobson   File: FilesystemJobsDAO.java    License: Apache License 2.0 5 votes vote down vote up
private void writeJobOutputToDisk(JobOutput jobOutput, Path outputPath) {
    try {
        IOUtils.copyLarge(jobOutput.getData().getData(), new FileOutputStream(outputPath.toFile(), false));
        jobOutput.getData().getData().close();
    } catch (IOException ex) {
        throw new RuntimeException(outputPath + ": cannot write: " + ex);
    }
}
 
Example 8
public void copyTo(OutputStream outstr) {
    try {
        InputStream inputStream = open();
        try {
            IOUtils.copyLarge(inputStream, outstr);
        } finally {
            inputStream.close();
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
 
Example 9
public void processResource(String resource, InputStream is, List<Relocator> relocators) throws IOException {
    final File tempFile = File.createTempFile("Log4j2Plugins", "dat");
    FileOutputStream fos = new FileOutputStream(tempFile);
    try {
        IOUtils.copyLarge(is, fos);
    } finally {
        IOUtils.closeQuietly(fos);
    }
    tempFiles.add(tempFile);

    if (relocators != null) {
        this.relocators.addAll(relocators);
    }
}
 
Example 10
Source Project: chassis   File: ActionInvokingWebSocket.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Gets the websocket session.
 * 
 * @return
 * @throws IOException 
 * @throws GeneralSecurityException 
 */
protected Future<Void> sendContent(InputStream inputStream) throws IOException, GeneralSecurityException {
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	
	IOUtils.copyLarge(inputStream, baos);

	// generate blob
	byte[] contentBlob = serDe.serialize(baos.toByteArray());

	// check if we need to do psk encryption
	contentBlob = pskFrameProcessor.processOutgoing(contentBlob, 0, contentBlob.length);
	
	return session.getRemote().sendBytesByFuture(ByteBuffer.wrap(contentBlob));
}
 
Example 11
Source Project: inception   File: GazeteerServiceImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
@Transactional
public void importGazeteerFile(Gazeteer aGazeteer, InputStream aStream) throws IOException
{
    File gazFile = getGazeteerFile(aGazeteer);
    
    if (!gazFile.getParentFile().exists()) {
        gazFile.getParentFile().mkdirs();
    }
    
    try (OutputStream os = new FileOutputStream(gazFile)) {
        IOUtils.copyLarge(aStream, os);
    }
}
 
Example 12
/**
 * 文件接收处理
 *
 * @return
 */
@Override
public BufferedInputStream recv(InputStream ins) throws IOException {
    BufferedInputStream bufferedInputStream = new BufferedInputStream(ins);
    // 实现文件下载
    byte[] buffer = new byte[bufferLength];
    try {
        IOUtils.copyLarge(ins, outputStream, buffer);
    } catch (IOException e) {
        throw new IOException("文件下载失败!", e);
    } finally {
        IOUtils.closeQuietly(bufferedInputStream);
    }
    return null;
}
 
Example 13
/**
 * Download a file using default settings.
 *
 * @param uri URI
 * @param socketTimeoutMs Socket timeout in milliseconds
 * @param dest File destination
 * @return Response status code
 * @throws IOException
 * @throws HttpErrorStatusException
 */
public int downloadFile(URI uri, int socketTimeoutMs, File dest)
    throws IOException, HttpErrorStatusException {
  HttpUriRequest request = getDownloadFileRequest(uri, socketTimeoutMs);
  try (CloseableHttpResponse response = _httpClient.execute(request)) {
    StatusLine statusLine = response.getStatusLine();
    int statusCode = statusLine.getStatusCode();
    if (statusCode >= 300) {
      throw new HttpErrorStatusException(getErrorMessage(request, response), statusCode);
    }

    HttpEntity entity = response.getEntity();
    try (InputStream inputStream = response.getEntity().getContent();
        OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(dest))) {
      IOUtils.copyLarge(inputStream, outputStream);
    }

    // Verify content length if known
    long contentLength = entity.getContentLength();
    if (contentLength >= 0L) {
      long fileLength = dest.length();
      Preconditions.checkState(fileLength == contentLength, String
          .format("While downloading file with uri: %s, file length: %d does not match content length: %d", uri,
              fileLength, contentLength));
    }

    return statusCode;
  }
}
 
Example 14
Source Project: HtmlUnit-Android   File: XmlSerializer.java    License: Apache License 2.0 5 votes vote down vote up
private Map<String, DomAttr> getAttributesFor(final BaseFrameElement frame) throws IOException {
    final Map<String, DomAttr> map = createAttributesCopyWithClonedAttribute(frame, "src");
    final DomAttr srcAttr = map.get("src");
    if (srcAttr == null) {
        return map;
    }

    final Page enclosedPage = frame.getEnclosedPage();
    final String suffix = getFileExtension(enclosedPage);
    final File file = createFile(srcAttr.getValue(), "." + suffix);

    if (enclosedPage != null) {
        if (enclosedPage.isHtmlPage()) {
            file.delete(); // TODO: refactor as it is stupid to create empty file at one place
            // and then to complain that it already exists
            ((HtmlPage) enclosedPage).save(file);
        }
        else {
            try (InputStream is = enclosedPage.getWebResponse().getContentAsStream()) {
                try (FileOutputStream fos = new FileOutputStream(file)) {
                    IOUtils.copyLarge(is, fos);
                }
            }
        }
    }

    srcAttr.setValue(file.getParentFile().getName() + FILE_SEPARATOR + file.getName());
    return map;
}
 
Example 15
@Override
public void writeToStream(long docId, String resource, OutputStream output) throws IOException {
	try {
		IOUtils.copyLarge(getStream(docId, resource), output);
	} catch (IOException ioe) {
		log.error(ioe.getMessage(), ioe);
		throw ioe;
	}
}
 
Example 16
Source Project: webanno   File: ProjectImportPanel.java    License: Apache License 2.0 4 votes vote down vote up
private void actionImport(AjaxRequestTarget aTarget, Form<Preferences> aForm)
{
    List<FileUpload> exportedProjects = fileUpload.getFileUploads();
    
    User currentUser = userRepository.getCurrentUser();
    boolean currentUserIsAdministrator = userRepository.isAdministrator(currentUser);
    boolean currentUserIsProjectCreator = userRepository.isProjectCreator(currentUser);
    
    boolean createMissingUsers;
    boolean importPermissions;
    
    // Importing of permissions is only allowed if the importing user is an administrator
    if (currentUserIsAdministrator) {
        createMissingUsers = preferences.getObject().generateUsers;
        importPermissions = preferences.getObject().importPermissions;
    }
    // ... otherwise we force-disable importing of permissions so that the only remaining
    // permission for non-admin users is that they become the managers of projects they import.
    else {
        createMissingUsers = false;
        importPermissions = false;
    }
    
    
    // If the current user is a project creator then we assume that the user is importing the
    // project for own use, so we add the user as a project manager. We do not do this if the
    // user is "just" an administrator but not a project creator.
    Optional<User> manager = currentUserIsProjectCreator ? Optional.of(currentUser)
            : Optional.empty();

    Project importedProject = null;
    for (FileUpload exportedProject : exportedProjects) {
        try {
            // Workaround for WICKET-6425
            File tempFile = File.createTempFile("webanno-training", null);
            try (
                    InputStream is = new BufferedInputStream(exportedProject.getInputStream());
                    OutputStream os = new FileOutputStream(tempFile);
            ) {
                if (!ZipUtils.isZipStream(is)) {
                    throw new IOException("Invalid ZIP file");
                }
                IOUtils.copyLarge(is, os);
                
                if (!ImportUtil.isZipValidWebanno(tempFile)) {
                    throw new IOException("ZIP file is not a WebAnno project archive");
                }
                
                ProjectImportRequest request = new ProjectImportRequest(createMissingUsers,
                        importPermissions, manager);
                importedProject = exportService.importProject(request, new ZipFile(tempFile));
            }
            finally {
                tempFile.delete();
            }
        }
        catch (Exception e) {
            aTarget.addChildren(getPage(), IFeedback.class);
            error("Error importing project: " + ExceptionUtils.getRootCauseMessage(e));
            LOG.error("Error importing project", e);
        }
    }
    
    if (importedProject != null) {
        selectedModel.setObject(importedProject);
        aTarget.add(getPage());
        Session.get().setMetaData(CURRENT_PROJECT, importedProject);
    }
}
 
Example 17
Source Project: geowave   File: BigtableEmulator.java    License: Apache License 2.0 4 votes vote down vote up
protected boolean install() throws IOException {
  final URL url = new URL(downloadUrl + "/" + fileName);

  final File downloadFile = new File(sdkDir.getParentFile(), fileName);
  if (!downloadFile.exists()) {
    try (FileOutputStream fos = new FileOutputStream(downloadFile)) {
      IOUtils.copyLarge(url.openStream(), fos);
      fos.flush();
    }
  }
  if (downloadFile.getName().endsWith(".zip")) {
    ZipUtils.unZipFile(downloadFile, sdkDir.getAbsolutePath());
  } else if (downloadFile.getName().endsWith(".tar.gz")) {
    final TarGZipUnArchiver unarchiver = new TarGZipUnArchiver();
    unarchiver.enableLogging(new ConsoleLogger(Logger.LEVEL_WARN, "Gcloud SDK Unarchive"));
    unarchiver.setSourceFile(downloadFile);
    unarchiver.setDestDirectory(sdkDir);
    unarchiver.extract();
  }
  if (!downloadFile.delete()) {
    LOGGER.warn("cannot delete " + downloadFile.getAbsolutePath());
  }
  // Check the install
  if (!isInstalled()) {
    LOGGER.error("Gcloud install failed");
    return false;
  }

  // Install the beta components
  final File gcloudExe = new File(sdkDir, GCLOUD_EXE_DIR + "/gcloud");

  final CommandLine cmdLine = new CommandLine(gcloudExe);
  cmdLine.addArgument("components");
  cmdLine.addArgument("install");
  cmdLine.addArgument("beta");
  cmdLine.addArgument("--quiet");
  final DefaultExecutor executor = new DefaultExecutor();
  final int exitValue = executor.execute(cmdLine);

  return (exitValue == 0);
}
 
Example 18
Source Project: find   File: MapController.java    License: MIT License 4 votes vote down vote up
private String getMapData(final String callback, final URLConnection urlConnection, final String contentType) throws IOException {
    try (final InputStream inputStream = urlConnection.getInputStream(); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
        IOUtils.copyLarge(inputStream, outputStream);
        return callback + "(\"data:" + contentType + ";base64," + new String(Base64.encodeBase64(outputStream.toByteArray(), false, false)) + "\")";
    }
}
 
Example 19
Source Project: webanno   File: AeroRemoteApiController.java    License: Apache License 2.0 4 votes vote down vote up
@ApiOperation(value = "Import a previously exported project")
    @RequestMapping(
            value = ("/" + PROJECTS + "/" + IMPORT), 
            method = RequestMethod.POST,
            consumes = MULTIPART_FORM_DATA_VALUE,
            produces = APPLICATION_JSON_UTF8_VALUE)    
    public ResponseEntity<RResponse<RProject>> projectImport(
            @RequestPart(PARAM_FILE) MultipartFile aFile)
        throws Exception
    {
        // Get current user - this will throw an exception if the current user does not exit
        User user = getCurrentUser();

        // Check for the access
        assertPermission("User [" + user.getUsername() + "] is not allowed to import projects",
                userRepository.isAdministrator(user));
        
        Project importedProject;
        File tempFile = File.createTempFile("webanno-training", null);
        try (
                InputStream is = new BufferedInputStream(aFile.getInputStream());
                OutputStream os = new FileOutputStream(tempFile);
        ) {
            if (!ZipUtils.isZipStream(is)) {
                throw new UnsupportedFormatException("Invalid ZIP file");
            }
            
            IOUtils.copyLarge(is, os);
            
            if (!ImportUtil.isZipValidWebanno(tempFile)) {
                throw new UnsupportedFormatException("Incompatible to webanno ZIP file");
            }
            
//            importedProject = importService.importProject(tempFile, false);
            ProjectImportRequest request = new ProjectImportRequest(false);
            importedProject = exportService.importProject(request, new ZipFile(tempFile));
        }
        finally {
            tempFile.delete();
        }

        return ResponseEntity.ok(new RResponse<>(new RProject(importedProject)));
    }
 
Example 20
/** 
 * get file
 */
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
	if (!securityService.isSuperUser()){
		log.error("Must be super user to download archives");
		response.sendError(HttpServletResponse.SC_FORBIDDEN, "Must be super user to download archives");
		return;
	}
	
	String archiveName = request.getParameter("archive");
	if (archiveName == null || archiveName.isEmpty()) {
		response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "You must supply a archive name");
		return;
	}
	Path sakaiHome = Paths.get(serverConfigurationService.getSakaiHomePath());
	Path archives = sakaiHome.resolve(serverConfigurationService.getString("archive.storage.path", "archive"));
	

	Path archivePath = archives.resolve(archiveName).normalize();
	if (!archivePath.startsWith(archives)) {
		log.error(String.format("The archive file (%s) is not inside the archives folder (%s)",
		archivePath.toString(), archives.toString()));
		response.sendError(HttpServletResponse.SC_BAD_REQUEST,
				"Archive param must be a valid site archive. Param was: " + archiveName);
		return;
	}
	if (!Files.exists(archivePath)) {
		response.sendError(HttpServletResponse.SC_NOT_FOUND);
		return;
	}

	response.setContentType("application/zip");
	response.setHeader("Content-Disposition","attachment;filename=" +archiveName);
	OutputStream out = response.getOutputStream();
	
	try (InputStream in = FileUtils.openInputStream(archivePath.toFile())) {
		IOUtils.copyLarge(in, out);
		out.flush();
		out.close();
	}
}