Example usage for org.apache.commons.io FileUtils openOutputStream

List of usage examples for org.apache.commons.io FileUtils openOutputStream

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils openOutputStream.

Prototype

public static FileOutputStream openOutputStream(File file) throws IOException 

Source Link

Document

Opens a FileOutputStream for the specified file, checking and creating the parent directory if it does not exist.

Usage

From source file:org.ngrinder.infra.AgentHome.java

/**
 * Save properties.//from  w w w .j  a  va2 s .c  o  m
 *
 * @param path       path to save
 * @param properties properties.
 */
public void saveProperties(String path, Properties properties) {
    OutputStream out = null;
    try {
        File propertiesFile = new File(getDirectory(), path);
        out = FileUtils.openOutputStream(propertiesFile);
        properties.store(out, null);
    } catch (IOException e) {
        LOGGER.error("Could not save property  sourceFile on " + path, e);
    } finally {
        IOUtils.closeQuietly(out);
    }
}

From source file:org.ngrinder.recorder.infra.RecorderHome.java

/**
 * Save properties./* w ww  . j  ava  2  s  .  c  o  m*/
 * 
 * @param path
 *            path to save
 * @param properties
 *            properties.
 */
public void saveProperties(String path, Properties properties) {
    OutputStream out = null;
    try {
        File propertiesFile = new File(getDirectory(), path);
        out = FileUtils.openOutputStream(propertiesFile);
        properties.store(out, null);
    } catch (IOException e) {
        LOGGER.error("Could not save property  file on " + path, e);
    } finally {
        IOUtils.closeQuietly(out);
    }
}

From source file:org.nuxeo.common.utils.ZipUtils.java

private static void unzip(ZipInputStream in, File dir, Predicate<ZipEntry> filter,
        Function<String, String> nameFormatter) throws IOException {
    dir.mkdirs();/*www . java2  s  .c  o  m*/
    ZipEntry entry;
    while ((entry = in.getNextEntry()) != null) {
        if (!entry.getName().contains("..") && filter.test(entry)) {
            File file = new File(dir, nameFormatter.apply(entry.getName()));
            if (entry.isDirectory()) {
                file.mkdirs();
            } else {
                file.getParentFile().mkdirs();
                try (FileOutputStream output = FileUtils.openOutputStream(file)) {
                    IOUtils.copy(in, output);
                }
            }
        }
    }
}

From source file:org.openanzo.security.keystore.SecretKeyStore.java

/**
 * Loads the secret key to use for encryption and decryption. It will read the key from the keystore if it exists. Otherwise it will create a new randomly
 * generated key and save it in a keystore at the given file. It will use the algorithm defined in the <code>algorithm</code> member.
 * /*www.j  a va2s.  co  m*/
 * @param keyStoreStream
 *            stream from which to read the keystore which holds the secret key. If null, a new keystore is created.
 * @param password
 *            password used to protect the and integrity-check the secret key.
 * @param keyStoreDestination
 *            File path to which to save the keystore in case it is newly created or a new key was added. If null, then nothing is written out.
 * @return the loaded or newly generated secret key.
 * @throws AnzoException
 */
private SecretKey loadKey(InputStream keyStoreStream, String password, File keyStoreDestination,
        String keystoreType) throws AnzoException {

    try {
        KeyStore keyStore = KeyStore.getInstance(keystoreType);
        keyStore.load(keyStoreStream, password.toCharArray());

        Key key = null;
        if (keyStore.containsAlias(KEY_NAME)) {
            key = keyStore.getKey(KEY_NAME, password.toCharArray());
        } else {
            log.warn("Could not find key '{}' within keystore. Generating a new key.", KEY_NAME);
            KeyGenerator kgen = KeyGenerator.getInstance(algorithm);
            key = kgen.generateKey();
            keyStore.setKeyEntry(KEY_NAME, key, password.toCharArray(), new Certificate[0]);
            if (keyStoreDestination != null) {
                log.warn("Storing new key in the keystore.");
                OutputStream outputStream = null;
                try {
                    outputStream = FileUtils.openOutputStream(keyStoreDestination);
                    keyStore.store(outputStream, password.toCharArray());
                } finally {
                    if (outputStream != null) {
                        outputStream.close();
                    }
                }

            }
        }

        if (!(key instanceof SecretKey))
            throw new AnzoException(ExceptionConstants.OSGI.INTERNAL_COMPONENT_ERROR,
                    "key must be of type SecretKey: " + key);
        return (SecretKey) key;
    } catch (GeneralSecurityException e) {
        throw new AnzoException(ExceptionConstants.OSGI.INTERNAL_COMPONENT_ERROR, e);
    } catch (IOException e) {
        throw new AnzoException(ExceptionConstants.OSGI.INTERNAL_COMPONENT_ERROR, e);
    }

}

From source file:org.openanzo.security.keystore.TestSecretKeyEncoder.java

/**
 * Main method used to generate a keystore. Useful for bootstrapping the first time.
 * /*  ww w  .  ja  va2  s.c o m*/
 * @param args
 * @throws Exception
 */
public static void main(String[] args) throws Exception {
    File file = new File("testKeystore");
    System.out.println("Generating new keystore to:" + file.getAbsolutePath());

    KeyStore keyStore = KeyStore.getInstance("JCEKS");
    keyStore.load(null, TEST_KEYSTORE_PASSWORD);
    KeyGenerator kgen = KeyGenerator.getInstance(ALGORITHM);
    Key key = kgen.generateKey();
    keyStore.setKeyEntry(KEY_NAME, key, TEST_KEYSTORE_PASSWORD, new Certificate[0]);
    keyStore.store(FileUtils.openOutputStream(file), TEST_KEYSTORE_PASSWORD);
    System.out.println("Done generating keystore.");
}

