Example usage for java.util.zip GZIPOutputStream close

List of usage examples for java.util.zip GZIPOutputStream close

Introduction

In this page you can find the example usage for java.util.zip GZIPOutputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Writes remaining compressed data to the output stream and closes the underlying stream.

Usage

From source file:org.openmrs.module.odkconnector.serialization.serializer.ListSerializerTest.java

@Test
public void serialize_shouldSerializePatientInformation() throws Exception {
    File file = File.createTempFile("PatientSerialization", "Example");
    GZIPOutputStream outputStream = new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(file)));

    log.info("Writing to: " + file.getAbsolutePath());

    Cohort cohort = new Cohort();
    cohort.addMember(6);//ww  w . j a  v  a  2  s. c  om
    cohort.addMember(7);
    cohort.addMember(8);

    List<Patient> patients = new ArrayList<Patient>();
    List<Obs> observations = new ArrayList<Obs>();
    List<Form> forms = new ArrayList<Form>();

    for (Integer patientId : cohort.getMemberIds()) {
        Patient patient = Context.getPatientService().getPatient(patientId);
        observations.addAll(Context.getObsService().getObservationsByPerson(patient));
        patients.add(patient);
    }

    Serializer serializer = HandlerUtil.getPreferredHandler(Serializer.class, List.class);
    serializer.write(outputStream, patients);
    serializer.write(outputStream, observations);
    serializer.write(outputStream, forms);

    outputStream.close();

    GZIPInputStream inputStream = new GZIPInputStream(new BufferedInputStream(new FileInputStream(file)));
    DataInputStream dataInputStream = new DataInputStream(inputStream);

    // total number of patients
    Integer patientCounter = dataInputStream.readInt();
    System.out.println("Patient Counter: " + patientCounter);
    for (int i = 0; i < patientCounter; i++) {
        System.out.println("=================Patient=====================");
        System.out.println("Patient Id: " + dataInputStream.readInt());
        System.out.println("Family Name: " + dataInputStream.readUTF());
        System.out.println("Middle Name: " + dataInputStream.readUTF());
        System.out.println("Last Name: " + dataInputStream.readUTF());
        System.out.println("Gender: " + dataInputStream.readUTF());
        System.out.println("Birth Date: " + dataInputStream.readLong());
        System.out.println("Identifier" + dataInputStream.readUTF());
    }

    Integer obsCounter = dataInputStream.readInt();
    for (int j = 0; j < obsCounter; j++) {
        System.out.println("==================Observation=================");
        System.out.println("Patient Id: " + dataInputStream.readInt());
        System.out.println("Concept Name: " + dataInputStream.readUTF());

        byte type = dataInputStream.readByte();
        if (type == ObsSerializer.TYPE_STRING)
            System.out.println("Value: " + dataInputStream.readUTF());
        else if (type == ObsSerializer.TYPE_INT)
            System.out.println("Value: " + dataInputStream.readInt());
        else if (type == ObsSerializer.TYPE_DOUBLE)
            System.out.println("Value: " + dataInputStream.readDouble());
        else if (type == ObsSerializer.TYPE_DATE)
            System.out.println("Value: " + dataInputStream.readLong());
        System.out.println("Time: " + dataInputStream.readLong());
    }

    Integer formCounter = dataInputStream.readInt();
    for (int j = 0; j < formCounter; j++) {
        System.out.println("==================Form=================");
        System.out.println("Form Id: " + dataInputStream.readInt());
    }

    System.out.println();

    inputStream.close();
}

From source file:org.openmrs.module.odkconnector.serialization.serializer.openmrs.PatientSerializerTest.java

