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.artifactory.util.ResourceUtils.java

public static void copyResource(String resourcePath, File outputFile, InputStreamManipulator manipulator,
        Class clazz) throws IOException {
    OutputStream os = null;//from  w  ww. j  a v a2 s  .  c om
    try {
        os = new BufferedOutputStream(FileUtils.openOutputStream(outputFile));
        copyResource(resourcePath, os, manipulator, clazz);
    } finally {
        IOUtils.closeQuietly(os);
    }
}

From source file:org.artificer.atom.archive.ArtificerArchive.java

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

From source file:org.artificer.atom.archive.ArtificerArchive.java

/**
 * Packs up the current contents of the S-RAMP archive into a single (.zip) file and
 * returns a reference to it.  This method is guaranteed to either throw an Exception
 * or return a valid {@link File}.  It will never throw and leave a temporary file
 * behind./*from w  w w.  j av  a 2 s .co m*/
 * @throws ArtificerArchiveException
 */
public File pack() throws ArtificerArchiveException {
    try {
        File archiveFile = null;
        try {
            archiveFile = File.createTempFile("artificer-archive", ".sramp");
            FileOutputStream outputStream = FileUtils.openOutputStream(archiveFile);
            ZipOutputStream zipOutputStream = null;
            try {
                zipOutputStream = new ZipOutputStream(outputStream);
                Collection<ArtificerArchiveEntry> entries = getEntries();
                for (ArtificerArchiveEntry entry : entries) {
                    packEntry(entry, zipOutputStream);
                }
            } finally {
                IOUtils.closeQuietly(zipOutputStream);
            }
        } catch (Throwable t) {
            // If anything goes wrong, make sure the File is cleaned up, as
            // we won't have another chance to do so.
            if (archiveFile != null && archiveFile.isFile())
                archiveFile.delete();
            throw t;
        }
        return archiveFile;
    } catch (Throwable t) {
        throw new ArtificerArchiveException(Messages.i18n.format("ERROR_PACKING_ARCHIVE"), t);
    }
}

From source file:org.artificer.common.ArtifactContent.java

public ArtifactContent(String path, InputStream is) throws IOException {
    if (path != null) {
        // Normalize and ensure it's a *relative* path.
        if (path.startsWith(File.separatorChar + "")) {
            path = path.replaceFirst(File.separatorChar + "", "");
        }// ww w.  ja v  a  2 s  .c  o  m
        this.path = path;
        int index = path.lastIndexOf(File.separatorChar);
        this.filename = path.substring(index + 1);
    } else {
        this.path = null;
        this.filename = null;
    }

    OutputStream os = null;
    try {
        tempFile = File.createTempFile(UUID.randomUUID().toString(), filename);
        os = FileUtils.openOutputStream(tempFile);
        IOUtils.copy(is, os);
    } catch (IOException e) {
        if (tempFile != null) {
            FileUtils.deleteQuietly(tempFile);
        }
        throw e;
    } finally {
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(os);
    }
}

From source file:org.artificer.ui.server.servlets.AbstractUploadServlet.java

/**
 * Make a temporary copy of the resource by saving the content to a temp file.
 * @param resourceInputStream/*from  ww  w  .j av  a2  s.c  o m*/
 * @throws IOException
 */
