... out.println("Content-Encoding: deflate"); // NOTICE deflate encoding out.println("Content-Type: text/html"); out.println("Connection: close"); out.println(); String x = "jsonp1330xxxxx462022184([["; DeflaterInputStream dis = new DeflaterInputStream(out); dis.write(x.getBytes("utf-8")); // JSON is UTF-8 dis.close(); server.close(); // this a bad idea, the client may not have read the data yet
@Override public void close() throws IOException { if (LOG.isDebugEnabled()) { final StringBuilder sb = new StringBuilder(); long bytesWritten = this.def.getBytesWritten(); long bytesRead = this.def.getBytesRead(); sb.append("POST body length compressed / uncompressed / compression ratio: "); sb.append(bytesWritten); sb.append(" / "); sb.append(bytesRead); sb.append(" / "); sb.append(String.format( "%.2f", bytesRead / (double)bytesWritten)); LOG.debug(sb.toString()); } super.close(); } };
private static byte[] zipText(byte[] licenseText) throws IOException { int len; byte[] buff = new byte[64]; ByteArrayInputStream in = new ByteArrayInputStream(licenseText); DeflaterInputStream deflater = new DeflaterInputStream(in, new Deflater()); ByteArrayOutputStream out = new ByteArrayOutputStream(); try { while ((len = deflater.read(buff)) > 0) { out.write(buff, 0, len); } return out.toByteArray(); } finally { out.close(); deflater.close(); in.close(); } }
private static byte[] deflate(byte[] orig) throws ItlException { try { DeflaterInputStream isDeflater = new DeflaterInputStream(new ByteArrayInputStream(orig), new Deflater()); ByteArrayOutputStream osCompressed = new ByteArrayOutputStream(orig.length); byte[] deflated = new byte[orig.length]; int iCompressed; while(true) { iCompressed = isDeflater.read(deflated, 0, orig.length); if (iCompressed == -1) break; osCompressed.write(deflated, 0, iCompressed); } deflated = osCompressed.toByteArray(); osCompressed.close(); isDeflater.close(); return deflated; } catch (IOException ioe) { throw new ItlException("Error when zipping the file contents", ioe); } }