@Test
public void serialize_shouldSerializePatientInformation() throws Exception {
    File file = File.createTempFile("PatientSerialization", "Example");
    GZIPOutputStream outputStream = new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(file)));

    log.info("Writing to: " + file.getAbsolutePath());

    Cohort cohort = new Cohort();
    cohort.addMember(6);/*from   w  w  w  . jav a2s. c o  m*/
    cohort.addMember(7);
    cohort.addMember(8);

    List<Patient> patients = new ArrayList<Patient>();
    List<Obs> observations = new ArrayList<Obs>();
    List<Form> forms = new ArrayList<Form>();

    for (Integer patientId : cohort.getMemberIds()) {
        Patient patient = Context.getPatientService().getPatient(patientId);
        observations.addAll(Context.getObsService().getObservationsByPerson(patient));
        patients.add(patient);
    }

    Serializer serializer = HandlerUtil.getPreferredHandler(Serializer.class, List.class);
    serializer.write(outputStream, patients);
    serializer.write(outputStream, observations);
    serializer.write(outputStream, forms);

    outputStream.close();

    GZIPInputStream inputStream = new GZIPInputStream(new BufferedInputStream(new FileInputStream(file)));
    DataInputStream dataInputStream = new DataInputStream(inputStream);

    // total number of patients
    Integer patientCounter = dataInputStream.readInt();
    System.out.println("Patient Counter: " + patientCounter);
    for (int i = 0; i < patientCounter; i++) {
        System.out.println("=================Patient=====================");
        System.out.println("Patient Id: " + dataInputStream.readInt());
        System.out.println("Family Name: " + dataInputStream.readUTF());
        System.out.println("Middle Name: " + dataInputStream.readUTF());
        System.out.println("Last Name: " + dataInputStream.readUTF());
        System.out.println("Gender: " + dataInputStream.readUTF());
        System.out.println("Birth Date: " + dataInputStream.readUTF());
        System.out.println("Identifier" + dataInputStream.readUTF());
    }

    Integer obsCounter = dataInputStream.readInt();
    for (int j = 0; j < obsCounter; j++) {
        System.out.println("==================Observation=================");
        System.out.println("Patient Id: " + dataInputStream.readInt());
        System.out.println("Concept Name: " + dataInputStream.readUTF());

        byte type = dataInputStream.readByte();
        if (type == ObsSerializer.TYPE_STRING)
            System.out.println("Value: " + dataInputStream.readUTF());
        else if (type == ObsSerializer.TYPE_INT)
            System.out.println("Value: " + dataInputStream.readInt());
        else if (type == ObsSerializer.TYPE_DOUBLE)
            System.out.println("Value: " + dataInputStream.readDouble());
        else if (type == ObsSerializer.TYPE_DATE)
            System.out.println("Value: " + dataInputStream.readUTF());
        System.out.println("Time: " + dataInputStream.readUTF());
    }

    Integer formCounter = dataInputStream.readInt();
    for (int j = 0; j < formCounter; j++) {
        System.out.println("==================Form=================");
        System.out.println("Form Id: " + dataInputStream.readInt());
    }

    System.out.println();

    inputStream.close();
}

From source file:org.mskcc.cbio.portal.servlet.NetworkServlet.java

