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');
}
}
}
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();
}
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;
}
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);
}
}
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);
}
Aggregations