List of usage examples for java.util.zip GZIPOutputStream write
public synchronized void write(byte[] buf, int off, int len) throws IOException
From source file:Base64.java
/** * Encodes a byte array into Base64 notation. * <p>//from w ww. j av a 2 s.c o m * Valid options:<pre> * GZIP: gzip-compresses object before encoding it. * DONT_BREAK_LINES: don't break lines at 76 characters * <i>Note: Technically, this makes your encoding non-compliant.</i> * </pre> * <p> * Example: <code>encodeBytes( myData, Base64.GZIP )</code> or * <p> * Example: <code>encodeBytes( myData, Base64.GZIP | Base64.DONT_BREAK_LINES )</code> * * * @param source The data to convert * @param off Offset in array where conversion should begin * @param len Length of data to convert * @param options Specified options * @see Base64#GZIP * @see Base64#DONT_BREAK_LINES * @return the text node * @since 2.0 */ public static String encodeBytes(byte[] source, int off, int len, int options) { // Isolate options int dontBreakLines = (options & DONT_BREAK_LINES); int gzip = (options & GZIP); // Compress? if (gzip == GZIP) { java.io.ByteArrayOutputStream baos = null; java.util.zip.GZIPOutputStream gzos = null; Base64.OutputStream b64os = null; try { // GZip -> Base64 -> ByteArray baos = new java.io.ByteArrayOutputStream(); b64os = new Base64.OutputStream(baos, ENCODE | dontBreakLines); gzos = new java.util.zip.GZIPOutputStream(b64os); gzos.write(source, off, len); gzos.close(); } // end try catch (java.io.IOException e) { e.printStackTrace(); return null; } // end catch finally { try { gzos.close(); } catch (Exception e) { } try { b64os.close(); } catch (Exception e) { } try { baos.close(); } catch (Exception e) { } } // end finally // Return value according to relevant encoding. try { return new String(baos.toByteArray(), PREFERRED_ENCODING); } // end try catch (java.io.UnsupportedEncodingException uue) { return new String(baos.toByteArray()); } // end catch } // end if: compress // Else, don't compress. Better not to use streams at all then. else { // Convert option to boolean in way that code likes it. boolean breakLines = dontBreakLines == 0; int len43 = len * 4 / 3; byte[] outBuff = new byte[(len43) // Main 4:3 + ((len % 3) > 0 ? 4 : 0) // Account for padding + (breakLines ? (len43 / MAX_LINE_LENGTH) : 0)]; // New lines int d = 0; int e = 0; int len2 = len - 2; int lineLength = 0; for (; d < len2; d += 3, e += 4) { encode3to4(source, d + off, 3, outBuff, e); lineLength += 4; if (breakLines && lineLength == MAX_LINE_LENGTH) { outBuff[e + 4] = NEW_LINE; e++; lineLength = 0; } // end if: end of line } // en dfor: each piece of array if (d < len) { encode3to4(source, d + off, len - d, outBuff, e); e += 4; } // end if: some padding needed // Return value according to relevant encoding. try { return new String(outBuff, 0, e, PREFERRED_ENCODING); } // end try catch (java.io.UnsupportedEncodingException uue) { return new String(outBuff, 0, e); } // end catch } // end else: don't compress }
From source file:agilejson.Base64.java
/** * Encodes a byte array into Base64 notation. * <p>/*from w w w.j a v a 2s. co m*/ * Valid options: * <ul> * <li>GZIP: gzip-compresses object before encoding it.</li> * <li>DONT_BREAK_LINES: don't break lines at 76 characters. <i>Note: * Technically, this makes your encoding non-compliant.</i></li> * </ul> * * <p> * Example: <code>encodeBytes( myData, Base64.GZIP )</code> or * <p> * Example: * <code>encodeBytes( myData, Base64.GZIP | Base64.DONT_BREAK_LINES )</code> * * @param source The data to convert * @param off Offset in array where conversion should begin * @param len Length of data to convert * @param options Specified options * @see Base64#GZIP * @see Base64#DONT_BREAK_LINES * @see Base64#URL_SAFE * @see Base64#ORDERED * @return encoded byte array * @since 2.0 */ public static String encodeBytes(byte[] source, int off, int len, int options) { if ((options & GZIP) == GZIP) { // Compress? // GZip -> Base64 -> ByteArray ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream gzos = null; try { gzos = new GZIPOutputStream(new Base64OutputStream(baos, ENCODE | options)); gzos.write(source, off, len); gzos.close(); gzos = null; return new String(baos.toByteArray(), PREFERRED_ENCODING); } catch (UnsupportedEncodingException uue) { return new String(baos.toByteArray()); } catch (IOException e) { LOG.error("error encoding byte array", e); return null; } finally { if (gzos != null) { try { gzos.close(); } catch (Exception e) { LOG.error("error closing GZIPOutputStream", e); } } try { baos.close(); } catch (Exception e) { LOG.error("error closing ByteArrayOutputStream", e); } } // end finally } // end Compress // Don't compress. Better not to use streams at all then. boolean breakLines = ((options & DONT_BREAK_LINES) == 0); int len43 = len * 4 / 3; byte[] outBuff = new byte[(len43) // Main 4:3 + ((len % 3) > 0 ? 4 : 0) // padding + (breakLines ? (len43 / MAX_LINE_LENGTH) : 0)]; // New lines int d = 0; int e = 0; int len2 = len - 2; int lineLength = 0; for (; d < len2; d += 3, e += 4) { encode3to4(source, d + off, 3, outBuff, e, options); lineLength += 4; if (breakLines && lineLength == MAX_LINE_LENGTH) { outBuff[e + 4] = NEW_LINE; e++; lineLength = 0; } // end if: end of line } // end for: each piece of array if (d < len) { encode3to4(source, d + off, len - d, outBuff, e, options); e += 4; } // end if: some padding needed // Return value according to relevant encoding. try { return new String(outBuff, 0, e, PREFERRED_ENCODING); } catch (UnsupportedEncodingException uue) { return new String(outBuff, 0, e); } }
From source file:org.apache.hadoop.io.compress.TestCodec.java
void GzipConcatTest(Configuration conf, Class<? extends Decompressor> decomClass) throws IOException { Random r = new Random(); long seed = r.nextLong(); r.setSeed(seed);/*from w w w . j a va 2 s. c o m*/ LOG.info(decomClass + " seed: " + seed); final int CONCAT = r.nextInt(4) + 3; final int BUFLEN = 128 * 1024; DataOutputBuffer dflbuf = new DataOutputBuffer(); DataOutputBuffer chkbuf = new DataOutputBuffer(); byte[] b = new byte[BUFLEN]; for (int i = 0; i < CONCAT; ++i) { GZIPOutputStream gzout = new GZIPOutputStream(dflbuf); r.nextBytes(b); int len = r.nextInt(BUFLEN); int off = r.nextInt(BUFLEN - len); chkbuf.write(b, off, len); gzout.write(b, off, len); gzout.close(); } final byte[] chk = Arrays.copyOf(chkbuf.getData(), chkbuf.getLength()); CompressionCodec codec = ReflectionUtils.newInstance(GzipCodec.class, conf); Decompressor decom = codec.createDecompressor(); assertNotNull(decom); assertEquals(decomClass, decom.getClass()); DataInputBuffer gzbuf = new DataInputBuffer(); gzbuf.reset(dflbuf.getData(), dflbuf.getLength()); InputStream gzin = codec.createInputStream(gzbuf, decom); dflbuf.reset(); IOUtils.copyBytes(gzin, dflbuf, 4096); final byte[] dflchk = Arrays.copyOf(dflbuf.getData(), dflbuf.getLength()); assertTrue(java.util.Arrays.equals(chk, dflchk)); }
From source file:org.xwiki.contrib.mailarchive.xwiki.internal.XWikiPersistence.java
private String treatHtml(final String htmlcontent, final HashMap<String, MailAttachment> attachmentsMap) throws IOException { String htmlcontentReplaced = ""; if (!StringUtils.isBlank(htmlcontent)) { logger.debug("Original HTML length " + htmlcontent.length()); // Replacement to avoid issue of "A circumflex" characters displayed (???) htmlcontentReplaced = htmlcontent.replaceAll("Â", " "); // Replace attachment URLs in HTML content for images to be shown for (Entry<String, MailAttachment> att : attachmentsMap.entrySet()) { // remove starting "<" and finishing ">" final String cid = att.getKey(); // If there is no cid, it means this attachment is not INLINE, so there's nothing more to do if (!StringUtils.isEmpty(cid)) { String pattern = att.getKey().substring(1, att.getKey().length() - 2); pattern = "cid:" + pattern; logger.debug("Testing for CID pattern " + Util.encodeURI(pattern, context) + " " + pattern); String replacement = att.getValue().getUrl(); logger.debug("To be replaced by " + replacement); htmlcontentReplaced = htmlcontentReplaced.replaceAll(pattern, replacement); } else { logger.warn("treatHtml: attachment is supposed not inline as cid is null or empty: " + att.getValue().getFilename()); }//from w w w .j a v a2 s. co m } logger.debug("Zipping HTML part ..."); ByteArrayOutputStream bos = new ByteArrayOutputStream(); GZIPOutputStream zos = new GZIPOutputStream(bos); byte[] bytes = htmlcontentReplaced.getBytes("UTF8"); zos.write(bytes, 0, bytes.length); zos.finish(); zos.close(); byte[] compbytes = bos.toByteArray(); htmlcontentReplaced = Utils.byte2hex(compbytes); bos.close(); if (htmlcontentReplaced.length() > ITextUtils.LONG_STRINGS_MAX_LENGTH) { logger.debug("Failed to have HTML fit in target field, truncating"); htmlcontentReplaced = textUtils.truncateForLargeString(htmlcontentReplaced); } } else { logger.debug("No HTML to treat"); } logger.debug("Html Zipped length : " + htmlcontentReplaced.length()); return htmlcontentReplaced; }
From source file:org.esigate.http.HttpClientRequestExecutorTest.java
private HttpResponse createMockGzippedResponse(String content) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream gzos = new GZIPOutputStream(baos); byte[] uncompressedBytes = content.getBytes(); gzos.write(uncompressedBytes, 0, uncompressedBytes.length); gzos.close();//from w w w. j a va 2 s .c om byte[] compressedBytes = baos.toByteArray(); ByteArrayEntity httpEntity = new ByteArrayEntity(compressedBytes); httpEntity.setContentType("text/html; charset=ISO-8859-1"); httpEntity.setContentEncoding("gzip"); StatusLine statusLine = new BasicStatusLine(new ProtocolVersion("p", 1, 2), HttpStatus.SC_OK, "OK"); BasicHttpResponse httpResponse = new BasicHttpResponse(statusLine); httpResponse.addHeader("Content-type", "text/html; charset=ISO-8859-1"); httpResponse.addHeader("Content-encoding", "gzip"); httpResponse.setEntity(httpEntity); return httpResponse; }
From source file:edu.ku.brc.specify.web.HttpLargeFileTransfer.java
/** * @param infileName//from www . ja va2 s .c om * @param outFileName * @param changeListener * @return */ public boolean compressFile(final String infileName, final String outFileName, final PropertyChangeListener propChgListener) { final File file = new File(infileName); if (file.exists()) { long fileSize = file.length(); if (fileSize > 0) { SwingWorker<Integer, Integer> backupWorker = new SwingWorker<Integer, Integer>() { protected String errorMsg = null; protected FileInputStream fis = null; protected GZIPOutputStream fos = null; /* (non-Javadoc) * @see javax.swing.SwingWorker#doInBackground() */ @Override protected Integer doInBackground() throws Exception { try { Thread.sleep(100); long totalSize = file.length(); long bytesCnt = 0; FileInputStream fis = new FileInputStream(infileName); GZIPOutputStream fos = new GZIPOutputStream(new FileOutputStream(outFileName)); byte[] bytes = new byte[BUFFER_SIZE * 10]; while (true) { int len = fis.read(bytes); if (len > 0) { fos.write(bytes, 0, len); bytesCnt += len; firePropertyChange("MEGS", 0, (int) (((double) bytesCnt / (double) totalSize) * 100.0)); } else { break; } } fis.close(); fos.close(); } catch (Exception ex) { ex.printStackTrace(); errorMsg = ex.toString(); } finally { try { if (fis != null) { fis.close(); } if (fos != null) { fos.close(); } } catch (IOException ex) { errorMsg = ex.toString(); } } firePropertyChange("MEGS", 0, 100); return null; } @Override protected void done() { super.done(); UIRegistry.getStatusBar().setProgressDone(HttpLargeFileTransfer.class.toString()); //UIRegistry.clearSimpleGlassPaneMsg(); if (StringUtils.isNotEmpty(errorMsg)) { UIRegistry.showError(errorMsg); } if (propChgListener != null) { propChgListener.propertyChange( new PropertyChangeEvent(HttpLargeFileTransfer.this, "Done", 0, 1)); } } }; final JStatusBar statusBar = UIRegistry.getStatusBar(); statusBar.setIndeterminate(HttpLargeFileTransfer.class.toString(), true); UIRegistry.writeSimpleGlassPaneMsg(getLocalizedMessage("Compressing Backup..."), 24); backupWorker.addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(final PropertyChangeEvent evt) { if ("MEGS".equals(evt.getPropertyName())) { Integer value = (Integer) evt.getNewValue(); double val = value / 10.0; statusBar .setText(UIRegistry.getLocalizedMessage("MySQLBackupService.BACKUP_MEGS", val)); } } }); backupWorker.execute(); } else { // file doesn't exist } } else { // file doesn't exist } return false; }
From source file:XmldapCertsAndKeys.java
/** * Encodes a byte array into Base64 notation. * <p>// www . j a v a2s .com * Valid options: * * <pre> * GZIP: gzip-compresses object before encoding it. * DONT_BREAK_LINES: don't break lines at 76 characters * <i>Note: Technically, this makes your encoding non-compliant.</i> * </pre> * <p> * Example: <code>encodeBytes( myData, Base64.GZIP )</code> or * <p> * Example: * <code>encodeBytes( myData, Base64.GZIP | Base64.DONT_BREAK_LINES )</code> * * * @param source * The data to convert * @param off * Offset in array where conversion should begin * @param len * Length of data to convert * @param options * Specified options * @see Base64#GZIP * @see Base64#DONT_BREAK_LINES * @return the text node * @since 2.0 */ public static String encodeBytes(byte[] source, int off, int len, int options) { // Isolate options int dontBreakLines = (options & DONT_BREAK_LINES); int gzip = (options & GZIP); // Compress? if (gzip == GZIP) { java.io.ByteArrayOutputStream baos = null; java.util.zip.GZIPOutputStream gzos = null; Base64.OutputStream b64os = null; try { // GZip -> Base64 -> ByteArray baos = new java.io.ByteArrayOutputStream(); b64os = new Base64.OutputStream(baos, ENCODE | dontBreakLines); gzos = new java.util.zip.GZIPOutputStream(b64os); gzos.write(source, off, len); gzos.close(); } // end try catch (java.io.IOException e) { e.printStackTrace(); return null; } // end catch finally { try { gzos.close(); } catch (Exception e) { } try { b64os.close(); } catch (Exception e) { } try { baos.close(); } catch (Exception e) { } } // end finally // Return value according to relevant encoding. try { return new String(baos.toByteArray(), PREFERRED_ENCODING); } // end try catch (java.io.UnsupportedEncodingException uue) { return new String(baos.toByteArray()); } // end catch } // end if: compress // Else, don't compress. Better not to use streams at all then. else { // Convert option to boolean in way that code likes it. boolean breakLines = dontBreakLines == 0; int len43 = len * 4 / 3; byte[] outBuff = new byte[(len43) // Main 4:3 + ((len % 3) > 0 ? 4 : 0) // Account for padding + (breakLines ? (len43 / MAX_LINE_LENGTH) : 0)]; // New // lines int d = 0; int e = 0; int len2 = len - 2; int lineLength = 0; for (; d < len2; d += 3, e += 4) { encode3to4(source, d + off, 3, outBuff, e); lineLength += 4; if (breakLines && lineLength == MAX_LINE_LENGTH) { outBuff[e + 4] = NEW_LINE; e++; lineLength = 0; } // end if: end of line } // en dfor: each piece of array if (d < len) { encode3to4(source, d + off, len - d, outBuff, e); e += 4; } // end if: some padding needed // Return value according to relevant encoding. try { return new String(outBuff, 0, e, PREFERRED_ENCODING); } // end try catch (java.io.UnsupportedEncodingException uue) { return new String(outBuff, 0, e); } // end catch } // end else: don't compress }
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 */// w w w .ja 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:org.esigate.DriverTest.java
public void testGzipErrorPage() throws Exception { Properties properties = new Properties(); properties.put(Parameters.REMOTE_URL_BASE.getName(), "http://localhost"); HttpResponse response = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), HttpStatus.SC_INTERNAL_SERVER_ERROR, "Internal Server Error"); response.addHeader("Content-type", "Text/html;Charset=UTF-8"); response.addHeader("Content-encoding", "gzip"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream gzos = new GZIPOutputStream(baos); byte[] uncompressedBytes = "".getBytes("UTF-8"); gzos.write(uncompressedBytes, 0, uncompressedBytes.length); gzos.close();/*ww w . j a v a 2 s .co m*/ byte[] compressedBytes = baos.toByteArray(); ByteArrayEntity httpEntity = new ByteArrayEntity(compressedBytes); httpEntity.setContentType("Text/html;Charset=UTF-8"); httpEntity.setContentEncoding("gzip"); response.setEntity(httpEntity); mockConnectionManager.setResponse(response); Driver driver = createMockDriver(properties, mockConnectionManager); CloseableHttpResponse driverResponse; try { driverResponse = driver.proxy("/", request.build()); fail("We should get an HttpErrorPage"); } catch (HttpErrorPage e) { driverResponse = e.getHttpResponse(); } assertEquals("", HttpResponseUtils.toString(driverResponse)); }
From source file:org.warlock.itk.distributionenvelope.Payload.java
/** * Compress the content according to the RFC1952 GZip algorithm. Since * compression produces binary output, to fit * into an XML document the output has to be base64 encoded, which results * in a 33% increase in size. So the compressed form is only "accepted" if * the attempt results in an overall reduction in size. * //from www .j a va2s . com * @param content * @throws Exception */ private void compressIfViable(byte[] content) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); GZIPOutputStream gzOut = new GZIPOutputStream(out, UNCOMPRESSBUFFERSIZE); gzOut.write(content, 0, content.length); gzOut.finish(); byte[] comp = out.toByteArray(); double ratio = (double) content.length / (double) comp.length; if (ratio > 1.34) { Base64 b64 = new Base64(); comp = b64.encode(comp); payloadBody = new String(comp); compressed = true; } else { compressed = false; if (payloadBody != null) { return; } Base64 b64 = new Base64(); comp = b64.encode(content); payloadBody = new String(comp); base64 = true; } }