Example usage for java.util.zip GZIPOutputStream write

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

Introduction

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

Prototype

public void write(int b) throws IOException 

Source Link

Document

Writes a byte to the compressed output stream.

Usage

From source file:com.unilever.audit.services2.Sync_Down.java

/**
 * Retrieves representation of an instance of
 * com.unilever.audit.services2.AuditResource
 *
 * @param id/*from  ww  w .  j  a va2  s  . co  m*/
 * @param dataType
 * @return an instance of java.lang.String
 */
@GET
@Path("getSyncObject/{id}/{dataType}/{compress}")
@Produces("application/json")
public byte[] getSyncObject(@PathParam("id") int id, @PathParam("dataType") String dataType,
        @PathParam("compress") int compress) {

    GZIPOutputStream gzip = null;
    count++;
    ByteArrayOutputStream out = null;
    SyncDownObjects syncDownObjects = getObject(dataType, id);

    try {
        out = new ByteArrayOutputStream();
        gzip = new GZIPOutputStream(out);

        ObjectMapper mapper = new ObjectMapper();
        AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
        AnnotationIntrospector introspector1 = new JacksonAnnotationIntrospector();
        mapper.setAnnotationIntrospectors(introspector, introspector1);
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

        //String jsonString = mapper.writeValueAsString(syncDownObjects);
        //JSONObject jsonobject = (JSONObject) new JSONParser().parse(jsonString);
        //gzip.write(jsonobject.toString().getBytes("8859_1"));
        //gzip.write(jsonobject.toString().getBytes("UTF-8"));
        gzip.write(mapper.writeValueAsBytes(syncDownObjects));
        gzip.close();

    } catch (IOException ex) {
        ex.printStackTrace();
    } //catch (ParseException ex) {
      // ex.printStackTrace();
      // }
    System.out.println("======================= count : " + count);
    return out.toByteArray();
}

From source file:org.apache.click.util.ClickUtils.java

/**
 * Perform an auto post redirect to the specified target using the given
 * response. If the params Map is defined then the form will post these
 * values as name value pairs. If the compress value is true, this method
 * will attempt to gzip compress the response content if requesting
 * browser accepts "gzip" encoding.//from   w w  w . ja v a2 s  .  c  om
 * <p/>
 * Once this method has returned you should not attempt to write to the
 * servlet response.
 *
 * @param request the servlet request
 * @param response the servlet response
 * @param target the target URL to send the auto post redirect to
 * @param params the map of parameter values to post
 * @param compress the flag to specify whether to attempt gzip compression
 *         of the response content
 */
public static void autoPostRedirect(HttpServletRequest request, HttpServletResponse response, String target,
        Map<?, ?> params, boolean compress) {

    Validate.notNull(request, "Null response parameter");
    Validate.notNull(response, "Null response parameter");
    Validate.notNull(target, "Null target parameter");

    HtmlStringBuffer buffer = new HtmlStringBuffer(1024);
    buffer.append("<html><body onload=\"document.forms[0].submit();\">");
    buffer.append("<form name=\"form\" method=\"post\" style=\"{display:none;}\" action=\"");
    buffer.append(target);
    buffer.append("\">");
    for (Map.Entry<?, ?> entry : params.entrySet()) {
        buffer.elementStart("textarea");
        buffer.appendAttribute("name", entry.getKey());
        buffer.elementEnd();
        buffer.append(entry.getValue());
        buffer.elementEnd("textarea");
    }
    buffer.append("</form></body></html>");

    // Determine whether browser will accept gzip compression
    if (compress) {
        compress = false;
        Enumeration<?> e = request.getHeaders("Accept-Encoding");

        while (e.hasMoreElements()) {
            String name = (String) e.nextElement();
            if (name.indexOf("gzip") != -1) {
                compress = true;
                break;
            }
        }
    }

    OutputStream os = null;
    GZIPOutputStream gos = null;
    try {
        response.setContentType("text/html");

        if (compress) {
            response.setHeader("Content-Encoding", "gzip");

            os = response.getOutputStream();
            gos = new GZIPOutputStream(os);
            gos.write(buffer.toString().getBytes());

        } else {
            response.setContentLength(buffer.length());

            os = response.getOutputStream();
            os.write(buffer.toString().getBytes());
        }

    } catch (IOException ex) {
        ClickUtils.getLogService().error(ex.getMessage(), ex);

    } finally {
        ClickUtils.close(gos);
        ClickUtils.close(os);
    }
}

From source file:hudson.plugins.project_inheritance.projects.InheritanceProject.java

/**
 * This method dumps the version store as serialized,
 * GZIP compressed, Base64 encoded XML.//  w ww .  ja  v  a  2s. c  om
 * @return
 */
public String doGetVersionsAsCompressedXML() {
    if (this.versionStore == null) {
        return "";
    }
    String xml = this.versionStore.toXML();
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
        BASE64EncoderStream b64s = new BASE64EncoderStream(baos);
        GZIPOutputStream gos = new GZIPOutputStream(b64s);
        gos.write(xml.getBytes());
        gos.finish();
        gos.close();
        return baos.toString();
    } catch (IOException ex) {
        return "";
    }

}