public void processGetNetworkRequest(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException {
    try {//from   ww  w. j  a v  a2  s.c o m
        StringBuilder messages = new StringBuilder();

        XDebug xdebug = new XDebug(req);

        String xd = req.getParameter("xdebug");
        boolean logXDebug = xd != null && xd.equals("1");

        //  Get User Defined Gene List
        String geneListStr = req.getParameter(QueryBuilder.GENE_LIST);
        Set<String> queryGenes = new HashSet<String>(Arrays.asList(geneListStr.toUpperCase().split("[, ]+")));
        int nMiRNA = filterNodes(queryGenes);
        if (nMiRNA > 0) {
            messages.append("MicroRNAs were excluded from the network query. ");
        }

        //String geneticProfileIdSetStr = xssUtil.getCleanInput (req, QueryBuilder.GENETIC_PROFILE_IDS);

        String netSrc = req.getParameter("netsrc");
        String strNetSize = req.getParameter("netsize");
        NetworkIO.NetworkSize netSize;
        try {
            netSize = NetworkIO.NetworkSize.valueOf(strNetSize.toUpperCase());
        } catch (Exception e) {
            netSize = NetworkIO.NetworkSize.LARGE;
        }

        String dsSrc = req.getParameter("source");
        List<String> dataSources = dsSrc == null ? null : Arrays.asList(dsSrc.split("[, ]+"));

        Network network;
        xdebug.startTimer();
        if ("cpath2".equalsIgnoreCase(netSrc)) {
            network = NetworkIO.readNetworkFromCPath2(queryGenes, true);
            if (logXDebug) {
                xdebug.logMsg("GetPathwayCommonsNetwork", "<a href=\"" + NetworkIO.getCPath2URL(queryGenes)
                        + "\" target=\"_blank\">cPath2 URL</a>");
            }
        } else {
            network = NetworkIO.readNetworkFromCGDS(queryGenes, netSize, dataSources, true);
        }

        //network = NetworkIO.readNetworkFromCPath2(queryGenes, true);

        xdebug.stopTimer();
        xdebug.logMsg(this,
                "Successfully retrieved networks from " + netSrc + ": took " + xdebug.getTimeElapsed() + "ms");

        // get cancer study id
        // if cancer study id is null, return the current network
        String cancerStudyId = req.getParameter(QueryBuilder.CANCER_STUDY_ID);
        CancerStudy cancerStudy = DaoCancerStudy.getCancerStudyByStableId(cancerStudyId);

        if (network.countNodes() != 0 && cancerStudyId != null) {

            // add attribute is_query to indicate if a node is in query genes
            // and get the list of genes in network
            xdebug.logMsg(this, "Retrieving data from CGDS...");

            // Get sample ids
            Set<String> targetSampleIds = getSampleIds(req, cancerStudyId);
            List<Integer> internalSampleIds = InternalIdUtil.getInternalNonNormalSampleIds(
                    cancerStudy.getInternalId(), new ArrayList<String>(targetSampleIds));

            //  Get User Selected Genetic Profiles
            Set<GeneticProfile> geneticProfileSet = getGeneticProfileSet(req, cancerStudyId);

            // getzScoreThreshold
            double zScoreThreshold = Double.parseDouble(req.getParameter(QueryBuilder.Z_SCORE_THRESHOLD));

            xdebug.startTimer();

            Map<String, Map<String, Integer>> mapQueryGeneAlterationCaseNumber = getMapQueryGeneAlterationCaseNumber(
                    req);

            DaoGeneOptimized daoGeneOptimized = DaoGeneOptimized.getInstance();

            Set<Node> queryNodes = new HashSet<Node>();
            for (Node node : network.getNodes()) {
                if (node.getType().equals(NodeType.DRUG))
                    continue;

                String ngnc = NetworkUtils.getSymbol(node);
                if (ngnc == null) {
                    continue;
                }

                if (mapQueryGeneAlterationCaseNumber != null) {
                    if (queryGenes.contains(ngnc)) {
                        queryNodes.add(node);
                        continue;
                    }
                }

                CanonicalGene canonicalGene = daoGeneOptimized.getGene(ngnc);
                if (canonicalGene == null) {
                    continue;
                }

                long entrezGeneId = canonicalGene.getEntrezGeneId();

                // add attributes
                addCGDSDataAsNodeAttribute(node, entrezGeneId, geneticProfileSet, internalSampleIds,
                        zScoreThreshold);
            }

            xdebug.stopTimer();
            xdebug.logMsg(this, "Retrived data from CGDS. Took " + xdebug.getTimeElapsed() + "ms");

            if (mapQueryGeneAlterationCaseNumber != null) {
                addAttributesForQueryGenes(queryNodes, internalSampleIds.size(),
                        mapQueryGeneAlterationCaseNumber);
            }

            String nLinker = req.getParameter("linkers");
            if (nLinker != null && nLinker.matches("[0-9]+")) {
                int nBefore = network.countNodes();
                int querySize = queryGenes.size();
                String strDiffusion = req.getParameter("diffusion");
                double diffusion;
                try {
                    diffusion = Double.parseDouble(strDiffusion);
                } catch (Exception ex) {
                    diffusion = 0;
                }

                xdebug.startTimer();
                pruneNetworkByAlteration(network, diffusion, Integer.parseInt(nLinker), querySize);
                int nAfter = network.countNodes(true);
                if (nBefore != nAfter) {
                    messages.append("The network below contains ");
                    messages.append(nAfter);
                    messages.append(" nodes, including your ");
                    messages.append(querySize);
                    messages.append(" query gene");
                    if (querySize > 1) {
                        messages.append("s");
                    }
                    messages.append(" and the ");
                    messages.append(nAfter - querySize);
                    messages.append(" most frequently altered neighbor genes ");
                    messages.append(" (out of a total of ");
                    messages.append(nBefore - querySize);
                    messages.append(").\n");
                }
                xdebug.stopTimer();
                xdebug.logMsg(this, "Prune network. Took " + xdebug.getTimeElapsed() + "ms");
            }

            String encodedQueryAlteration = encodeQueryAlteration(mapQueryGeneAlterationCaseNumber);

            if (logXDebug) {
                xdebug.logMsg(this,
                        "<a href=\"" + getNetworkServletUrl(req, false, false, false, encodedQueryAlteration)
                                + "\" target=\"_blank\">NetworkServlet URL</a>");
            }

            messages.append("Download the complete network in ");
            messages.append("<a href=\"");
            messages.append(getNetworkServletUrl(req, true, true, false, encodedQueryAlteration));
            messages.append("\" target=\"_blank\">GraphML</a> ");
            messages.append("or <a href=\"");
            messages.append(getNetworkServletUrl(req, true, true, true, encodedQueryAlteration));
            messages.append("\" target=\"_blank\">SIF</a>");
            messages.append(
                    " for import into <a href=\"http://cytoscape.org\" target=\"_blank\">Cytoscape</a>");
            messages.append(" (<a href=\"http://chianti.ucsd.edu/cyto_web/plugins/displayplugininfo.php?");
            messages.append("name=GraphMLReader\" target=\"_blank\">GraphMLReader plugin</a>");
            messages.append(" is required for importing GraphML).");
        }

        String format = req.getParameter("format");
        boolean sif = format != null && format.equalsIgnoreCase("sif");

        String download = req.getParameter("download");
        if (download != null && download.equalsIgnoreCase("on")) {
            res.setContentType("application/octet-stream");
            res.addHeader("content-disposition",
                    "attachment; filename=cbioportal." + (sif ? "sif" : "graphml"));
            messages.append("In order to open this file in Cytoscape, please install GraphMLReader plugin.");
        } else {
            res.setContentType("text/" + (sif ? "plain" : "xml"));
        }

        String gzip = req.getParameter("gzip");
        boolean isGzip = gzip != null && gzip.equalsIgnoreCase("on");
        if (isGzip) {
            res.setHeader("Content-Encoding", "gzip");
        }

        NetworkIO.NodeLabelHandler nodeLabelHandler = new NetworkIO.NodeLabelHandler() {
            // using HGNC gene symbol as label if available
            public String getLabel(Node node) {
                if (node.getType().equals(NodeType.DRUG))
                    return (String) node.getAttribute("NAME");

                String symbol = NetworkUtils.getSymbol(node);
                if (symbol != null) {
                    return symbol;
                }

                Object strNames = node.getAttributes().get("PARTICIPANT_NAME");
                if (strNames != null) {
                    String[] names = strNames.toString().split(";", 2);
                    if (names.length > 0) {
                        return names[0];
                    }
                }

                return node.getId();
            }
        };

        String graph;
        if (sif) {
            graph = NetworkIO.writeNetwork2Sif(network, nodeLabelHandler);
        } else {
            graph = NetworkIO.writeNetwork2GraphML(network, nodeLabelHandler);
        }

        if (logXDebug) {
            writeXDebug(xdebug, res);
        }

        String msgoff = req.getParameter("msgoff");
        if ((msgoff == null || !msgoff.equals("t")) && messages.length() > 0) {
            writeMsg(messages.toString(), res);
        }

        if (isGzip) {
            GZIPOutputStream out = new GZIPOutputStream(res.getOutputStream());
            out.write(graph.getBytes());
            out.close();
        } else {
            PrintWriter writer = res.getWriter();
            writer.write(graph);
            writer.close();
        }
    } catch (Exception e) {
        //throw new ServletException (e);
        writeMsg("Error loading network. Please report this to " + GlobalProperties.getEmailContact() + "!\n"
                + e.toString(), res);
        res.getWriter().write("<graphml></graphml>");
    }
}

From source file:es.upm.fi.dia.oeg.sitemap4rdf.Generator.java

/**
 * Save as zip file the given XML Document using a given file name 
 * @param fileName the name of the generated zip file
 * @param doc the XML document to store in the zip file 
 *//*from  w ww . j  a  v a  2 s .co m*/
protected void saveZipFile(String fileName, Document doc) {
    String outfileName = fileName + zipFileExtension;
    if (outputDir != null && !outputDir.isEmpty())
        outfileName = outputDir + outfileName;

    try {

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        Source xmlSource = new DOMSource(doc);

        Result outputTarget = new StreamResult(new OutputStreamWriter(outputStream, DEFAULT_ENCODING));
        TransformerFactory tf = TransformerFactory.newInstance();
        tf.setAttribute(xmlAttributeIdentNumber, new Integer(4));

        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, DEFAULT_ENCODING);
        transformer.setOutputProperty("{http://xml. customer .org/xslt}indent-amount", "4");
        transformer.transform(xmlSource, outputTarget);

        InputStream in = new ByteArrayInputStream(outputStream.toString(DEFAULT_ENCODING).getBytes());

        byte[] buf = new byte[1024];
        //create the zip file
        GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(outfileName));

        //Transfer bytes from the inputstream to the ZIP
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }

        //Complete the entry
        in.close();

        //complete the zip file
        out.finish();
        out.close();
    } catch (FileNotFoundException e) {
        logger.debug("FileNotFoundException ", e);
        System.err.println(e.getMessage());
        System.exit(3);
    } catch (IOException e) {
        logger.debug("IOException ", e);
        System.err.println(e.getMessage());
        System.exit(3);
    } catch (TransformerConfigurationException e) {
        logger.debug("TransformerConfigurationException ", e);
        System.err.println(e.getMessage());
        System.exit(3);
    } catch (TransformerException e) {
        logger.debug("TransformerException ", e);
        System.err.println(e.getMessage());
        System.exit(3);
    } catch (TransformerFactoryConfigurationError e) {
        logger.debug("TransformerFactoryConfigurationError ", e);
        System.err.println(e.getMessage());
        System.exit(3);
    }
}

