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

The following examples show how to use org.apache.commons.io.IOUtils#copy() . 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
Source File: ArchiveUtils.java    From konduit-serving with Apache License 2.0 6 votes vote down vote up
/**
 * Extract a single file from a tar.gz file. Does not support directories.
 * NOTE: This should not be used for batch extraction of files, due to the need to iterate over the entries until the
 * specified entry is found. Use {@link #unzipFileTo(String, String)} for batch extraction instead
 *
 * @param tarGz       A tar.gz file
 * @param destination The destination file to extract to
 * @param pathInTarGz The path in the tar.gz file to extract
 */
public static void tarGzExtractSingleFile(File tarGz, File destination, String pathInTarGz) throws IOException {
    try(TarArchiveInputStream tin = new TarArchiveInputStream(new GZIPInputStream(new BufferedInputStream(new FileInputStream(tarGz))))) {
        ArchiveEntry entry;
        boolean extracted = false;
        while((entry = tin.getNextTarEntry()) != null){
            String name = entry.getName();
            if(pathInTarGz.equals(name)){
                try(OutputStream os = new BufferedOutputStream(new FileOutputStream(destination))){
                    IOUtils.copy(tin, os);
                }
                extracted = true;
            }
        }
        Preconditions.checkState(extracted, "No file was extracted. File not found? %s", pathInTarGz);
    }
}
 
Example 2
Source File: FSManager.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
public final FileObject putInMemory(final InputStream is, final String path) throws IOException {
  LOG.info("Write in memory {}", path);
  final FileObject memObject = fsManager.resolveFile(MEM_PREFIX + path);

  if (memObject.exists()) {
    memObject.delete();
  }

  // create in-memory file
  memObject.createFile();

  // read in-memory content
  final OutputStream os = memObject.getContent().getOutputStream();
  IOUtils.copy(is, os);
  IOUtils.closeQuietly(is);
  IOUtils.closeQuietly(os);

  return memObject;
}
 