From source file:org.opencastproject.episode.filesystem.FileSystemElementStore.java

/**
 * @see org.opencastproject.episode.impl.elementstore.ElementStore#put(org.opencastproject.episode.impl.StoragePath,
 *      Source)//from   w w w .jav a2 s . com
 */
@Override
public void put(StoragePath storagePath, Source source) throws ElementStoreException {
    InputStream in = null;
    FileOutputStream output = null;
    HttpResponse response = null;
    final File destination = createFile(storagePath, source);
    try {
        mkParent(destination);
        final HttpGet getRequest = new HttpGet(source.getUri());
        response = httpClient.execute(getRequest);
        in = response.getEntity().getContent();
        output = FileUtils.openOutputStream(destination);
        IOUtils.copy(in, output);
    } catch (Exception e) {
        FileUtils.deleteQuietly(destination);
        logger.error("Error storing source {} to archive {}", source, destination.getAbsolutePath());
        throw new ElementStoreException(e);
    } finally {
        IOUtils.closeQuietly(output);
        IOUtils.closeQuietly(in);
        httpClient.close(response);
    }
}

From source file:org.openengsb.persistence.rulebase.filebackend.GlobalDeclarationPersistenceBackendService.java

private void writeStorageFile(Map<String, String> globals) throws PersistenceException {
    LOGGER.debug("write globals to \"{}\"", storageFile);
    OutputStream os = null;//from  w w w .j a v a 2s  .  c  o m
    try {
        os = FileUtils.openOutputStream(storageFile);
        for (Entry<String, String> entry : globals.entrySet()) {
            IOUtils.write(entry.getValue() + SEPARATOR + entry.getKey() + IOUtils.LINE_SEPARATOR, os);
        }
    } catch (IOException ex) {
        throw new PersistenceException(ex);
    } finally {
        IOUtils.closeQuietly(os);
    }
}

From source file:org.overlord.dtgov.ui.server.servlets.DeploymentUploadServlet.java

/**
 * Make a temporary copy of the resource by saving the content to a temp file.
 * @param resourceInputStream/*from w  w  w .ja v a2 s.c  o  m*/
 * @throws IOException
 */
private File stashResourceContent(InputStream resourceInputStream) throws IOException {
    File resourceTempFile = null;
    OutputStream oStream = null;
    try {
        resourceTempFile = File.createTempFile("dtgov-ui-upload", ".tmp"); //$NON-NLS-1$ //$NON-NLS-2$
        oStream = FileUtils.openOutputStream(resourceTempFile);
        IOUtils.copy(resourceInputStream, oStream);
        return resourceTempFile;
    } catch (IOException e) {
        FileUtils.deleteQuietly(resourceTempFile);
        throw e;
    } finally {
        IOUtils.closeQuietly(resourceInputStream);
        IOUtils.closeQuietly(oStream);
    }
}

From source file:org.overlord.sramp.atom.archive.expand.ZipToSrampArchive.java

/**
 * Copies the JAR content from the input stream to the given output file.
 * @param jarStream// ww w.  j  a va  2  s .c  o  m
 * @param jarOutputFile
 * @throws IOException
 */
private static void copyJarStream(InputStream jarStream, File jarOutputFile) throws IOException {
    OutputStream oStream = null;
    try {
        oStream = FileUtils.openOutputStream(jarOutputFile);
        IOUtils.copy(jarStream, oStream);
    } finally {
        IOUtils.closeQuietly(jarStream);
        IOUtils.closeQuietly(oStream);
    }
}

From source file:org.overlord.sramp.atom.archive.expand.ZipToSrampArchiveTest.java

/**
 * Test method for {@link org.overlord.sramp.atom.archive.jar.ZipToSrampArchive.jar.JarToSrampArchive#JarToSrampArchive(java.io.File)}.
 *//*w ww.  j  a v a  2  s  . co  m*/
@Test
public void testJarToSrampArchiveFile() throws Exception {
    InputStream resourceAsStream = null;
    File tempFile = null;
    FileOutputStream tempFileStream = null;
    ZipToSrampArchive j2sramp = null;

    try {
        resourceAsStream = ZipToSrampArchiveTest.class.getResourceAsStream("sample-webservice-0.0.1.jar"); //$NON-NLS-1$
        tempFile = File.createTempFile("j2sramp_test", ".jar"); //$NON-NLS-1$ //$NON-NLS-2$
        tempFileStream = FileUtils.openOutputStream(tempFile);
        IOUtils.copy(resourceAsStream, tempFileStream);
    } finally {
        IOUtils.closeQuietly(resourceAsStream);
        IOUtils.closeQuietly(tempFileStream);
    }

    try {
        j2sramp = new ZipToSrampArchive(tempFile) {
        };

        File jarWorkDir = getJarWorkDir(j2sramp);
        Assert.assertNotNull(jarWorkDir);
        Assert.assertTrue(jarWorkDir.isDirectory());
        Collection<File> files = FileUtils.listFiles(jarWorkDir, new String[] { "xsd", "wsdl" }, true); //$NON-NLS-1$ //$NON-NLS-2$
        Assert.assertEquals(2, files.size());
        Set<String> fnames = new HashSet<String>();
        for (File f : files) {
            fnames.add(f.getName());
        }
        Assert.assertTrue(fnames.contains("teetime.xsd")); //$NON-NLS-1$
        Assert.assertTrue(fnames.contains("teetime.wsdl")); //$NON-NLS-1$
    } finally {
        FileUtils.deleteQuietly(tempFile);
        ZipToSrampArchive.closeQuietly(j2sramp);
    }
}