From source file:bookkeepr.xmlable.DatabaseManager.java

public synchronized void save(List<Session> sessions) throws BookKeeprException {
    if (!rootPath.exists()) {
        rootPath.mkdirs();/*w  ww .  j  a va 2  s  .co  m*/
    }

    if (sessions == null) {
        Logger.getLogger(DatabaseManager.class.getName()).log(Level.FINE, "Saving null session");
        try {
            XMLWriter.write(new FileOutputStream(rootPath.getPath() + File.separator + "_index.xml"), this);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(DatabaseManager.class.getName()).log(Level.SEVERE, null, ex);
            throw new BookKeeprException(ex);
        }
        return;
    }

    ArrayList<IdAble> externals = new ArrayList<IdAble>();
    for (Session session : sessions) {
        if (session.getExternalItems() != null) {
            externals.addAll(session.getExternalItems());
        }
    }
    if (externals.size() > 0) {
        this.saveExternals(externals);
    }

    ArrayList<String> keys = new ArrayList<String>();

    HashMap<Long, Boolean> isModified = new HashMap<Long, Boolean>();

    for (Session session : sessions) {
        if (session.getModified() == null || session.getModified().size() == 0) {
            // don't bother saving the session if it's empty.
            // It's probably just dealing with externals.
            continue;
        }

        // add the session to the list of things to write.
        session.addModifiedItem(session);

        //@todo: We really want to write files then add, but that might require some more work.
        // Then add all the new items.
        for (IdAble item : session.getModified()) {

            Index index = indicies.get(getKey(item.getId()));
            if (index == null) {
                if (!(item instanceof DeletedId)) {
                    index = TypeIdManager.getIndexFromClass(item.getClass());
                    String key = getKey(item.getId());
                    if (indicies.get(key) != null) {
                        indicies.remove(key);

                    }
                    indicies.put(key, index);
                }
            }

            if (item instanceof DeletedId) {
                if (index != null) {
                    // if we are deleting, and the index exists, we just remove the item!
                    IdAble deleted = index.getItem(item.getId());

                    if (deleted != null) {
                        index.remove(item);
                        isModified.put(item.getId(), true);
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-DD");
                        String datestr = format.format(new Date());
                        // save the item to the trash
                        File trashPath = new File(
                                rootPath.getPath() + File.separator + "trash" + File.separator + datestr);
                        if (!trashPath.exists()) {
                            trashPath.mkdirs();
                        }
                        GZIPOutputStream out;
                        try {
                            out = new GZIPOutputStream(new FileOutputStream(trashPath + File.separator
                                    + StringConvertable.ID.toString(item.getId()) + ".xml.gz"));
                            XMLWriter.write(out, deleted);
                            out.close();
                        } catch (IOException ex) {
                            Logger.getLogger(DatabaseManager.class.getName()).log(Level.SEVERE,
                                    "Error saving deleted item!", ex);
                            throw new BookKeeprException(ex);
                        }
                    }
                }
            } else {
                // otherwise we will have definitely created the index so we should add it.
                isModified.put(item.getId(), index.addItemIsExisting(item));
                int type = getType(item);
                int origin = getOrigin(item);
                if (origin > this.maxOriginId) {
                    this.maxOriginId = origin;
                }
                if (item.getId() > latestIds[origin][type]) {
                    latestIds[origin][type] = item.getId();
                }
            }
        }

        for (IdAble item : session.getModified()) {
            long id = item.getId();
            String key = getKey(id);
            if (!keys.contains(key)) {
                keys.add(key);
            }
        }
    }
    HashMap<String, File> files = new HashMap<String, File>();
    for (String key : keys) {
        OutputStream out = null;
        File tmpfile = null;
        try {
            tmpfile = File.createTempFile("db" + key, "wrk.gz", rootPath);
            tmpfile.deleteOnExit();
            out = new GZIPOutputStream(new FileOutputStream(tmpfile));
            XMLWriter.write(out, indicies.get(key));
            files.put(rootPath.getPath() + File.separator + key + ".xml.gz", tmpfile);
        } catch (IOException ex) {
            Logger.getLogger(DatabaseManager.class.getName()).log(Level.SEVERE, null, ex);
            throw new BookKeeprException(ex);
        } finally {
            try {
                out.close();
            } catch (IOException ex) {
                Logger.getLogger(DatabaseManager.class.getName()).log(Level.SEVERE, null, ex);
                throw new BookKeeprException(ex);
            }
        }
    }
    for (String fname : files.keySet()) {
        files.get(fname).renameTo(new File(fname));
    }

    try {
        XMLWriter.write(new FileOutputStream(rootPath.getPath() + File.separator + "_index.xml"), this);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(DatabaseManager.class.getName()).log(Level.SEVERE, null, ex);
    }
    for (Session session : sessions) {
        if (session.getModified() == null || session.getModified().size() == 0) {
            // don't bother saving the session if it's empty.
            // It's probably just dealing with externals.
            continue;
        }
        // notify listeners.
        for (IdAble item : session.getModified()) {

            for (Object listener : listeners[this.getType(item)]) {
                //                    Logger.getLogger(DatabaseManager.class.getName()).log(Level.FINE, "Notifying " + ((ChangeListener) listener) + " about " + item);
                if (item instanceof DeletedId) {
                    ((ChangeListener) listener).itemUpdated(this, item, this.getOrigin(item) != this.originId,
                            true);
                } else {
                    ((ChangeListener) listener).itemUpdated(this, item, this.getOrigin(item) != this.originId,
                            isModified.get(item.getId()));
                }
            }

        }

        // then close the session
        session.setClosed(true);
    }
}