Example 3
Source File: TestControllerEndpoints.java    From proteus with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("resource")
@Test
public void responseUploadFileParameter()
{

	try
	{

		final InputStream is = given().multiPart("file", file).accept(ContentType.ANY).when().post("tests/response/file").asInputStream();

		final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		IOUtils.copy(is, byteArrayOutputStream);
		IOUtils.closeQuietly(byteArrayOutputStream);
		IOUtils.closeQuietly(is);

		assertThat(byteArrayOutputStream.size(), equalTo(Long.valueOf(file.length()).intValue()));

	} catch (Exception e)
	{
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

}
 
Example 4
Source File: AboutFrame.java    From ontopia with Apache License 2.0 6 votes vote down vote up
private Icon getAboutImage() {
  ByteArrayOutputStream output = new ByteArrayOutputStream();
  InputStream input = ClassLoader
      .getSystemResourceAsStream("net/ontopia/topicmaps/viz/logo.gif");

  if (input == null) return null;
  
  try {
    IOUtils.copy(input, output);
  } catch (IOException e) {
    e.printStackTrace();
    return null;
  }

  return new ImageIcon(output.toByteArray());
}
 
Example 5
Source File: PlayerController.java    From jsflight with Apache License 2.0 6 votes vote down vote up
/**
 * Get screenshot after some step in experiment
 * <p>
 * $ curl "127.0.0.1:8080/player/screenshot?experimentId=573b3169c92e9527bc805cc6&step=0"
 *
 * @param experimentId
 * @param step
 */
@RequestMapping(value = "/screenshot", method = RequestMethod.GET)
public void screenshot(@RequestParam("experimentId") String experimentId, @RequestParam("step") Integer step,
        HttpServletRequest request, HttpServletResponse response) throws IOException
{
    InputStream stream = player.getScreenshot(experimentId, step);
    if (stream == null)
    {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }

    response.setContentType("image/png");
    response.setHeader("Content-Disposition",
            String.format("attachment; filename=\"%s\"", experimentId + String.format("_%05d.png", step)));
    IOUtils.copy(stream, response.getOutputStream());
    response.flushBuffer();
}
 
Example 6
Source File: AttachmentsTest.java    From java-cloudant with Apache License 2.0 6 votes vote down vote up
@Test
public void getAttachmentStandaloneWithRev() throws IOException, URISyntaxException {
    byte[] bytesToDB = "binary data".getBytes();
    ByteArrayInputStream bytesIn = new ByteArrayInputStream(bytesToDB);
    Response response = db.saveAttachment(bytesIn, "foo.txt", "text/plain");

    Document doc = db.find(Document.class, response.getId());
    assertTrue(doc.getAttachments().containsKey("foo.txt"));

    InputStream in = db.getAttachment(
            response.getId(), "foo.txt", response.getRev());

    try {

        ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
        IOUtils.copy(in, bytesOut);
        byte[] bytesFromDB = bytesOut.toByteArray();
        assertArrayEquals(bytesToDB, bytesFromDB);
    } finally {
        in.close();
    }
}
 
Example 7
Source File: LabsMaintenanceBOAttachmentAft.java    From rice with Educational Community License v2.0 6 votes vote down vote up
protected void setUpResourceFiles(String resourceDir) throws Exception {
    System.out.println("In for setUpResourceFiles");
    String[] resources = getResourceListing(getClass(), resourceDir);
    fileUploadList = new ArrayList<File>();

    for (String resource : resources) {
        InputStream inputStream = getClass().getResourceAsStream(resource);
        File file = new File(System.getProperty("java.io.tmpdir") + File.separator + resource);
        OutputStream outputStream = new FileOutputStream(file);
        IOUtils.copy(inputStream, outputStream);
        outputStream.close();
        fileUploadList.add(file);
        System.out.println("For for setUpResourceFiles");
    }
    Collections.sort(fileUploadList);
}
 
Example 8
Source File: DeferredFileOutputStream.java    From aion-germany with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Writes the data from this output stream to the specified output stream,
 * after it has been closed.
 *
 * @param out output stream to write to.
 * @exception IOException if this stream is not yet closed or an error occurs.
 */
public void writeTo(OutputStream out) throws IOException 
{
    // we may only need to check if this is closed if we are working with a file
    // but we should force the habit of closing wether we are working with
    // a file or memory.
    if (!closed)
    {
        throw new IOException("Stream not closed");
    }
    
    if(isInMemory())
    {
        memoryOutputStream.writeTo(out);
    }
    else
    {
        FileInputStream fis = new FileInputStream(outputFile);
        try {
            IOUtils.copy(fis, out);
        } finally {
            IOUtils.closeQuietly(fis);
        }
    }
}
 
Example 9
Source File: WebhookJsonReader.java    From java-client with Apache License 2.0 5 votes vote down vote up
@Override
public T readFrom(Class<T> type, Type genericType, Annotation[] annotations,
                  MediaType mediaType, MultivaluedMap<String, String> httpHeaders,
                  InputStream entityStream) throws IOException, WebApplicationException {
    StringWriter writer = new StringWriter();
    IOUtils.copy(entityStream, writer, "UTF-8");
    String json = writer.toString();
    if (String.class == genericType)
        return type.cast(json);
    return GsonFactory.getGson().fromJson(json, genericType);
}
 
Example 10
Source File: MetsResource.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
@GET
@Path("/editor/start/{derivateId}")
@Produces(MediaType.TEXT_HTML)
public String startEditor(@PathParam("derivateId") String derivateId) {
    MCRObjectID derivateIdObject = MCRObjectID.getInstance(derivateId);

    checkDerivateExists(derivateIdObject);
    checkDerivateAccess(derivateIdObject, MCRAccessManager.PERMISSION_WRITE);

    InputStream resourceAsStream = MCRClassTools.getClassLoader().getResourceAsStream("mets-editor.html");
    try {
        StringWriter writer = new StringWriter();
        IOUtils.copy(resourceAsStream, writer, StandardCharsets.UTF_8);
        String htmlTemplate = writer.toString();
        // add additional javascript code
        String js = MCRConfiguration2.getString("MCR.Mets.Editor.additional.javascript").orElse(null);
        if (js != null && !js.isEmpty()) {
            htmlTemplate = htmlTemplate.replace("<link rel=\"additionalJS\" />", js);
        }
        // replace variables
        htmlTemplate = htmlTemplate.replaceAll("\\{baseURL\\}", MCRFrontendUtil.getBaseURL())
            .replaceAll("\\{derivateID\\}", derivateId);
        return htmlTemplate;
    } catch (IOException e) {
        throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
    }
}
 
Example 11
Source File: Browser.java    From kurento-java with Apache License 2.0 5 votes vote down vote up
public void injectKurentoTestJs() throws IOException {
  if (this.getBrowserType() != BrowserType.IEXPLORER) {

    String kurentoTestJsContent = "";
    String kurentoTestPath = "static/lib/kurento-test.min.js";
    try {
      File pageFile =
          new File(this.getClass().getClassLoader().getResource(kurentoTestPath).getFile());
      kurentoTestJsContent = new String(Files.readAllBytes(pageFile.toPath()));
    } catch (NoSuchFileException nsfe) {
      InputStream inputStream =
          this.getClass().getClassLoader().getResourceAsStream(kurentoTestPath);
      StringWriter writer = new StringWriter();
      IOUtils.copy(inputStream, writer, Charset.defaultCharset());
      kurentoTestJsContent = writer.toString();
    }

    String kurentoTestJs = "var kurentoScript=window.document.createElement('script');";
    kurentoTestJs += "kurentoScript.type='text/javascript';";
    kurentoTestJs += "kurentoScript.text='" + kurentoTestJsContent + "';";
    kurentoTestJs += "window.document.head.appendChild(kurentoScript);";
    kurentoTestJs += "return true;";
    this.executeScript(kurentoTestJs);

    // Disable RecordRTC.js injection
    // String recordingJs = "var recScript=window.document.createElement('script');";
    // recordingJs += "recScript.type='text/javascript';";
    // recordingJs += "recScript.src='https://cdn.webrtc-experiment.com/RecordRTC.js';";
    // recordingJs += "window.document.head.appendChild(recScript);";
    // recordingJs += "return true;";
    // this.executeScript(recordingJs);
  }
}
 
Example 12
Source File: DockerAccessWithHcClient.java    From docker-maven-plugin with Apache License 2.0 5 votes vote down vote up
private ResponseHandler<Object> getImageResponseHandler(final String filename, final ArchiveCompression compression) throws FileNotFoundException {
    return new ResponseHandler<Object>() {
        @Override
        public Object handleResponse(HttpResponse response) throws IOException {
            try (InputStream stream = response.getEntity().getContent();
                 OutputStream out = compression.wrapOutputStream(new FileOutputStream(filename))) {
                IOUtils.copy(stream, out, COPY_BUFFER_SIZE);
            }
            return null;
        }
    };
}
 
Example 13
Source File: MoreoverJsonActivitySerializerIT.java    From streams with Apache License 2.0 5 votes vote down vote up
/**
 * Before.
 * @throws Exception Exception
 */
@BeforeClass
public void setup() throws Exception {

  StringWriter writer = new StringWriter();
  InputStream resourceAsStream = this.getClass().getResourceAsStream("/moreover.json");
  IOUtils.copy(resourceAsStream, writer, Charset.forName("UTF-8"));

  mapper = new ObjectMapper();
  mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, Boolean.FALSE);
  mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, Boolean.TRUE);
  mapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, Boolean.TRUE);

  json = mapper.readValue(writer.toString(), JsonNode.class);
}
 