protected File stashResourceContent(InputStream resourceInputStream) throws IOException {
    File resourceTempFile = null;
    OutputStream oStream = null;
    try {
        resourceTempFile = File.createTempFile("artificer-ui-upload", ".tmp");
        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.bdval.BDVModel.java

/**
 * Save the model to a set of files. The files will contain all the information needed to
 * apply the BDVal model to new samples.
 *
 * @param options        The options associated with this model
 * @param task           The classification task used for this model
 * @param splitPlan      The split plan used to generat this model
 * @param writeModelMode The mode saving the model
 * @throws IOException if there is a problem writing to the files
 *//*  w  w w.  j  a  v  a2 s  .c  o  m*/
public void save(final DAVOptions options, final ClassificationTask task, final SplitPlan splitPlan,
        final WriteModel writeModelMode) throws IOException {
    if (zipModel) {
        LOG.info("Writing model to filename: " + zipFilename);
        ZipOutputStream zipStream = null;
        try {
            // Create the ZIP file
            zipStream = new ZipOutputStream(new FileOutputStream(zipFilename));
            save(zipStream, options, task, splitPlan, writeModelMode);
        } finally {
            IOUtils.closeQuietly(zipStream);
        }
    } else {
        LOG.info("Writing model properties  to filename: " + modelPropertiesFilename);
        saveProperties(FileUtils.openOutputStream(new File(modelPropertiesFilename)), options, task, splitPlan,
                writeModelMode);

        helper.model.write(modelFilename);
        if (options.scaleFeatures) {
            if (options.probesetScaleMeanMap.size() <= 0) {
                throw new IllegalArgumentException("mean map must be populated.");
            }
            if (options.probesetScaleRangeMap.size() <= 0) {
                throw new IllegalArgumentException("range map must be populated.");
            }
        }

        saveMeansMap(FileUtils.openOutputStream(new File(meansMapFilename)), options);
        saveRangeMap(FileUtils.openOutputStream(new File(rangeMapFilename)), options);

        savePlatform(FileUtils.openOutputStream(new File(platformFilename)), options);
    }
}

From source file:org.bdval.ConsensusBDVModel.java

/**
 * Loads the juror models used for consensus.
 * @param options specific options to use when loading the model
 * @throws IOException if there is a problem accessing the model
 * @throws ClassNotFoundException if the type of the model is not recognized
 *//*  w w  w.  j  a va2s .com*/
private void loadJurorModels(final DAVOptions options) throws IOException, ClassNotFoundException {
    jurorModels.clear();

    final String pathToModel = FilenameUtils.getFullPath(modelFilename);
    final String endpointName = FilenameUtils.getBaseName(FilenameUtils.getPathNoEndSeparator(pathToModel));

    if (properties.getBoolean("bdval.consensus.jurors.embedded", false)) {
        final File tmpdir = File.createTempFile("juror-models", "");
        tmpdir.delete();
        tmpdir.mkdir();

        try {
            // load juror models from the zip file
            final ZipFile zipFile = new ZipFile(zipFilename);
            for (final String jurorPrefix : jurorModelFilenamePrefixes) {
                // zip files should always use "/" as a separator
                final String jurorFilename = "models/" + endpointName + "/" + jurorPrefix + ".zip";
                LOG.debug("Loading juror model " + jurorFilename);
                final InputStream jurorStream = zipFile.getInputStream(zipFile.getEntry(jurorFilename));

                final File jurorFile = new File(FilenameUtils.concat(tmpdir.getPath(), jurorFilename));

                // put the juror model to disk so it can be loaded with existing code
                IOUtils.copy(jurorStream, FileUtils.openOutputStream(jurorFile));

                final BDVModel jurorModel = new BDVModel(jurorFile.getPath());
                jurorModel.load(options);
                jurorModels.add(jurorModel);
            }
        } finally {
            FileUtils.forceDeleteOnExit(tmpdir);
        }
    } else {
        // load juror models from disk
        final File finalModelPath = new File(pathToModel);
        final File finalModelParentPath = new File(finalModelPath.getParent());
        // assume the model is under a directory "models" at the same level as a models
        // directory which contains the model components.
        for (final String jurorPrefix : jurorModelFilenamePrefixes) {
            final String modelComponentFilename = finalModelParentPath.getParent() + SystemUtils.FILE_SEPARATOR
                    + "models" + SystemUtils.FILE_SEPARATOR + endpointName + SystemUtils.FILE_SEPARATOR
                    + jurorPrefix;
            LOG.debug("Loading model component " + modelComponentFilename);
            final BDVModel jurorModel = new BDVModel(modelComponentFilename);
            jurorModel.load(options);
            jurorModels.add(jurorModel);
        }
    }

    if (jurorModels.size() < 1) {
        throw new IllegalStateException("No juror models could be found");
    }

    jurorModelsAreLoaded = true;
}

From source file:org.bdval.ConsensusBDVModel.java

/**
 * Save the model to a set of files. The files will contain all the information needed to
 * apply the BDVal model to new samples.
 *
 * NOTE: For consensus models, the juror models must be loaded by calling
 * {@link #load(DAVOptions)} before this method is called
 *
 * @param options The options associated with this model
 * @param task The classification task used for this model
 * @param splitPlan The split plan used to generat this model
 * @param writeModelMode The mode saving the model
 * @throws IOException if there is a problem writing to the files
 *///from  w  w w  .j  a v a  2  s . c om
@Override
public void save(final DAVOptions options, final ClassificationTask task, final SplitPlan splitPlan,
        final WriteModel writeModelMode) throws IOException {
    if (zipModel) {
        LOG.info("Writing model to filename: " + zipFilename);
        ZipOutputStream zipStream = null;
        try {
            // Create the ZIP file
            zipStream = new ZipOutputStream(new FileOutputStream(zipFilename));
            setZipStreamComment(zipStream);

            // Add ZIP entry for the model properties to output stream.
            saveProperties(zipStream, options, task, splitPlan, writeModelMode);

            // the juror models will actually be contained within the consensus model
            saveJurorModels(zipStream, options, task, splitPlan, writeModelMode);
        } finally {
            IOUtils.closeQuietly(zipStream);
        }
    } else {
        LOG.info("Writing model properties to filename: " + modelPropertiesFilename);
        saveProperties(FileUtils.openOutputStream(new File(modelPropertiesFilename)), options, task, splitPlan,
                writeModelMode);
        // NOTE: old code never saved the juror models
    }
}

From source file:org.bigmouth.nvwa.network.ftp.client.SimpleFTPClient.java

@Override
public File download(String remotePath, String remoteFileName) throws IOException {
    FileOutputStream output = null;
    try {//from   w  ww . ja  v  a2 s .  c  o  m
        connect();
        File file = new File(
                PathUtils.appendEndFileSeparator(System.getProperty("java.io.tmpdir")) + remoteFileName);
        if (client.changeWorkingDirectory(remotePath)) {
            client.setFileType(FTPClient.BINARY_FILE_TYPE);
            client.setBufferSize(BUFF_SIZE);
            output = FileUtils.openOutputStream(file);
            if (!client.retrieveFile(remoteFileName, output)) {
                throw new IOException("File " + remoteFileName + " download failure!");
            }
            if (LOGGER.isDebugEnabled())
                LOGGER.debug("File " + remoteFileName + " Download successful.");
        }
        return file;
    } catch (IOException e) {
        throw new IOException(e);
    } finally {
        IOUtils.closeQuietly(output);
        if (autoDisconnect)
            disconnect();
    }
}

From source file:org.bigmouth.nvwa.network.ftp.client.SSHFTPClient.java

@Override
public File download(String remotePath, String remoteFileName) throws IOException {
    FileOutputStream output = null;
    try {//w  w w . j  a  v a  2  s  . c o m
        connect();
        channel.cd(remotePath);
        File file = new File(
                PathUtils.appendEndFileSeparator(System.getProperty("java.io.tmpdir")) + remoteFileName);
        output = FileUtils.openOutputStream(file);
        channel.get(remoteFileName, output);
        return file;
    } catch (Exception e) {
        throw new IOException(e);
    } finally {
        IOUtils.closeQuietly(output);
        if (autoDisconnect)
            disconnect();
    }
}