From source file:com.bt.download.android.gui.httpserver.BrowseHandler.java

@Override
public void handle(HttpExchange exchange) throws IOException {
    assertUPnPActive();//from  ww w  . j a  v a  2 s  . c om

    GZIPOutputStream os = null;

    byte type = -1;

    try {

        List<NameValuePair> query = URLEncodedUtils.parse(exchange.getRequestURI(), "UTF-8");

        for (NameValuePair item : query) {
            if (item.getName().equals("type")) {
                type = Byte.parseByte(item.getValue());
            }
        }

        if (type == -1) {
            exchange.sendResponseHeaders(Code.HTTP_BAD_REQUEST, 0);
            return;
        }

        String response = getResponse(exchange, type);

        exchange.getResponseHeaders().set("Content-Encoding", "gzip");
        exchange.getResponseHeaders().set("Content-Type", "text/json; charset=UTF-8");
        exchange.sendResponseHeaders(Code.HTTP_OK, 0);

        os = new GZIPOutputStream(exchange.getResponseBody());

        os.write(response.getBytes("UTF-8"));
        os.finish();

    } catch (IOException e) {
        LOG.warning("Error browsing files type=" + type);
        throw e;
    } finally {
        if (os != null) {
            os.close();
        }
        exchange.close();
    }
}