Example 14
Source File: WindupCommandTest.java    From windup with Eclipse Public License 1.0 4 votes vote down vote up
@Test
public void testDuplicateUserRulesDirMigration() throws Exception
{
    Assert.assertNotNull(uiTestHarness);
    try (CommandController controller = uiTestHarness.createCommandController(WindupCommand.class))
    {
        File outputFile = File.createTempFile("windupwizardtest", ".jar");
        outputFile.deleteOnExit();
        try (InputStream iStream = getClass().getResourceAsStream("/test.jar"))
        {
            try (OutputStream oStream = new FileOutputStream(outputFile))
            {
                IOUtils.copy(iStream, oStream);
            }
        }

        File reportPath = new File(outputFile.getAbsoluteFile() + "_output");
        try
        {
            reportPath.mkdirs();

            setupController(controller, outputFile, reportPath);

            Path expectedUserHomeRulesDir = PathUtil.getUserRulesDir();
            expectedUserHomeRulesDir.toFile().mkdirs();
            controller.setValueFor(UserRulesDirectoryOption.NAME, Collections.singletonList(expectedUserHomeRulesDir.toFile()));

            Result result = controller.execute();
            final String msg = "controller.execute() 'Failed': " + result.getMessage();
            Assert.assertFalse(msg, result instanceof Failed);

            WindupConfiguration windupConfiguration = (WindupConfiguration) controller.getContext()
                        .getAttributeMap()
                        .get(WindupConfiguration.class);
            Collection<File> resultUserSpecifiedRulesDirs = windupConfiguration.getOptionValue(UserRulesDirectoryOption.NAME);
            
            Assert.assertEquals(expectedUserHomeRulesDir.toFile(), resultUserSpecifiedRulesDirs.iterator().next());

            Iterable<Path> allRulesPaths = windupConfiguration.getAllUserRulesDirectories();

            Path expectedWindupHomeRulesDir = PathUtil.getWindupRulesDir();

            boolean foundUserHomeDirRulesPath = false;
            boolean foundWindupHomeDirRulesPath = false;
            int totalFound = 0;
            for (Path rulesPath : allRulesPaths)
            {
                totalFound++;
                if (rulesPath.equals(expectedUserHomeRulesDir))
                {
                    foundUserHomeDirRulesPath = true;
                }
                if (rulesPath.equals(expectedWindupHomeRulesDir))
                {
                    foundWindupHomeDirRulesPath = true;
                }
            }
            Assert.assertTrue(foundUserHomeDirRulesPath);
            Assert.assertTrue(foundWindupHomeDirRulesPath);
            Assert.assertEquals(2, totalFound);
        }
        finally
        {
            outputFile.delete();
            FileUtils.deleteDirectory(reportPath);
        }
    }
}
 
