Java Code Examples for org.apache.commons.io.FileUtils#openOutputStream()

The following examples show how to use org.apache.commons.io.FileUtils#openOutputStream() . 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: NativeHelper.java    From systemds with Apache License 2.0 6 votes vote down vote up
private static boolean loadLibraryHelper(String path)  {
	OutputStream out = null;
	try(InputStream in = NativeHelper.class.getResourceAsStream("/lib/"+path)) {
		// This logic is added because Java does not allow to load library from a resource file.
		if(in != null) {
			File temp = File.createTempFile(path, "");
			temp.deleteOnExit();
			out = FileUtils.openOutputStream(temp);
			IOUtils.copy(in, out);
			System.load(temp.getAbsolutePath());
			return true;
		}
		else
			LOG.warn("No lib available in the jar:" + path);
	}
	catch(IOException e) {
		LOG.warn("Unable to load library " + path + " from resource:" + e.getMessage());
	}
	finally {
		IOUtilFunctions.closeSilently(out);
	}
	return false;
}
 
Example 2
Source File: SfDictionaryConverterTest.java    From sailfish-core with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void initClass() {
    ClassLoader classLoader = SfDictionaryConverterTest.class.getClassLoader();
    try (InputStream in = classLoader.getResourceAsStream("fix/qfj2dict.xsl");
            InputStream inTypes = classLoader.getResourceAsStream("fix/types.xml")) {
        dictionaryValidator = new FullFIXDictionaryValidatorFactory().createDictionaryValidator();
        converter = new SailfishDictionaryToQuckfixjConverter();
        File xsl = new File(outputFolder, "qfj2dict.xsl");
        File types = new File("types.xml");
        try (OutputStream out = FileUtils.openOutputStream(xsl);
                OutputStream outTypes = FileUtils.openOutputStream(types)) {
            IOUtils.copy(in, FileUtils.openOutputStream(xsl));
            IOUtils.copy(inTypes, FileUtils.openOutputStream(types));
        }
        try (InputStream inXsl = new FileInputStream(xsl)) {
            transformer = transformerFactory.newTransformer(new StreamSource(inXsl));
        }
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        Assert.fail(e.getMessage());
    }
}
 
Example 3
Source File: SailfishDictionaryToQuckfixjConverter.java    From sailfish-core with Apache License 2.0 6 votes vote down vote up
public void convertToQuickFixJ(InputStream sailfishXML, String outputDir)
        throws IOException, DOMException, MessageNotFoundException, TransformerException {
    Map<String, Document> outputXMLs = new HashMap<>();
    sailfishDictionary = loader.load(sailfishXML);
    String[] versionFix = sailfishDictionary.getNamespace().split("_");
    minor = Integer.parseInt(versionFix[versionFix.length - 1]);
    major = Integer.parseInt(versionFix[versionFix.length - 2]);

    if (major < 5) {
        outputXMLs.put("FIX" + major + minor + ".xml",
                createQuickFixJDictionaryStructure(documentBuilder.newDocument(), Mode.ALL));
    } else {
        outputXMLs.put("FIXT11.xml", createQuickFixJDictionaryStructure(documentBuilder.newDocument(), Mode.ADMIN));
        outputXMLs.put("FIX" + major + minor + ".xml",
                createQuickFixJDictionaryStructure(documentBuilder.newDocument(), Mode.APP));
    }
    StreamResult outputResult = new StreamResult();
    for (String fileName : outputXMLs.keySet()) {
        try (OutputStream out = FileUtils.openOutputStream(
                new File(outputDir, fileName))) {
            outputResult.setOutputStream(out);
            transformer.transform(new DOMSource(outputXMLs.get(fileName)), outputResult);
        }
    }
}
 