From source file:it.drwolf.ridire.session.async.Mapper.java

private void createArchivedResource(File f, CrawledResource cr, EntityManager entityManager) {
    // System.out.println(System.getProperty("java.io.tmpdir"));
    String posEnabled = this.em.find(Parameter.class, Parameter.POS_ENABLED.getKey()).getValue();
    File resourceDir;/*from  ww  w  . j  a  va 2s.c  o  m*/
    int status = Parameter.FINISHED;
    try {
        resourceDir = new File(FilenameUtils.getFullPath(f.getCanonicalPath().replaceAll("__\\d+", ""))
                + JobMapperMonitor.RESOURCESDIR);
        if (!resourceDir.exists()) {
            FileUtils.forceMkdir(resourceDir);
        }
        ArchiveReader reader = ArchiveReaderFactory.get(f);
        ARCRecord record = (ARCRecord) reader.get(cr.getOffset());
        record.skipHttpHeader();
        byte[] buf = new byte[Mapper.BUFLENGTH];
        int count = 0;
        String resourceFile = cr.getDigest() + ".gz";
        GZIPOutputStream baos = new GZIPOutputStream(new FileOutputStream(new File(resourceDir, resourceFile)));
        while ((count = record.read(buf)) != -1) {
            baos.write(buf, 0, count);
        }
        baos.finish();
        baos.close();
        reader.close();
        // long t1 = System.currentTimeMillis();
        StringWithEncoding cleanText = this.createPlainTextResource(f, cr, entityManager);
        this.removeGZippedResource(resourceDir, resourceFile);
        // long t2 = System.currentTimeMillis();
        // System.out.println("Creazione plain text: " + (t2 - t1));
        String plainTextFileName = cr.getDigest() + ".txt";
        if (cleanText != null && cleanText.getString() != null && cleanText.getString().trim().length() > 0
                && cleanText.getCleaner() != null && (cleanText.getCleaner().equals(Mapper.ALCHEMY)
                        || cleanText.getCleaner().equals(Mapper.READABILITY))) {
            cr.setCleaner(cleanText.getCleaner());
            File plainTextFile = new File(resourceDir, plainTextFileName);
            FileUtils.writeStringToFile(plainTextFile, cleanText.getString(), cleanText.getEncoding());
            cr.setExtractedTextHash(MD5DigestCreator.getMD5Digest(plainTextFile));
            // language detection
            // t1 = System.currentTimeMillis();
            String language = this.detectLanguage(cleanText.getString());
            // t2 = System.currentTimeMillis();
            // System.out.println("Language detection: " + (t2 - t1));
            cr.setLanguage(language);
            if (language != null && language.equalsIgnoreCase(Mapper.ITALIAN) && posEnabled != null
                    && posEnabled.equalsIgnoreCase("true")) {
                // PoS tag if it's an italian text
                // t1 = System.currentTimeMillis();
                String posTagResourceFileName = this.createPoSTagResource(plainTextFile, entityManager,
                        cleanText.getEncoding());
                // t2 = System.currentTimeMillis();
                // System.out.println("PoS tagging: " + (t2 - t1));
                if (posTagResourceFileName != null) {
                    Integer wordsNumber = Mapper.countWordsFromPoSTagResource(posTagResourceFileName);
                    cr.setWordsNumber(wordsNumber);
                }
            }
        }
    } catch (Exception e) {
        status = Parameter.PROCESSING_ERROR;
        e.printStackTrace();
    }
    cr.setProcessed(status);
}

