List of usage examples for java.util.zip GZIPOutputStream flush
public void flush() throws IOException
From source file:org.appcelerator.transport.AjaxServiceTransportServlet.java
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ////from w ww . j a v a 2s . com // make sure we check the integrity of the request before we continue // if (!validate(req, resp)) { LOG.warn("security validation failed for request=" + req + " from " + req.getRemoteAddr()); return; } String type = req.getContentType(); int idx = type.indexOf(';'); if (idx > 0) { type = type.substring(0, idx); } try { // decode the incoming request ArrayList<Message> requests = new ArrayList<Message>(1); ArrayList<Message> responses = new ArrayList<Message>(1); ServiceMarshaller.getMarshaller(type).decode(req.getInputStream(), requests); if (requests.isEmpty()) { // no incoming messages, just return accepted header resp.setHeader("Content-Length", "0"); resp.setContentType("text/plain;charset=UTF-8"); resp.setStatus(HttpServletResponse.SC_ACCEPTED); return; } HttpSession session = req.getSession(); InetAddress address = InetAddress.getByName(req.getRemoteAddr()); //String instanceid = req.getParameter("instanceid"); for (Message request : requests) { request.setUser(req.getUserPrincipal()); request.setSession(session); request.setAddress(address); request.setServletRequest(req); //FIXME => refactor this out if (request.getType().equals(MessageType.APPCELERATOR_STATUS_REPORT)) { IMessageDataObject data = (IMessageDataObject) request.getData(); data.put("remoteaddr", req.getRemoteAddr()); data.put("remotehost", req.getRemoteHost()); data.put("remoteuser", req.getRemoteUser()); } ServiceRegistry.dispatch(request, responses); } if (responses.isEmpty()) { // no response messages, just return accepted header resp.setHeader("Content-Length", "0"); resp.setContentType("text/plain;charset=UTF-8"); resp.setStatus(HttpServletResponse.SC_ACCEPTED); return; } // setup the response resp.setStatus(HttpServletResponse.SC_OK); resp.setHeader("Connection", "Keep-Alive"); resp.setHeader("Pragma", "no-cache"); resp.setHeader("Cache-control", "no-cache, no-store, private, must-revalidate"); resp.setHeader("Expires", "Mon, 26 Jul 1997 05:00:00 GMT"); // encode the responses ServletOutputStream output = resp.getOutputStream(); ByteArrayOutputStream bout = new ByteArrayOutputStream(1000); String responseType = ServiceMarshaller.getMarshaller(type).encode(responses, req.getSession().getId(), bout); byte buf[] = bout.toByteArray(); ByteArrayInputStream bin = new ByteArrayInputStream(buf); resp.setContentType(responseType); // do gzip encoding if browser supports it and if length > 1000 bytes String ae = req.getHeader("accept-encoding"); if (ae != null && ae.indexOf("gzip") != -1 && buf.length > 1000) { resp.setHeader("Content-Encoding", "gzip"); //a Vary: Accept-Encoding HTTP response header to alert proxies that a cached response should be sent only to //clients that send the appropriate Accept-Encoding request header. This prevents compressed content from being sent //to a client that will not understand it. resp.addHeader("Vary", "Accept-Encoding"); GZIPOutputStream gzip = new GZIPOutputStream(output, buf.length); Util.copy(bin, gzip); gzip.flush(); gzip.finish(); } else { resp.setContentLength(buf.length); Util.copy(bin, output); } output.flush(); } catch (Throwable e) { LOG.error("Error handling incoming POST request", e); resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } }
From source file:org.hyperic.hq.bizapp.server.session.UpdateBossImpl.java
/** * Meant to be called internally by the fetching thread * // ww w. j a v a 2 s.c o m * */ public void fetchReport() { final boolean debug = log.isDebugEnabled(); final StopWatch watch = new StopWatch(); UpdateStatus status = getOrCreateStatus(); Properties req; byte[] reqBytes; if (status.getMode().equals(UpdateStatusMode.NONE)) { return; } req = getRequestInfo(status); try { ByteArrayOutputStream bOs = new ByteArrayOutputStream(); GZIPOutputStream gOs = new GZIPOutputStream(bOs); req.store(gOs, ""); gOs.flush(); gOs.close(); bOs.flush(); bOs.close(); reqBytes = bOs.toByteArray(); } catch (IOException e) { log.warn("Error creating report request", e); return; } if (debug) { log.debug("Generated report. Size=" + reqBytes.length + " report:\n" + req); } try { HttpConfig config = new HttpConfig(HTTP_TIMEOUT_MILLIS, HTTP_TIMEOUT_MILLIS, null, -1); HQHttpClient client = new HQHttpClient(keystoreConf, config, acceptUnverifiedCertificates); HttpPost post = new HttpPost(updateNotifyUrl); post.addHeader("x-hq-guid", req.getProperty("hq.guid")); ByteArrayInputStream bIs = new ByteArrayInputStream(reqBytes); HttpEntity entity = new InputStreamEntity(bIs, reqBytes.length); post.setEntity(entity); if (debug) watch.markTimeBegin("post"); HttpResponse response = client.execute(post); if (debug) watch.markTimeEnd("post"); if (response != null && response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { processReport(response.getStatusLine().getStatusCode(), EntityUtils.toString(response.getEntity(), "UTF-8")); } else { if (debug) { log.debug("fetchReport: " + watch + ", currentReport {" + status.getReport() + "}, latestReport {url=" + updateNotifyUrl + ", statusCode=" + response.getStatusLine().getStatusCode() + ", response=" + EntityUtils.toString(response.getEntity(), "UTF-8") + "}"); } } } catch (ClientProtocolException e) { log.error(e); } catch (IOException e) { log.error(e); } }
From source file:org.geoserver.wcs.responses.AscCoverageResponseDelegate.java
public void encode(GridCoverage2D sourceCoverage, String outputFormat, Map<String, String> econdingParameters, OutputStream output) throws ServiceException, IOException { if (sourceCoverage == null) { throw new IllegalStateException(new StringBuffer("It seems prepare() has not been called") .append(" or has not succeed").toString()); }//from ww w . j av a2s . c o m GZIPOutputStream gzipOut = null; if (isOutputCompressed(outputFormat)) { gzipOut = new GZIPOutputStream(output); output = gzipOut; } ArcGridWriter writer = null; try { writer = new ArcGridWriter(output); writer.write(sourceCoverage, null); if (gzipOut != null) { gzipOut.finish(); gzipOut.flush(); } } finally { try { if (writer != null) writer.dispose(); } catch (Throwable e) { // eating exception } if (gzipOut != null) IOUtils.closeQuietly(gzipOut); sourceCoverage.dispose(true); } }
From source file:org.jabsorb.JSONRPCServlet.java
/** * Gzip something./*from ww w . ja va 2 s . c om*/ * * @param in original content * @return size gzipped content */ private byte[] gzip(byte[] in) { if (in != null && in.length > 0) { long tstart = System.currentTimeMillis(); ByteArrayOutputStream bout = new ByteArrayOutputStream(); try { GZIPOutputStream gout = new GZIPOutputStream(bout); gout.write(in); gout.flush(); gout.close(); if (log.isDebugEnabled()) { log.debug("gzipping took " + (System.currentTimeMillis() - tstart) + " msec"); } return bout.toByteArray(); } catch (IOException io) { log.error("io exception gzipping byte array", io); } } return new byte[0]; }
From source file:org.vfny.geoserver.wcs.responses.coverage.AscCoverageResponseDelegate.java
public void encode(OutputStream output) throws ServiceException, IOException { if (sourceCoverage == null) { throw new IllegalStateException(new StringBuffer("It seems prepare() has not been called") .append(" or has not succeed").toString()); }/* w ww .j av a 2 s .co m*/ GZIPOutputStream gzipOut = null; if (compressOutput) { gzipOut = new GZIPOutputStream(output); output = gzipOut; } ArcGridWriter writer = null; try { writer = new ArcGridWriter(output); writer.write(sourceCoverage, null); if (gzipOut != null) { gzipOut.finish(); gzipOut.flush(); } } finally { try { if (writer != null) writer.dispose(); } catch (Throwable e) { // eating exception } if (gzipOut != null) IOUtils.closeQuietly(gzipOut); this.sourceCoverage.dispose(false); this.sourceCoverage = null; } }
From source file:org.jabsorb.ng.JSONRPCServlet.java
/** * Gzip something.// w ww.j a v a 2 s . com * * @param in * original content * @return size gzipped content */ private byte[] gzip(final byte[] in) { if (in != null && in.length > 0) { long tstart = System.currentTimeMillis(); ByteArrayOutputStream bout = new ByteArrayOutputStream(); try { GZIPOutputStream gout = new GZIPOutputStream(bout); gout.write(in); gout.flush(); gout.close(); if (log.isDebugEnabled()) { log.debug("gzip", "gzipping took " + (System.currentTimeMillis() - tstart) + " msec"); } return bout.toByteArray(); } catch (IOException io) { log.error("gzip", "io exception gzipping byte array", io); } } return new byte[0]; }
From source file:com.taobao.android.builder.tasks.app.GenerateAtlasSourceTask.java
private String compressBundleInfo(String bundleInfo) { ByteArrayOutputStream cc = new ByteArrayOutputStream(); GZIPOutputStream gzip = null; try {// w w w . ja v a 2 s.com gzip = new GZIPOutputStream(cc); gzip.write(bundleInfo.getBytes("UTF-8")); gzip.flush(); IOUtils.closeQuietly(gzip); byte[] result = cc.toByteArray(); return org.apache.commons.codec.binary.Base64.encodeBase64String(result); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:com.evilco.license.server.encoder.CompressedLicenseEncoder.java
/** * {@inheritDoc}//from w w w . j a va2 s . c o m */ @Override public String encode(@Nonnull ILicense license) throws LicenseEncoderException { // define streams ByteArrayOutputStream outputStream = null; GZIPOutputStream gzipOutputStream = null; DataOutputStream dataOutputStream = null; // encode data try { // create stream instances outputStream = new ByteArrayOutputStream(); gzipOutputStream = new GZIPOutputStream(outputStream); dataOutputStream = new DataOutputStream(gzipOutputStream); // encode data this.childEncoder.encode(license, dataOutputStream); // flush buffers gzipOutputStream.flush(); outputStream.flush(); } catch (IOException ex) { throw new LicenseEncoderException(ex); } finally { IOUtils.closeQuietly(dataOutputStream); IOUtils.closeQuietly(gzipOutputStream); IOUtils.closeQuietly(outputStream); } // Encode Base64 String licenseText = BaseEncoding.base64().encode(outputStream.toByteArray()); // split text into chunks Joiner joiner = Joiner.on("\n").skipNulls(); return joiner.join(Splitter.fixedLength(LICENSE_CHUNK_WIDTH).split(licenseText)); }
From source file:org.kairosdb.datastore.remote.RemoteDatastore.java
/** Compresses the given file and removes the uncompressed file @param file/*from www.java 2s .com*/ @return Size of the zip file */ private long zipFile(String file) throws IOException { String zipFile = file + ".gz"; FileInputStream is = new FileInputStream(file); GZIPOutputStream gout = new GZIPOutputStream(new FileOutputStream(zipFile)); byte[] buffer = new byte[1024]; int readSize = 0; while ((readSize = is.read(buffer)) != -1) gout.write(buffer, 0, readSize); is.close(); gout.flush(); gout.close(); //delete uncompressed file new File(file).delete(); return (new File(zipFile).length()); }
From source file:org.codice.alliance.nsili.endpoint.requests.OrderRequestImpl.java
private void getGzip(GZIPOutputStream zipOut, InputStream data) throws IOException { IOUtils.copy(data, zipOut);/*w ww. j a va 2 s .c o m*/ zipOut.flush(); }