Example 4
Source File: JustCopy.java    From testarea-itext5 with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
     * <a href="https://stackoverflow.com/questions/48586750/itext-messes-up-pdf-while-joining-multiple-pdfs">
     * iText messes up PDF while joining multiple PDFs
     * </a>
     * <br/>
     * <a href="https://drive.google.com/open?id=1nH_21_f1bmnxeOn5ul8db3eC2CggZiRN">
     * page_444.pdf
     * </a>
     * <p>
     * I cannot reproduce the issue.
     * </p>
     */
    @Test
    public void testPage444() throws IOException, DocumentException {
        Rectangle pageSize = PageSize.A4;
        File outPut = new File(RESULT_FOLDER, "page_444-copied.pdf");

        final Document document = new Document(pageSize );
        final FileOutputStream fos = FileUtils.openOutputStream(outPut);
        final PdfWriter pdfWriter = new PdfSmartCopy(document, fos);

        pdfWriter.setViewerPreferences(PdfWriter.PageLayoutTwoColumnRight);
        pdfWriter.setFullCompression();
        pdfWriter.setPdfVersion(PdfWriter.VERSION_1_6);
//        pdfWriter.setXmpMetadata(getPdfMetaData());

        document.open();
        document.addAuthor("Author");

        final PdfReader reader = new PdfReader(getClass().getResourceAsStream("page_444.pdf"));
        PdfSmartCopy pdfSmartCopy = (PdfSmartCopy) pdfWriter;
        pdfSmartCopy.addPage(pdfSmartCopy.getImportedPage(reader, 1));
        pdfSmartCopy.freeReader(reader);

        // After all files are merged
        document.close();
    }
 
Example 5
Source File: Axis2ServerManager.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
private File copyServiceToFileSystem(String resourceName, String fileName) throws IOException {
    File file = new File(System.getProperty("basedir") + File.separator + "target" + File.separator + fileName);
    if (file.exists()) {
        FileUtils.deleteQuietly(file);
    }
    FileUtils.touch(file);
    OutputStream os = FileUtils.openOutputStream(file);
    InputStream is = new FileInputStream(
            ExtensionUtils.getSystemResourceLocation() + File.separator + "artifacts" + File.separator + "AXIS2"
                    + File.separator + "config" + File.separator + resourceName);
    if (is != null) {
        byte[] data = new byte[1024];
        int len;
        while ((len = is.read(data)) != -1) {
            os.write(data, 0, len);
        }
        os.flush();
        os.close();
        is.close();
    }
    return file;
}
 
Example 6
Source File: TreeSync.java    From jackrabbit-filevault with Apache License 2.0 6 votes vote down vote up
private void writeFile(SyncResult res, Entry e) throws IOException, RepositoryException {
    String action = e.file.exists() ? "U" : "A";
    Binary bin = null;
    InputStream in = null;
    OutputStream out = null;
    try {
        bin = e.node.getProperty("jcr:content/jcr:data").getBinary();
        in = bin.getStream();
        out = FileUtils.openOutputStream(e.file);
        IOUtils.copy(in, out);
        if (preserveFileDate) {
            Calendar lastModified = e.node.getProperty("jcr:content/jcr:lastModified").getDate();
            e.file.setLastModified(lastModified.getTimeInMillis());
        }
        syncLog.log("%s file://%s", action, e.file.getAbsolutePath());
        res.addEntry(e.getJcrPath(), e.getFsPath(), SyncResult.Operation.UPDATE_FS);
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
        if (bin != null) {
            bin.dispose();
        }
    }
}
 