From source file:com.ichi2.libanki.sync.BasicHttpSyncer.java

public HttpResponse req(String method, InputStream fobj, int comp, boolean hkey, JSONObject registerData,
        Connection.CancelCallback cancelCallback) {
    File tmpFileBuffer = null;// ww  w.j ava 2  s  .com
    try {
        String bdry = "--" + BOUNDARY;
        StringWriter buf = new StringWriter();
        // compression flag and session key as post vars
        buf.write(bdry + "\r\n");
        buf.write("Content-Disposition: form-data; name=\"c\"\r\n\r\n" + (comp != 0 ? 1 : 0) + "\r\n");
        if (hkey) {
            buf.write(bdry + "\r\n");
            buf.write("Content-Disposition: form-data; name=\"k\"\r\n\r\n" + mHKey + "\r\n");
        }
        tmpFileBuffer = File.createTempFile("syncer", ".tmp",
                new File(AnkiDroidApp.getCacheStorageDirectory()));
        FileOutputStream fos = new FileOutputStream(tmpFileBuffer);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        GZIPOutputStream tgt;
        // payload as raw data or json
        if (fobj != null) {
            // header
            buf.write(bdry + "\r\n");
            buf.write(
                    "Content-Disposition: form-data; name=\"data\"; filename=\"data\"\r\nContent-Type: application/octet-stream\r\n\r\n");
            buf.close();
            bos.write(buf.toString().getBytes("UTF-8"));
            // write file into buffer, optionally compressing
            int len;
            BufferedInputStream bfobj = new BufferedInputStream(fobj);
            byte[] chunk = new byte[65536];
            if (comp != 0) {
                tgt = new GZIPOutputStream(bos);
                while ((len = bfobj.read(chunk)) >= 0) {
                    tgt.write(chunk, 0, len);
                }
                tgt.close();
                bos = new BufferedOutputStream(new FileOutputStream(tmpFileBuffer, true));
            } else {
                while ((len = bfobj.read(chunk)) >= 0) {
                    bos.write(chunk, 0, len);
                }
            }
            bos.write(("\r\n" + bdry + "--\r\n").getBytes("UTF-8"));
        } else {
            buf.close();
            bos.write(buf.toString().getBytes("UTF-8"));
        }
        bos.flush();
        bos.close();
        // connection headers
        String url = Collection.SYNC_URL;
        if (method.equals("register")) {
            url = url + "account/signup" + "?username=" + registerData.getString("u") + "&password="
                    + registerData.getString("p");
        } else if (method.startsWith("upgrade")) {
            url = url + method;
        } else {
            url = url + "sync/" + method;
        }
        HttpPost httpPost = new HttpPost(url);
        HttpEntity entity = new ProgressByteEntity(tmpFileBuffer);

        // body
        httpPost.setEntity(entity);
        httpPost.setHeader("Content-type", "multipart/form-data; boundary=" + BOUNDARY);

        // HttpParams
        HttpParams params = new BasicHttpParams();
        params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30);
        params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30));
        params.setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
        params.setParameter(CoreProtocolPNames.USER_AGENT, "AnkiDroid-" + AnkiDroidApp.getPkgVersion());
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpConnectionParams.setSoTimeout(params, Connection.CONN_TIMEOUT);

        // Registry
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", new EasySSLSocketFactory(), 443));
        ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(params, registry);
        if (cancelCallback != null) {
            cancelCallback.setConnectionManager(cm);
        }

        try {
            HttpClient httpClient = new DefaultHttpClient(cm, params);
            return httpClient.execute(httpPost);
        } catch (SSLException e) {
            Log.e(AnkiDroidApp.TAG, "SSLException while building HttpClient", e);
            return null;
        }
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        Log.e(AnkiDroidApp.TAG, "BasicHttpSyncer.sync: IOException", e);
        return null;
    } catch (JSONException e) {
        throw new RuntimeException(e);
    } finally {
        if (tmpFileBuffer != null && tmpFileBuffer.exists()) {
            tmpFileBuffer.delete();
        }
    }
}

From source file:com.hichinaschool.flashcards.libanki.sync.BasicHttpSyncer.java

