Search in sources :

Example 6 with CloseShieldOutputStream

use of org.apache.commons.io.output.CloseShieldOutputStream in project repseqio by repseqio.

the class GenerateClonesAction method go.

@Override
public void go(ActionHelper helper) throws Exception {
    GCloneModel model = GModels.getGCloneModelByName(params.getModelName());
    GCloneGenerator generator = model.create(new Well19937c(params.getSeed()), VDJCLibraryRegistry.getDefault());
    VDJCLibrary library = VDJCLibraryRegistry.getDefault().getLibrary(model.libraryId());
    try (BufferedOutputStream s = new BufferedOutputStream(params.getOutput().equals(".") ? System.out : new FileOutputStream(params.getOutput()), 128 * 1024)) {
        s.write(GlobalObjectMappers.toOneLine(model.libraryId()).getBytes());
        s.write('\n');
        ObjectWriter writer = GlobalObjectMappers.ONE_LINE.writerFor(new TypeReference<GClone>() {
        }).withAttribute(VDJCGene.JSON_CURRENT_LIBRARY_ATTRIBUTE_KEY, library);
        OUTER: for (int i = 0; i < params.numberOfClones; i++) {
            GClone clone = generator.sample();
            for (GGene g : clone.genes.values()) {
                NucleotideSequence cdr3 = g.getFeature(GeneFeature.CDR3);
                if (params.isInFrame())
                    if (cdr3.size() % 3 != 0) {
                        --i;
                        continue OUTER;
                    }
                if (params.isNoStops())
                    if (AminoAcidSequence.translateFromCenter(cdr3).containStops()) {
                        --i;
                        continue OUTER;
                    }
            }
            writer.writeValue(new CloseShieldOutputStream(s), clone);
            s.write('\n');
        }
    }
}
Also used : GCloneGenerator(io.repseq.gen.dist.GCloneGenerator) GGene(io.repseq.gen.GGene) FileOutputStream(java.io.FileOutputStream) NucleotideSequence(com.milaboratory.core.sequence.NucleotideSequence) VDJCLibrary(io.repseq.core.VDJCLibrary) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) TypeReference(com.fasterxml.jackson.core.type.TypeReference) GCloneModel(io.repseq.gen.dist.GCloneModel) Well19937c(org.apache.commons.math3.random.Well19937c) BufferedOutputStream(java.io.BufferedOutputStream) GClone(io.repseq.gen.GClone) CloseShieldOutputStream(org.apache.commons.io.output.CloseShieldOutputStream)

Example 7 with CloseShieldOutputStream

use of org.apache.commons.io.output.CloseShieldOutputStream in project deeplearning4j by deeplearning4j.

the class ModelSerializer method writeModel.

/**
     * Write a model to an output stream
     * @param model the model to save
     * @param stream the output stream to write to
     * @param saveUpdater whether to save the updater for the model or not
     * @throws IOException
     */
public static void writeModel(@NonNull Model model, @NonNull OutputStream stream, boolean saveUpdater) throws IOException {
    ZipOutputStream zipfile = new ZipOutputStream(new CloseShieldOutputStream(stream));
    // Save configuration as JSON
    String json = "";
    if (model instanceof MultiLayerNetwork) {
        json = ((MultiLayerNetwork) model).getLayerWiseConfigurations().toJson();
    } else if (model instanceof ComputationGraph) {
        json = ((ComputationGraph) model).getConfiguration().toJson();
    }
    ZipEntry config = new ZipEntry("configuration.json");
    zipfile.putNextEntry(config);
    zipfile.write(json.getBytes());
    // Save parameters as binary
    ZipEntry coefficients = new ZipEntry("coefficients.bin");
    zipfile.putNextEntry(coefficients);
    DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(zipfile));
    try {
        Nd4j.write(model.params(), dos);
    } finally {
        dos.flush();
        if (!saveUpdater)
            dos.close();
    }
    if (saveUpdater) {
        INDArray updaterState = null;
        if (model instanceof MultiLayerNetwork) {
            updaterState = ((MultiLayerNetwork) model).getUpdater().getStateViewArray();
        } else if (model instanceof ComputationGraph) {
            updaterState = ((ComputationGraph) model).getUpdater().getStateViewArray();
        }
        if (updaterState != null && updaterState.length() > 0) {
            ZipEntry updater = new ZipEntry(UPDATER_BIN);
            zipfile.putNextEntry(updater);
            try {
                Nd4j.write(updaterState, dos);
            } finally {
                dos.flush();
                dos.close();
            }
        }
    }
    zipfile.close();
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) ComputationGraph(org.deeplearning4j.nn.graph.ComputationGraph) MultiLayerNetwork(org.deeplearning4j.nn.multilayer.MultiLayerNetwork) CloseShieldOutputStream(org.apache.commons.io.output.CloseShieldOutputStream)

Example 8 with CloseShieldOutputStream