Example 7
Source File: DirectJsonQueryRequestFacetingEmbeddedTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void beforeClass() throws Exception {
  final String sourceHome = ExternalPaths.SOURCE_HOME;

  final File tempSolrHome = LuceneTestCase.createTempDir().toFile();
  FileUtils.copyFileToDirectory(new File(sourceHome, "server/solr/solr.xml"), tempSolrHome);
  final File collectionDir = new File(tempSolrHome, COLLECTION_NAME);
  FileUtils.forceMkdir(collectionDir);
  final File configSetDir = new File(sourceHome, "server/solr/configsets/sample_techproducts_configs/conf");
  FileUtils.copyDirectoryToDirectory(configSetDir, collectionDir);

  final Properties props = new Properties();
  props.setProperty("name", COLLECTION_NAME);

  try (Writer writer = new OutputStreamWriter(FileUtils.openOutputStream(new File(collectionDir, "core.properties")),
      "UTF-8");) {
    props.store(writer, null);
  }

  final String config = tempSolrHome.getAbsolutePath() + "/" + COLLECTION_NAME + "/conf/solrconfig.xml";
  final String schema = tempSolrHome.getAbsolutePath() + "/" + COLLECTION_NAME + "/conf/managed-schema";
  initCore(config, schema, tempSolrHome.getAbsolutePath(), COLLECTION_NAME);

  client = new EmbeddedSolrServer(h.getCoreContainer(), COLLECTION_NAME) {
    @Override
    public void close() {
      // do not close core container
    }
  };

  ContentStreamUpdateRequest up = new ContentStreamUpdateRequest("/update");
  up.setParam("collection", COLLECTION_NAME);
  up.addFile(getFile("solrj/techproducts.xml"), "application/xml");
  up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
  UpdateResponse updateResponse = up.process(client);
  assertEquals(0, updateResponse.getStatus());
}
 
Example 8
Source File: ArtifactsService.java    From gocd with Apache License 2.0 5 votes vote down vote up
public boolean saveOrAppendFile(File dest, InputStream stream) {
    String destPath = dest.getAbsolutePath();
    try {
        LOGGER.trace("Appending file [{}]", destPath);
        try (FileOutputStream out = FileUtils.openOutputStream(dest, true)) {
            IOUtils.copyLarge(stream, out);
        }
        LOGGER.trace("File [{}] appended.", destPath);
        return true;
    } catch (IOException e) {
        LOGGER.error("Failed to save the file to : [{}]", destPath, e);
        return false;
    }
}
 
Example 9
Source File: ElasticsearchArtifactResolverMaster.java    From elasticsearch-maven-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Download the artifact from the download repository.
 * @return the downloaded file
 * @throws IOException when an IO exception occurs
 */
private File downloadArtifact() throws IOException
{
    config.getLog().debug("Downloading the ES artifact");

    String filename = artifactReference.buildBundleFilename();

    File tempFile = new File(FilesystemUtil.getTempDirectory(), filename);
    tempFile.deleteOnExit();
    FileUtils.deleteQuietly(tempFile);

    URL downloadUrl = new URL(
            StringUtils.isBlank(config.getDownloadUrl())
                    ? String.format(ELASTICSEARCH_DOWNLOAD_URL, filename)
                    : config.getDownloadUrl().endsWith(ELASTICSEARCH_FILE_PARAM)
                        ? String.format(config.getDownloadUrl(), filename)
                        : config.getDownloadUrl());

    config.getLog().info("Downloading " + downloadUrl + " to " + tempFile);
    URLConnection connection = downloadUrl.openConnection();
    Optional.ofNullable(config.getDownloadUrlUsername()).ifPresent(username -> {
        String basicAuthenticationEncoded = Base64.getEncoder().encodeToString((config.getDownloadUrlUsername() + ":" + config.getDownloadUrlPassword()).getBytes(StandardCharsets.UTF_8));
        connection.setRequestProperty("Authorization", "Basic " + basicAuthenticationEncoded);
    });
    try (OutputStream out = FileUtils.openOutputStream(tempFile)) {
        IOUtils.copyLarge(connection.getInputStream(), out);
    }

    return tempFile;
}
 