public HttpResponse req(String method, InputStream fobj, int comp, boolean hkey, JSONObject registerData,
        Connection.CancelCallback cancelCallback) {
    File tmpFileBuffer = null;/*  w w  w  . j a  v a 2  s  . c o  m*/
    try {
        String bdry = "--" + BOUNDARY;
        StringWriter buf = new StringWriter();
        HashMap<String, Object> vars = new HashMap<String, Object>();
        // compression flag and session key as post vars
        vars.put("c", comp != 0 ? 1 : 0);
        if (hkey) {
            vars.put("k", mHKey);
            vars.put("s", mSKey);
        }
        for (String key : vars.keySet()) {
            buf.write(bdry + "\r\n");
            buf.write(String.format(Locale.US, "Content-Disposition: form-data; name=\"%s\"\r\n\r\n%s\r\n", key,
                    vars.get(key)));
        }
        tmpFileBuffer = File.createTempFile("syncer", ".tmp",
                new File(AnkiDroidApp.getCacheStorageDirectory()));
        FileOutputStream fos = new FileOutputStream(tmpFileBuffer);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        GZIPOutputStream tgt;
        // payload as raw data or json
        if (fobj != null) {
            // header
            buf.write(bdry + "\r\n");
            buf.write(
                    "Content-Disposition: form-data; name=\"data\"; filename=\"data\"\r\nContent-Type: application/octet-stream\r\n\r\n");
            buf.close();
            bos.write(buf.toString().getBytes("UTF-8"));
            // write file into buffer, optionally compressing
            int len;
            BufferedInputStream bfobj = new BufferedInputStream(fobj);
            byte[] chunk = new byte[65536];
            if (comp != 0) {
                tgt = new GZIPOutputStream(bos);
                while ((len = bfobj.read(chunk)) >= 0) {
                    tgt.write(chunk, 0, len);
                }
                tgt.close();
                bos = new BufferedOutputStream(new FileOutputStream(tmpFileBuffer, true));
            } else {
                while ((len = bfobj.read(chunk)) >= 0) {
                    bos.write(chunk, 0, len);
                }
            }
            bos.write(("\r\n" + bdry + "--\r\n").getBytes("UTF-8"));
        } else {
            buf.close();
            bos.write(buf.toString().getBytes("UTF-8"));
        }
        bos.flush();
        bos.close();
        // connection headers
        String url = Collection.SYNC_URL;
        if (method.equals("register")) {
            url = url + "account/signup" + "?username=" + registerData.getString("u") + "&password="
                    + registerData.getString("p");
        } else if (method.startsWith("upgrade")) {
            url = url + method;
        } else {
            url = url + "sync/" + method;
        }
        HttpPost httpPost = new HttpPost(url);
        HttpEntity entity = new ProgressByteEntity(tmpFileBuffer);

        // body
        httpPost.setEntity(entity);
        httpPost.setHeader("Content-type", "multipart/form-data; boundary=" + BOUNDARY);

        // HttpParams
        HttpParams params = new BasicHttpParams();
        params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30);
        params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30));
        params.setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
        params.setParameter(CoreProtocolPNames.USER_AGENT, "AnkiDroid-" + AnkiDroidApp.getPkgVersionName());
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpConnectionParams.setSoTimeout(params, Connection.CONN_TIMEOUT);

        // Registry
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", new EasySSLSocketFactory(), 443));
        ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(params, registry);
        if (cancelCallback != null) {
            cancelCallback.setConnectionManager(cm);
        }

        try {
            HttpClient httpClient = new DefaultHttpClient(cm, params);
            return httpClient.execute(httpPost);
        } catch (SSLException e) {
            Log.e(AnkiDroidApp.TAG, "SSLException while building HttpClient", e);
            return null;
        }
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        Log.e(AnkiDroidApp.TAG, "BasicHttpSyncer.sync: IOException", e);
        return null;
    } catch (JSONException e) {
        throw new RuntimeException(e);
    } finally {
        if (tmpFileBuffer != null && tmpFileBuffer.exists()) {
            tmpFileBuffer.delete();
        }
    }
}

From source file:edu.cornell.med.icb.goby.alignments.AlignmentWriterImpl.java

private void writeIndex() throws IOException {
    if (!indexWritten) {
        // Push the last chunkoffset:
        pushIndex(entriesChunkWriter.getCurrentChunkStartOffset(), firstTargetIndexInChunk,
                firstPositionInChunk);/*from ww  w  .j  av  a  2s  .  c om*/
        GZIPOutputStream indexOutput = null;
        try {
            indexOutput = new GZIPOutputStream(new FileOutputStream(basename + ".index"));
            final Alignments.AlignmentIndex.Builder indexBuilder = Alignments.AlignmentIndex.newBuilder();
            assert (indexOffsets.size() == indexAbsolutePositions.size()) : "index sizes must be consistent.";
            indexBuilder.addAllOffsets(indexOffsets);
            indexBuilder.addAllAbsolutePositions(indexAbsolutePositions);
            indexBuilder.build().writeTo(indexOutput);
        } finally {
            if (indexOutput != null)
                indexOutput.close();
            indexWritten = true;
        }
    }
}