Example 15
Source File: SourceContextPluginIntegrationTest.java    From app-gradle-plugin with Apache License 2.0 4 votes vote down vote up
/** Create a test project with git source context. */
public void setUpTestProject() throws IOException {
  Path buildFile = testProjectDir.getRoot().toPath().resolve("build.gradle");

  Files.createDirectory(testProjectDir.getRoot().toPath().resolve("src"));
  InputStream buildFileContent =
      getClass()
          .getClassLoader()
          .getResourceAsStream("projects/sourcecontext-project/build.gradle");
  Files.copy(buildFileContent, buildFile);

  Path gitContext = testProjectDir.getRoot().toPath().resolve("gitContext.zip");
  InputStream gitContextContent =
      getClass()
          .getClassLoader()
          .getResourceAsStream("projects/sourcecontext-project/gitContext.zip");
  Files.copy(gitContextContent, gitContext);

  try (ZipFile zipFile = new ZipFile(gitContext.toFile())) {
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
      ZipEntry entry = entries.nextElement();
      File entryDestination = new File(testProjectDir.getRoot(), entry.getName());
      if (entry.isDirectory()) {
        entryDestination.mkdirs();
      } else {
        entryDestination.getParentFile().mkdirs();
        InputStream in = zipFile.getInputStream(entry);
        OutputStream out = new FileOutputStream(entryDestination);
        IOUtils.copy(in, out);
        IOUtils.closeQuietly(in);
        out.close();
      }
    }
  }

  FileUtils.delete(gitContext.toFile());

  Path webInf = testProjectDir.getRoot().toPath().resolve("src/main/webapp/WEB-INF");
  Files.createDirectories(webInf);
  File appengineWebXml = Files.createFile(webInf.resolve("appengine-web.xml")).toFile();
  Files.write(appengineWebXml.toPath(), "<appengine-web-app/>".getBytes(Charsets.UTF_8));
}
 
Example 16
Source File: StramAppLauncher.java    From Bats with Apache License 2.0 4 votes vote down vote up
private void init() throws Exception
{
  String originalAppId = propertiesBuilder.conf.get(ORIGINAL_APP_ID);
  if (originalAppId == null) {
    throw new AssertionError("Need original app id if launching without apa or appjar");
  }
  Path appsBasePath = new Path(StramClientUtils.getApexDFSRootDir(fs, conf), StramClientUtils.SUBDIR_APPS);
  Path origAppPath = new Path(appsBasePath, originalAppId);
  StringWriter writer = new StringWriter();
  try (FSDataInputStream in = fs.open(new Path(origAppPath, "meta.json"))) {
    IOUtils.copy(in, writer);
  }
  JSONObject metaJson = new JSONObject(writer.toString());
  String originalLibJars = null;

  // Getting the old libjar dependency is necessary here to construct the class loader because during launch it
  // needs to deserialize the dag to change the serialized state with new app id. This may become unnecessary if we
  // don't rely on object deserialization for changing the app id in the future.
  try {
    JSONObject attributes = metaJson.getJSONObject("attributes");
    originalLibJars = attributes.getString(Context.DAGContext.LIBRARY_JARS.getSimpleName());
    recoveryAppName = attributes.getString(Context.DAGContext.APPLICATION_NAME.getSimpleName());
  } catch (JSONException ex) {
    recoveryAppName = "Recovery App From " + originalAppId;
  }

  LinkedHashSet<URL> clUrls = new LinkedHashSet<>();
  String libjars = propertiesBuilder.conf.get(LIBJARS_CONF_KEY_NAME);

  if (StringUtils.isBlank(libjars)) {
    libjars = originalLibJars;
  } else if (StringUtils.isNotBlank(originalLibJars)) {
    libjars = libjars + "," + originalLibJars;
  }
  propertiesBuilder.conf.set(LIBJARS_CONF_KEY_NAME, libjars);
  processLibJars(libjars, clUrls);

  for (URL baseURL : clUrls) {
    LOG.debug("Dependency: {}", baseURL);
  }

  this.launchDependencies = clUrls;
}
 