Example 10
Source File: DebugParseFilter.java    From storm-crawler with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Override
public void configure(Map stormConf, JsonNode filterParams) {
    try {
        File outFile = File.createTempFile("DOMDump", ".xml");
        os = FileUtils.openOutputStream(outFile);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 11
Source File: CheckPointStore.java    From kylin with Apache License 2.0 5 votes vote down vote up
public void saveCheckPoint(CheckPoint cp) {
    try (FileOutputStream outputStream = FileUtils.openOutputStream(getCheckPointFile(cp), true)) {
        String jsonCP = JsonUtil.writeValueAsIndentString(cp);
        outputStream.write(Bytes.toBytes(wrapCheckPointString(jsonCP)));
        outputStream.flush();
    } catch (Exception e) {
        logger.error("CheckPoint error for cube " + cubeName, e);
    }
}
 
Example 12
Source File: FragmentFilesMerger.java    From kylin with Apache License 2.0 5 votes vote down vote up
public CuboidMetricDataWriter(long cuboidId, String metricName, int maxValLen) throws IOException {
    this.cuboidId = cuboidId;
    this.metricName = metricName;
    this.maxValLen = maxValLen;
    this.tmpMetricDataFile = new File(mergeWorkingDirectory, cuboidId + "-" + metricName + ".data");
    this.countingOutput = new CountingOutputStream(
            new BufferedOutputStream(FileUtils.openOutputStream(tmpMetricDataFile)));
    this.output = new DataOutputStream(countingOutput);
}
 
Example 13
Source File: MarkdownRenderer.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void tell(String id) throws IOException {
	String path = directoryPath + "/" + this.getRelativePath(id);
	FileUtils.forceMkdir(new File(path));
	io = new PrintStream(
			FileUtils.openOutputStream(new File(path + "/" + id + ".md"))
			);
}
 
Example 14
Source File: FragmentFilesMerger.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public CuboidMetricDataWriter(long cuboidId, String metricName, int maxValLen) throws IOException {
    this.cuboidId = cuboidId;
    this.metricName = metricName;
    this.maxValLen = maxValLen;
    this.tmpMetricDataFile = new File(mergeWorkingDirectory, cuboidId + "-" + metricName + ".data");
    this.countingOutput = new CountingOutputStream(
            new BufferedOutputStream(FileUtils.openOutputStream(tmpMetricDataFile)));
    this.output = new DataOutputStream(countingOutput);
}
 
Example 15
Source File: LoginProperties.java    From cuba with Apache License 2.0 5 votes vote down vote up
protected void saveProperties() {
    File propertiesFile = new File(dataDir, "login.properties");
    try {
        FileOutputStream stream = FileUtils.openOutputStream(propertiesFile);
        try {
            properties.store(stream, "Login properties");
        } finally {
            IOUtils.closeQuietly(stream);
        }
    } catch (IOException e) {
        log.error("Error saving login properties", e);
    }
}
 
Example 16
Source File: HttpsServiceViaHttpProxyTestCase.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
/**
 * Method to change the axis2 config file dynamically
 *
 * @param file : Config file
 * @throws IOException
 */
private void changeConfiguration(String file) throws IOException {
    StringBuilder sb = new StringBuilder();
    File config = new File(
            FrameworkPathUtil.getSystemResourceLocation() + File.separator + "artifacts" + File.separator + "AXIS2"
                    + File.separator + "config" + File.separator + file);
    if (config != null) {
        String currentLine;
        BufferedReader br = new BufferedReader(new FileReader(config));
        while ((currentLine = br.readLine()) != null) {
            if (currentLine.contains("REPLACE_CK")) {
                currentLine = currentLine.replace("REPLACE_CK",
                        System.getProperty(ServerConstants.CARBON_HOME) + File.separator + "repository"
                                + File.separator + "resources" + File.separator + "security" + File.separator
                                + "wso2carbon.jks");
            } else if (currentLine.contains("REPLACE_TS")) {
                currentLine = currentLine.replace("REPLACE_TS",
                        System.getProperty(ServerConstants.CARBON_HOME) + File.separator + "repository"
                                + File.separator + "resources" + File.separator + "security" + File.separator
                                + "client-truststore.jks");
            }
            sb.append(currentLine);
        }
        br.close();
    }
    File newConfig = new File(
            FrameworkPathUtil.getSystemResourceLocation() + File.separator + "artifacts" + File.separator + "AXIS2"
                    + File.separator + "config" + File.separator + MODIFIED_RESOURCE_NAME);
    if (newConfig.exists()) {
        FileUtils.deleteQuietly(newConfig);
    }

    FileUtils.touch(newConfig);
    OutputStream os = FileUtils.openOutputStream(newConfig);
    os.write(sb.toString().getBytes("UTF-8"));
    os.close();
}
 
Example 17
Source File: FragmentFilesMerger.java    From kylin with Apache License 2.0 5 votes vote down vote up
public CuboidColumnDataWriter(long cuboidId, String colName) throws IOException {
    this.cuboidId = cuboidId;
    this.colName = colName;

    this.tmpColDataFile = new File(mergeWorkingDirectory, cuboidId + "-" + colName + ".data");
    this.output = new CountingOutputStream(
            new BufferedOutputStream(FileUtils.openOutputStream(tmpColDataFile)));
}
 
Example 18
Source File: TestCgroupsLCEResourcesHandler.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeleteCgroup() throws Exception {
  final MockClock clock = new MockClock();
  clock.time = System.currentTimeMillis();
  CgroupsLCEResourcesHandler handler = new CgroupsLCEResourcesHandler();
  handler.setConf(new YarnConfiguration());
  handler.initConfig();
  handler.clock = clock;

  FileUtils.deleteQuietly(cgroupDir);

  // Create a non-empty tasks file
  File tfile = new File(cgroupDir.getAbsolutePath(), "tasks");
  FileOutputStream fos = FileUtils.openOutputStream(tfile);
  fos.write("1234".getBytes());
  fos.close();

  final CountDownLatch latch = new CountDownLatch(1);
  new Thread() {
    @Override
    public void run() {
      latch.countDown();
      try {
        Thread.sleep(200);
      } catch (InterruptedException ex) {
        //NOP
      }
      clock.time += YarnConfiguration.
          DEFAULT_NM_LINUX_CONTAINER_CGROUPS_DELETE_TIMEOUT;
    }
  }.start();
  latch.await();
  Assert.assertFalse(handler.deleteCgroup(cgroupDir.getAbsolutePath()));
  FileUtils.deleteQuietly(cgroupDir);
}
 
Example 19
Source File: HttpsServiceViaHttpProxyTestCase.java    From product-ei with Apache License 2.0 4 votes vote down vote up
/**
 * Method to change the axis2 config file dynamically
 *
 * @param file : Config file
 * @throws java.io.IOException
 */
private void changeConfiguration(String file) throws IOException {
    StringBuilder sb = new StringBuilder();
    File config =
            new File(FrameworkPathUtil.getSystemResourceLocation() + File.separator +
                    "artifacts" + File.separator + "AXIS2" + File.separator + "config" +
                    File.separator + file);
    if (config != null) {
        String currentLine;
        BufferedReader br = new BufferedReader(new FileReader(config));
        while ((currentLine = br.readLine()) != null) {
            if (currentLine.contains("REPLACE_CK")) {
                currentLine = currentLine.replace("REPLACE_CK",
                        System.getProperty(ServerConstants.CARBON_HOME) +
                                File.separator + "repository" + File.separator +
                                "resources" + File.separator + "security" +
                                File.separator + "wso2carbon.jks");
            } else if (currentLine.contains("REPLACE_TS")) {
                currentLine = currentLine.replace("REPLACE_TS",
                        System.getProperty(ServerConstants.CARBON_HOME) +
                                File.separator + "repository" + File.separator +
                                "resources" + File.separator + "security" +
                                File.separator + "client-truststore.jks");
            }
            sb.append(currentLine);
        }
        br.close();
    }
    File newConfig =
            new File(FrameworkPathUtil.getSystemResourceLocation() + File.separator +
                    "artifacts" + File.separator + "AXIS2" + File.separator + "config" +
                    File.separator + MODIFIED_RESOURCE_NAME);
    if (newConfig.exists()) {
        FileUtils.deleteQuietly(newConfig);
    }

    FileUtils.touch(newConfig);
    OutputStream os = FileUtils.openOutputStream(newConfig);
    os.write(sb.toString().getBytes("UTF-8"));
    os.close();
}
 
Example 20
Source File: ArchiveUtils.java    From nd4j with Apache License 2.0 4 votes vote down vote up
/**
 * Extracts files to the specified destination
 *
 * @param file the file to extract to
 * @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();
}