use of org.apache.commons.io.output.CloseShieldOutputStream in project jena by apache.

the class RdfStats method run.

@Override
public int run(String[] args) {
    try (ColorizedOutputStream<BasicColor> error = new AnsiBasicColorizedOutputStream(new CloseShieldOutputStream(System.err))) {
        try {
            if (args.length == 0) {
                showUsage();
            }
            // Parse custom arguments
            RdfStats cmd = SingleCommand.singleCommand(RdfStats.class).parse(args);
            // Copy Hadoop configuration across
            cmd.setConf(this.getConf());
            // Show help if requested and exit with success
            if (cmd.helpOption.showHelpIfRequested()) {
                return 0;
            }
            // Run the command and exit with success
            cmd.run();
            return 0;
        } catch (ParseException e) {
            error.setForegroundColor(BasicColor.RED);
            error.println(e.getMessage());
            error.println();
        } catch (Throwable e) {
            error.setForegroundColor(BasicColor.RED);
            error.println(e.getMessage());
            e.printStackTrace(error);
            error.println();
        }
    }
    return 1;
}
Also used : BasicColor(com.github.rvesse.airline.io.colors.BasicColor) AnsiBasicColorizedOutputStream(com.github.rvesse.airline.io.output.AnsiBasicColorizedOutputStream) ParseException(com.github.rvesse.airline.parser.errors.ParseException) CloseShieldOutputStream(org.apache.commons.io.output.CloseShieldOutputStream)

Example 9 with CloseShieldOutputStream

use of org.apache.commons.io.output.CloseShieldOutputStream in project lucene-solr by apache.

the class LoadAdminUiServlet method doGet.

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    // security: SOLR-7966 - avoid clickjacking for admin interface
    response.addHeader("X-Frame-Options", "DENY");
    // This attribute is set by the SolrDispatchFilter
    String admin = request.getRequestURI().substring(request.getContextPath().length());
    CoreContainer cores = (CoreContainer) request.getAttribute("org.apache.solr.CoreContainer");
    InputStream in = getServletContext().getResourceAsStream(admin);
    Writer out = null;
    if (in != null && cores != null) {
        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/html");
            // Protect container owned streams from being closed by us, see SOLR-8933
            out = new OutputStreamWriter(new CloseShieldOutputStream(response.getOutputStream()), StandardCharsets.UTF_8);
            String html = IOUtils.toString(in, "UTF-8");
            Package pack = SolrCore.class.getPackage();
            String[] search = new String[] { "${contextPath}", "${adminPath}", "${version}" };
            String[] replace = new String[] { StringEscapeUtils.escapeJavaScript(request.getContextPath()), StringEscapeUtils.escapeJavaScript(CommonParams.CORES_HANDLER_PATH), StringEscapeUtils.escapeJavaScript(pack.getSpecificationVersion()) };
            out.write(StringUtils.replaceEach(html, search, replace));
        } finally {
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        }
    } else {
        response.sendError(404);
    }
}
Also used : CoreContainer(org.apache.solr.core.CoreContainer) InputStream(java.io.InputStream) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) CloseShieldOutputStream(org.apache.commons.io.output.CloseShieldOutputStream)

Example 10 with CloseShieldOutputStream

use of org.apache.commons.io.output.CloseShieldOutputStream in project coprhd-controller by CoprHD.

the class SupportOrderPackageCreator method nextEntry.

private OutputStream nextEntry(ZipOutputStream zip, String path) throws IOException {
    Logger.debug("Adding entry: %s", path);
    ZipEntry entry = new ZipEntry(path);
    zip.putNextEntry(entry);
    return new CloseShieldOutputStream(zip);
}
Also used : ZipEntry(java.util.zip.ZipEntry) CloseShieldOutputStream(org.apache.commons.io.output.CloseShieldOutputStream)

Aggregations

CloseShieldOutputStream (org.apache.commons.io.output.CloseShieldOutputStream)12 ZipEntry (java.util.zip.ZipEntry)4 BasicColor (com.github.rvesse.airline.io.colors.BasicColor)2 AnsiBasicColorizedOutputStream (com.github.rvesse.airline.io.output.AnsiBasicColorizedOutputStream)2 Base64OutputStream (org.apache.commons.codec.binary.Base64OutputStream)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 ParseException (com.github.rvesse.airline.parser.errors.ParseException)1 NucleotideSequence (com.milaboratory.core.sequence.NucleotideSequence)1 VDJCLibrary (io.repseq.core.VDJCLibrary)1 GClone (io.repseq.gen.GClone)1 GGene (io.repseq.gen.GGene)1 GCloneGenerator (io.repseq.gen.dist.GCloneGenerator)1 GCloneModel (io.repseq.gen.dist.GCloneModel)1 BufferedOutputStream (java.io.BufferedOutputStream)1 EOFException (java.io.EOFException)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1