List of usage examples for java.util.zip Deflater BEST_COMPRESSION
int BEST_COMPRESSION
To view the source code for java.util.zip Deflater BEST_COMPRESSION.
Click Source Link
From source file:com.uber.hoodie.common.TestRawTripPayload.java
private byte[] compressData(String jsonData) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DeflaterOutputStream dos = new DeflaterOutputStream(baos, new Deflater(Deflater.BEST_COMPRESSION), true); try {//www .ja v a2 s . co m dos.write(jsonData.getBytes()); } finally { dos.flush(); dos.close(); } return baos.toByteArray(); }
From source file:org.syncope.core.scheduling.ReportJob.java
@Override public void execute(final JobExecutionContext context) throws JobExecutionException { Report report = reportDAO.find(reportId); if (report == null) { throw new JobExecutionException("Report " + reportId + " not found"); }// w ww.j ava 2 s . c o m // 1. create execution ReportExec execution = new ReportExec(); execution.setStatus(ReportExecStatus.STARTED); execution.setStartDate(new Date()); execution.setReport(report); execution = reportExecDAO.save(execution); // 2. define a SAX handler for generating result as XML TransformerHandler handler; ByteArrayOutputStream baos = new ByteArrayOutputStream(); ZipOutputStream zos = new ZipOutputStream(baos); zos.setLevel(Deflater.BEST_COMPRESSION); try { SAXTransformerFactory transformerFactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); handler = transformerFactory.newTransformerHandler(); Transformer serializer = handler.getTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); // a single ZipEntry in the ZipOutputStream zos.putNextEntry(new ZipEntry(report.getName())); // streaming SAX handler in a compressed byte array stream handler.setResult(new StreamResult(zos)); } catch (Exception e) { throw new JobExecutionException("While configuring for SAX generation", e, true); } execution.setStatus(ReportExecStatus.RUNNING); execution = reportExecDAO.save(execution); ConfigurableListableBeanFactory beanFactory = ApplicationContextManager.getApplicationContext() .getBeanFactory(); // 3. actual report execution StringBuilder reportExecutionMessage = new StringBuilder(); StringWriter exceptionWriter = new StringWriter(); try { // report header handler.startDocument(); AttributesImpl atts = new AttributesImpl(); atts.addAttribute("", "", ATTR_NAME, XSD_STRING, report.getName()); handler.startElement("", "", ELEMENT_REPORT, atts); // iterate over reportlet instances defined for this report for (ReportletConf reportletConf : report.getReportletConfs()) { Class reportletClass = null; try { reportletClass = Class.forName(reportletConf.getReportletClassName()); } catch (ClassNotFoundException e) { LOG.error("Reportlet class not found: {}", reportletConf.getReportletClassName(), e); } if (reportletClass != null) { Reportlet autowired = (Reportlet) beanFactory.createBean(reportletClass, AbstractBeanDefinition.AUTOWIRE_BY_TYPE, false); autowired.setConf(reportletConf); // invoke reportlet try { autowired.extract(handler); } catch (Exception e) { execution.setStatus(ReportExecStatus.FAILURE); Throwable t = e instanceof ReportException ? e.getCause() : e; exceptionWriter.write(t.getMessage() + "\n\n"); t.printStackTrace(new PrintWriter(exceptionWriter)); reportExecutionMessage.append(exceptionWriter.toString()).append("\n==================\n"); } } } // report footer handler.endElement("", "", ELEMENT_REPORT); handler.endDocument(); if (!ReportExecStatus.FAILURE.name().equals(execution.getStatus())) { execution.setStatus(ReportExecStatus.SUCCESS); } } catch (Exception e) { execution.setStatus(ReportExecStatus.FAILURE); exceptionWriter.write(e.getMessage() + "\n\n"); e.printStackTrace(new PrintWriter(exceptionWriter)); reportExecutionMessage.append(exceptionWriter.toString()); throw new JobExecutionException(e, true); } finally { try { zos.closeEntry(); zos.close(); baos.close(); } catch (IOException e) { LOG.error("While closing StreamResult's backend", e); } execution.setExecResult(baos.toByteArray()); execution.setMessage(reportExecutionMessage.toString()); execution.setEndDate(new Date()); reportExecDAO.save(execution); } }
From source file:org.apache.syncope.core.logic.report.ReportJobDelegate.java
@Transactional public void execute(final String reportKey) throws JobExecutionException { Report report = reportDAO.find(reportKey); if (report == null) { throw new JobExecutionException("Report " + reportKey + " not found"); }//from w ww . j a va2s . c o m if (!report.isActive()) { LOG.info("Report {} not active, aborting...", reportKey); return; } // 1. create execution ReportExec execution = entityFactory.newEntity(ReportExec.class); execution.setStatus(ReportExecStatus.STARTED); execution.setStart(new Date()); execution.setReport(report); execution = reportExecDAO.save(execution); report.add(execution); report = reportDAO.save(report); // 2. define a SAX handler for generating result as XML TransformerHandler handler; ByteArrayOutputStream baos = new ByteArrayOutputStream(); ZipOutputStream zos = new ZipOutputStream(baos); zos.setLevel(Deflater.BEST_COMPRESSION); try { SAXTransformerFactory tFactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); tFactory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true); handler = tFactory.newTransformerHandler(); Transformer serializer = handler.getTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, SyncopeConstants.DEFAULT_ENCODING); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); // a single ZipEntry in the ZipOutputStream zos.putNextEntry(new ZipEntry(report.getName())); // streaming SAX handler in a compressed byte array stream handler.setResult(new StreamResult(zos)); } catch (Exception e) { throw new JobExecutionException("While configuring for SAX generation", e, true); } execution.setStatus(ReportExecStatus.RUNNING); execution = reportExecDAO.save(execution); // 3. actual report execution StringBuilder reportExecutionMessage = new StringBuilder(); try { // report header handler.startDocument(); AttributesImpl atts = new AttributesImpl(); atts.addAttribute("", "", ReportXMLConst.ATTR_NAME, ReportXMLConst.XSD_STRING, report.getName()); handler.startElement("", "", ReportXMLConst.ELEMENT_REPORT, atts); // iterate over reportlet instances defined for this report for (ReportletConf reportletConf : report.getReportletConfs()) { Class<? extends Reportlet> reportletClass = implementationLookup .getReportletClass(reportletConf.getClass()); if (reportletClass == null) { LOG.warn("Could not find matching reportlet for {}", reportletConf.getClass()); } else { // fetch (or create) reportlet Reportlet reportlet; if (ApplicationContextProvider.getBeanFactory().containsSingleton(reportletClass.getName())) { reportlet = (Reportlet) ApplicationContextProvider.getBeanFactory() .getSingleton(reportletClass.getName()); } else { reportlet = (Reportlet) ApplicationContextProvider.getBeanFactory() .createBean(reportletClass, AbstractBeanDefinition.AUTOWIRE_BY_TYPE, false); ApplicationContextProvider.getBeanFactory().registerSingleton(reportletClass.getName(), reportlet); } // invoke reportlet try { reportlet.extract(reportletConf, handler); } catch (Throwable t) { LOG.error("While executing reportlet {} for report {}", reportlet, reportKey, t); execution.setStatus(ReportExecStatus.FAILURE); Throwable effective = t instanceof ReportException ? t.getCause() : t; reportExecutionMessage.append(ExceptionUtils2.getFullStackTrace(effective)) .append("\n==================\n"); } } } // report footer handler.endElement("", "", ReportXMLConst.ELEMENT_REPORT); handler.endDocument(); if (!ReportExecStatus.FAILURE.name().equals(execution.getStatus())) { execution.setStatus(ReportExecStatus.SUCCESS); } } catch (Exception e) { execution.setStatus(ReportExecStatus.FAILURE); reportExecutionMessage.append(ExceptionUtils2.getFullStackTrace(e)); throw new JobExecutionException(e, true); } finally { try { zos.closeEntry(); IOUtils.closeQuietly(zos); IOUtils.closeQuietly(baos); } catch (IOException e) { LOG.error("While closing StreamResult's backend", e); } execution.setExecResult(baos.toByteArray()); execution.setMessage(reportExecutionMessage.toString()); execution.setEnd(new Date()); reportExecDAO.save(execution); } }
From source file:org.apache.syncope.core.report.ReportJob.java
@SuppressWarnings("rawtypes") @Override// ww w . j a v a2 s .co m public void execute(final JobExecutionContext context) throws JobExecutionException { Report report = reportDAO.find(reportId); if (report == null) { throw new JobExecutionException("Report " + reportId + " not found"); } // 1. create execution ReportExec execution = new ReportExec(); execution.setStatus(ReportExecStatus.STARTED); execution.setStartDate(new Date()); execution.setReport(report); execution = reportExecDAO.save(execution); report.addExec(execution); report = reportDAO.save(report); // 2. define a SAX handler for generating result as XML TransformerHandler handler; ByteArrayOutputStream baos = new ByteArrayOutputStream(); ZipOutputStream zos = new ZipOutputStream(baos); zos.setLevel(Deflater.BEST_COMPRESSION); try { SAXTransformerFactory tFactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); handler = tFactory.newTransformerHandler(); Transformer serializer = handler.getTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, SyncopeConstants.DEFAULT_ENCODING); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); // a single ZipEntry in the ZipOutputStream zos.putNextEntry(new ZipEntry(report.getName())); // streaming SAX handler in a compressed byte array stream handler.setResult(new StreamResult(zos)); } catch (Exception e) { throw new JobExecutionException("While configuring for SAX generation", e, true); } execution.setStatus(ReportExecStatus.RUNNING); execution = reportExecDAO.save(execution); // 3. actual report execution StringBuilder reportExecutionMessage = new StringBuilder(); StringWriter exceptionWriter = new StringWriter(); try { // report header handler.startDocument(); AttributesImpl atts = new AttributesImpl(); atts.addAttribute("", "", ReportXMLConst.ATTR_NAME, ReportXMLConst.XSD_STRING, report.getName()); handler.startElement("", "", ReportXMLConst.ELEMENT_REPORT, atts); // iterate over reportlet instances defined for this report for (ReportletConf reportletConf : report.getReportletConfs()) { Class<Reportlet> reportletClass = dataBinder .findReportletClassHavingConfClass(reportletConf.getClass()); if (reportletClass != null) { Reportlet autowired = (Reportlet) ApplicationContextProvider.getBeanFactory() .createBean(reportletClass, AbstractBeanDefinition.AUTOWIRE_BY_TYPE, false); autowired.setConf(reportletConf); // invoke reportlet try { autowired.extract(handler); } catch (Exception e) { execution.setStatus(ReportExecStatus.FAILURE); Throwable t = e instanceof ReportException ? e.getCause() : e; reportExecutionMessage.append(ExceptionUtil.getFullStackTrace(t)) .append("\n==================\n"); } } } // report footer handler.endElement("", "", ReportXMLConst.ELEMENT_REPORT); handler.endDocument(); if (!ReportExecStatus.FAILURE.name().equals(execution.getStatus())) { execution.setStatus(ReportExecStatus.SUCCESS); } } catch (Exception e) { execution.setStatus(ReportExecStatus.FAILURE); reportExecutionMessage.append(ExceptionUtil.getFullStackTrace(e)); throw new JobExecutionException(e, true); } finally { try { zos.closeEntry(); IOUtils.closeQuietly(zos); IOUtils.closeQuietly(baos); } catch (IOException e) { LOG.error("While closing StreamResult's backend", e); } execution.setExecResult(baos.toByteArray()); execution.setMessage(reportExecutionMessage.toString()); execution.setEndDate(new Date()); reportExecDAO.save(execution); } }
From source file:org.getspout.spoutapi.packet.PacketCustomBlockChunkOverride.java
@Override public void compress() { if (!compressed) { if (data != null && hasData) { Deflater deflater = new Deflater(); deflater.setInput(data);/* www . j a v a 2 s .com*/ deflater.setLevel(Deflater.BEST_COMPRESSION); deflater.finish(); ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length); byte[] buffer = new byte[1024]; while (!deflater.finished()) { int bytesCompressed = deflater.deflate(buffer); bos.write(buffer, 0, bytesCompressed); } try { bos.close(); } catch (IOException e) { e.printStackTrace(); } data = bos.toByteArray(); } compressed = true; } }
From source file:org.apache.syncope.core.logic.report.ReportJob.java
@SuppressWarnings("rawtypes") @Override//from w w w. ja v a2s .c o m public void execute(final JobExecutionContext context) throws JobExecutionException { Report report = reportDAO.find(reportKey); if (report == null) { throw new JobExecutionException("Report " + reportKey + " not found"); } // 1. create execution ReportExec execution = entityFactory.newEntity(ReportExec.class); execution.setStatus(ReportExecStatus.STARTED); execution.setStartDate(new Date()); execution.setReport(report); execution = reportExecDAO.save(execution); report.addExec(execution); report = reportDAO.save(report); // 2. define a SAX handler for generating result as XML TransformerHandler handler; ByteArrayOutputStream baos = new ByteArrayOutputStream(); ZipOutputStream zos = new ZipOutputStream(baos); zos.setLevel(Deflater.BEST_COMPRESSION); try { SAXTransformerFactory tFactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); handler = tFactory.newTransformerHandler(); Transformer serializer = handler.getTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, SyncopeConstants.DEFAULT_ENCODING); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); // a single ZipEntry in the ZipOutputStream zos.putNextEntry(new ZipEntry(report.getName())); // streaming SAX handler in a compressed byte array stream handler.setResult(new StreamResult(zos)); } catch (Exception e) { throw new JobExecutionException("While configuring for SAX generation", e, true); } execution.setStatus(ReportExecStatus.RUNNING); execution = reportExecDAO.save(execution); // 3. actual report execution StringBuilder reportExecutionMessage = new StringBuilder(); try { // report header handler.startDocument(); AttributesImpl atts = new AttributesImpl(); atts.addAttribute("", "", ReportXMLConst.ATTR_NAME, ReportXMLConst.XSD_STRING, report.getName()); handler.startElement("", "", ReportXMLConst.ELEMENT_REPORT, atts); // iterate over reportlet instances defined for this report for (ReportletConf reportletConf : report.getReportletConfs()) { Class<Reportlet> reportletClass = dataBinder .findReportletClassHavingConfClass(reportletConf.getClass()); if (reportletClass != null) { Reportlet<ReportletConf> autowired = (Reportlet<ReportletConf>) ApplicationContextProvider .getBeanFactory() .createBean(reportletClass, AbstractBeanDefinition.AUTOWIRE_BY_TYPE, false); autowired.setConf(reportletConf); // invoke reportlet try { autowired.extract(handler); } catch (Exception e) { execution.setStatus(ReportExecStatus.FAILURE); Throwable t = e instanceof ReportException ? e.getCause() : e; reportExecutionMessage.append(ExceptionUtils2.getFullStackTrace(t)) .append("\n==================\n"); } } } // report footer handler.endElement("", "", ReportXMLConst.ELEMENT_REPORT); handler.endDocument(); if (!ReportExecStatus.FAILURE.name().equals(execution.getStatus())) { execution.setStatus(ReportExecStatus.SUCCESS); } } catch (Exception e) { execution.setStatus(ReportExecStatus.FAILURE); reportExecutionMessage.append(ExceptionUtils2.getFullStackTrace(e)); throw new JobExecutionException(e, true); } finally { try { zos.closeEntry(); IOUtils.closeQuietly(zos); IOUtils.closeQuietly(baos); } catch (IOException e) { LOG.error("While closing StreamResult's backend", e); } execution.setExecResult(baos.toByteArray()); execution.setMessage(reportExecutionMessage.toString()); execution.setEndDate(new Date()); reportExecDAO.save(execution); } }
From source file:org.getspout.spoutapi.packet.PacketAddonData.java
@Override public void compress() { if (!compressed) { if (data != null) { Deflater deflater = new Deflater(); deflater.setInput(data);//from w ww . ja va 2 s. c om deflater.setLevel(Deflater.BEST_COMPRESSION); deflater.finish(); ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length); byte[] buffer = new byte[1024]; while (!deflater.finished()) { int bytesCompressed = deflater.deflate(buffer); bos.write(buffer, 0, bytesCompressed); } try { bos.close(); } catch (IOException e) { e.printStackTrace(); } data = bos.toByteArray(); } compressed = true; } }
From source file:com.alibaba.citrus.service.requestcontext.session.valueencoder.AbstractSessionValueEncoder.java
private byte[] compress(byte[] data) throws SessionValueEncoderException { if (!doCompress()) { return data; }//from w w w . jav a2s.c om ByteArrayOutputStream baos = new ByteArrayOutputStream(); Deflater def = new Deflater(Deflater.BEST_COMPRESSION, false); DeflaterOutputStream dos = new DeflaterOutputStream(baos, def); try { dos.write(data); } catch (Exception e) { throw new SessionValueEncoderException(e); } finally { try { dos.close(); } catch (IOException e) { } def.end(); } return baos.toByteArray(); }
From source file:org.getspout.spout.packet.PacketAddonData.java
public void compress() { if (!compressed) { if (data != null) { Deflater deflater = new Deflater(); deflater.setInput(data);//w w w .ja va 2 s. com deflater.setLevel(Deflater.BEST_COMPRESSION); deflater.finish(); ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length); byte[] buffer = new byte[1024]; while (!deflater.finished()) { int bytesCompressed = deflater.deflate(buffer); bos.write(buffer, 0, bytesCompressed); } try { bos.close(); } catch (IOException e) { e.printStackTrace(); } data = bos.toByteArray(); } compressed = true; } }
From source file:com.asual.lesscss.ResourcePackage.java
private static byte[] deflate(byte[] input) throws IOException { Deflater deflater = new Deflater(); deflater.setLevel(Deflater.BEST_COMPRESSION); deflater.setInput(input);//from ww w . j a v a 2 s . co m deflater.finish(); ByteArrayOutputStream baos = new ByteArrayOutputStream(input.length); byte[] buf = new byte[1024]; while (!deflater.finished()) { int count = deflater.deflate(buf); baos.write(buf, 0, count); } baos.close(); return baos.toByteArray(); }