Example 17
Source File: BimServerClient.java    From BIMserver with GNU Affero General Public License v3.0 4 votes vote down vote up
public void downloadExtendedData(long edid, OutputStream outputStream) throws IOException {
	try (InputStream downloadData = channel.getDownloadExtendedData(baseAddress, token, edid)) {
		IOUtils.copy(downloadData, outputStream);
	}
}
 
Example 18
Source File: DownloadUtils.java    From es with Apache License 2.0 4 votes vote down vote up
public static void download(HttpServletRequest request, HttpServletResponse response, String displayName, byte[] bytes) throws IOException {


        if (ArrayUtils.isEmpty(bytes)) {
            response.setContentType("text/html;charset=utf-8");
            response.setCharacterEncoding("utf-8");
            response.getWriter().write("您下载的文件不存在!");
            return;
        }

        String userAgent = request.getHeader("User-Agent");
        boolean isIE = (userAgent != null) && (userAgent.toLowerCase().indexOf("msie") != -1);

        response.reset();
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "must-revalidate, no-transform");
        response.setDateHeader("Expires", 0L);

        response.setContentType("application/x-download");
        response.setContentLength((int) bytes.length);

        String displayFilename = displayName.substring(displayName.lastIndexOf("_") + 1);
        displayFilename = displayFilename.replace(" ", "_");
        if (isIE) {
            displayFilename = URLEncoder.encode(displayFilename, "UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=\"" + displayFilename + "\"");
        } else {
            displayFilename = new String(displayFilename.getBytes("UTF-8"), "ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename=" + displayFilename);
        }
        BufferedInputStream is = null;
        OutputStream os = null;
        try {

            os = response.getOutputStream();
            is = new BufferedInputStream(new ByteArrayInputStream(bytes));
            IOUtils.copy(is, os);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
 
Example 19
Source File: HttpJsonChunksInputOperatorTest.java    From attic-apex-malhar with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testHttpInputModule() throws Exception
{

  final List<String> receivedMessages = new ArrayList<String>();
  Handler handler = new AbstractHandler()
  {
    int responseCount = 0;

    @Override
    public void handle(String string, Request rq, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      IOUtils.copy(request.getInputStream(), bos);
      receivedMessages.add(new String(bos.toByteArray()));
      response.setContentType("application/json");
      response.setStatus(HttpServletResponse.SC_OK);
      response.setHeader("Transfer-Encoding", "chunked");
      try {
        JSONObject json = new JSONObject();
        json.put("responseId", "response" + ++responseCount);
        byte[] bytes = json.toString().getBytes();
        response.getOutputStream().println(bytes.length);
        response.getOutputStream().write(bytes);
        response.getOutputStream().println();
        response.getOutputStream().println(0);
        response.getOutputStream().flush();
      } catch (JSONException e) {
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Error generating response: " + e.toString());
      }

      ((Request)request).setHandled(true);
    }
  };

  Server server = new Server(0);
  server.setHandler(handler);
  server.start();

  String url = "http://localhost:" + server.getConnectors()[0].getLocalPort() + "/somecontext";
  final AbstractHttpInputOperator operator = new HttpJsonChunksInputOperator();

  CollectorTestSink sink = new CollectorTestSink();

  operator.outputPort.setSink(sink);
  operator.setUrl(new URI(url));

  operator.setup(null);
  operator.activate(null);

  int timeoutMillis = 3000;
  while (sink.collectedTuples.isEmpty() && timeoutMillis > 0) {
    operator.emitTuples();
    timeoutMillis -= 20;
    Thread.sleep(20);
  }

  Assert.assertTrue("tuple emitted", sink.collectedTuples.size() > 0);

  Map<String, String> tuple = (Map<String, String>)sink.collectedTuples.get(0);
  Assert.assertEquals("", tuple.get("responseId"), "response1");

  operator.deactivate();
  operator.teardown();
  server.stop();

}
 
Example 20
Source File: RestApiSession.java    From sdk-rest with MIT License 4 votes vote down vote up
private String streamToString(InputStream inputStream) throws IOException {
	StringWriter writer = new StringWriter();
	IOUtils.copy(inputStream, writer, "UTF-8");
	return writer.toString();

}