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:org.apache.qpid.multiconsumer.AMQTest.java
private String compressString(String string) throws Exception { long start = System.currentTimeMillis(); byte[] input = string.getBytes(); Deflater compressor = new Deflater(Deflater.BEST_COMPRESSION); compressor.setInput(input);// w w w. jav a2 s.c o m compressor.finish(); // Get byte array from output of compressor ByteArrayOutputStream baos = new ByteArrayOutputStream(input.length); byte[] buf = new byte[1024]; while (!compressor.finished()) { int cnt = compressor.deflate(buf); baos.write(buf, 0, cnt); } baos.close(); byte[] output = baos.toByteArray(); // Convert byte array into String byte[] base64 = Base64.encodeBase64(output); String sComp = new String(base64, UTF8); long diff = System.currentTimeMillis() - start; System.out.println( "Compressed text from " + input.length + " to " + sComp.getBytes().length + " in " + diff + " ms"); System.out.println("Compressed text = '" + sComp + "'"); return sComp; }
From source file:com.igormaznitsa.jcp.expression.functions.FunctionBINFILE.java
@Nonnull private static byte[] deflate(@Nonnull final byte[] data) throws IOException { final Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION); deflater.setInput(data);/* ww w . java 2 s . co m*/ final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length); deflater.finish(); final byte[] buffer = new byte[1024]; while (!deflater.finished()) { final int count = deflater.deflate(buffer); outputStream.write(buffer, 0, count); } outputStream.close(); final byte[] output = outputStream.toByteArray(); deflater.end(); return output; }
From source file:org.apache.sling.maven.bundlesupport.AbstractBundleDeployMojo.java
/** * Change the version in jar//from w w w . j a v a 2s . c o m * * @param newVersion * @param file * @return * @throws MojoExecutionException */ protected File changeVersion(File file, String oldVersion, String newVersion) throws MojoExecutionException { String fileName = file.getName(); int pos = fileName.indexOf(oldVersion); fileName = fileName.substring(0, pos) + newVersion + fileName.substring(pos + oldVersion.length()); JarInputStream jis = null; JarOutputStream jos; OutputStream out = null; JarFile sourceJar = null; try { // now create a temporary file and update the version sourceJar = new JarFile(file); final Manifest manifest = sourceJar.getManifest(); manifest.getMainAttributes().putValue("Bundle-Version", newVersion); jis = new JarInputStream(new FileInputStream(file)); final File destJar = new File(file.getParentFile(), fileName); out = new FileOutputStream(destJar); jos = new JarOutputStream(out, manifest); jos.setMethod(JarOutputStream.DEFLATED); jos.setLevel(Deflater.BEST_COMPRESSION); JarEntry entryIn = jis.getNextJarEntry(); while (entryIn != null) { JarEntry entryOut = new JarEntry(entryIn.getName()); entryOut.setTime(entryIn.getTime()); entryOut.setComment(entryIn.getComment()); jos.putNextEntry(entryOut); if (!entryIn.isDirectory()) { IOUtils.copy(jis, jos); } jos.closeEntry(); jis.closeEntry(); entryIn = jis.getNextJarEntry(); } // close the JAR file now to force writing jos.close(); return destJar; } catch (IOException ioe) { throw new MojoExecutionException("Unable to update version in jar file.", ioe); } finally { if (sourceJar != null) { try { sourceJar.close(); } catch (IOException ex) { // close } } IOUtils.closeQuietly(jis); IOUtils.closeQuietly(out); } }
From source file:com.atolcd.web.scripts.ZipContents.java
public void createZipFile(List<String> nodeIds, OutputStream os, boolean noaccent) throws IOException { File zip = null;// w w w.j a v a 2 s .com try { if (nodeIds != null && !nodeIds.isEmpty()) { zip = TempFileProvider.createTempFile(TEMP_FILE_PREFIX, ZIP_EXTENSION); FileOutputStream stream = new FileOutputStream(zip); CheckedOutputStream checksum = new CheckedOutputStream(stream, new Adler32()); BufferedOutputStream buff = new BufferedOutputStream(checksum); ZipArchiveOutputStream out = new ZipArchiveOutputStream(buff); out.setEncoding(encoding); out.setMethod(ZipArchiveOutputStream.DEFLATED); out.setLevel(Deflater.BEST_COMPRESSION); if (logger.isDebugEnabled()) { logger.debug("Using encoding '" + encoding + "' for zip file."); } try { for (String nodeId : nodeIds) { NodeRef node = new NodeRef(storeRef, nodeId); addToZip(node, out, noaccent, ""); } } catch (Exception e) { logger.error(e.getMessage(), e); throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, e.getMessage()); } finally { out.close(); buff.close(); checksum.close(); stream.close(); if (nodeIds.size() > 0) { InputStream in = new FileInputStream(zip); try { byte[] buffer = new byte[BUFFER_SIZE]; int len; while ((len = in.read(buffer)) > 0) { os.write(buffer, 0, len); } } finally { IOUtils.closeQuietly(in); } } } } } catch (Exception e) { logger.error(e.getMessage(), e); throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, e.getMessage()); } finally { // try and delete the temporary file if (zip != null) { zip.delete(); } } }
From source file:jfs.sync.encryption.JFSEncryptedStream.java
private void internalClose() throws IOException { delegate.close();//ww w .j a va2 s . c om byte[] bytes = delegate.toByteArray(); final byte[] originalBytes = bytes; long l = bytes.length; byte marker = COMPRESSION_NONE; if (log.isDebugEnabled()) { log.debug("close() checking for compressions for"); } // if CompressionThread dt = new CompressionThread(originalBytes) { @Override public void run() { try { ByteArrayOutputStream deflaterStream = new ByteArrayOutputStream(); Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION, true); OutputStream dos = new DeflaterOutputStream(deflaterStream, deflater, COMPRESSION_BUFFER_SIZE); dos.write(originalBytes); dos.close(); compressedValue = deflaterStream.toByteArray(); } catch (Exception e) { log.error("run()", e); } // try/catch } // run() }; CompressionThread bt = new CompressionThread(originalBytes) { @Override public void run() { try { if (originalBytes.length > BZIP_MAX_LENGTH) { compressedValue = originalBytes; } else { ByteArrayOutputStream bzipStream = new ByteArrayOutputStream(); OutputStream bos = new BZip2CompressorOutputStream(bzipStream); bos.write(originalBytes); bos.close(); compressedValue = bzipStream.toByteArray(); } // if } catch (Exception e) { log.error("run()", e); } // try/catch } // run() }; CompressionThread lt = new CompressionThread(originalBytes) { /* * // " -a{N}: set compression mode - [0, 1], default: 1 (max)\n" + * " -d{N}: set dictionary - [0,28], default: 23 (8MB)\n" * +" -fb{N}: set number of fast bytes - [5, 273], default: 128\n" * +" -lc{N}: set number of literal context bits - [0, 8], default: 3\n" * +" -lp{N}: set number of literal pos bits - [0, 4], default: 0\n" * +" -pb{N}: set number of pos bits - [0, 4], default: 2\n" * +" -mf{MF_ID}: set Match Finder: [bt2, bt4], default: bt4\n"+" -eos: write End Of Stream marker\n"); */ private int dictionarySize = 1 << 23; private int lc = 3; private int lp = 0; private int pb = 2; private int fb = 128; public int algorithm = 2; public int matchFinderIndex = 1; // 0, 1, 2 @Override public void run() { try { Encoder encoder = new Encoder(); encoder.SetEndMarkerMode(false); encoder.SetAlgorithm(algorithm); // Whatever that means encoder.SetDictionarySize(dictionarySize); encoder.SetNumFastBytes(fb); encoder.SetMatchFinder(matchFinderIndex); encoder.SetLcLpPb(lc, lp, pb); ByteArrayOutputStream lzmaStream = new ByteArrayOutputStream(); ByteArrayInputStream inStream = new ByteArrayInputStream(originalBytes); encoder.WriteCoderProperties(lzmaStream); encoder.Code(inStream, lzmaStream, -1, -1, null); compressedValue = lzmaStream.toByteArray(); } catch (Exception e) { log.error("run()", e); } // try/catch } // run() }; dt.start(); bt.start(); lt.start(); try { dt.join(); bt.join(); lt.join(); } catch (InterruptedException e) { log.error("run()", e); } // try/catch if (dt.compressedValue.length < l) { marker = COMPRESSION_DEFLATE; bytes = dt.compressedValue; l = bytes.length; } // if if (lt.compressedValue.length < l) { marker = COMPRESSION_LZMA; bytes = lt.compressedValue; l = bytes.length; } // if if (bt.compressedValue.length < l) { marker = COMPRESSION_BZIP2; bytes = bt.compressedValue; if (log.isWarnEnabled()) { log.warn("close() using bzip2 and saving " + (l - bytes.length) + " bytes."); } // if l = bytes.length; } // if if (log.isInfoEnabled()) { if (marker == COMPRESSION_NONE) { if (log.isInfoEnabled()) { log.info("close() using no compression"); } // if } // if if (marker == COMPRESSION_LZMA) { if (log.isInfoEnabled()) { log.info("close() using lzma"); } // if } // if } // if ObjectOutputStream oos = new ObjectOutputStream(baseOutputStream); oos.writeByte(marker); oos.writeLong(originalBytes.length); oos.flush(); OutputStream out = baseOutputStream; if (cipher != null) { out = new CipherOutputStream(out, cipher); } // if out.write(bytes); out.close(); delegate = null; baseOutputStream = null; }
From source file:edu.stanford.junction.addon.JSONObjWrapper.java
private static String compressString(String str) { byte[] input; try {/* w ww. j a v a 2 s. c o m*/ input = str.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { input = str.getBytes(); } // Create the compressor with highest level of compression Deflater compressor = new Deflater(); compressor.setLevel(Deflater.BEST_COMPRESSION); // Give the compressor the data to compress compressor.setInput(input); compressor.finish(); // Create an expandable byte array to hold the compressed data. // You cannot use an array that's the same size as the orginal because // there is no guarantee that the compressed data will be smaller than // the uncompressed data. ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length); // Compress the data byte[] buf = new byte[1024]; while (!compressor.finished()) { int count = compressor.deflate(buf); bos.write(buf, 0, count); } try { bos.close(); } catch (IOException e) { } // Get the compressed data byte[] compressedData = bos.toByteArray(); return Base64.encodeBytes(compressedData); }
From source file:org.mrgeo.rasterops.GeoTiffExporter.java
public static void export(final RenderedImage image, final Bounds bounds, final OutputStream os, final boolean replaceNan, final String xmp, final Number nodata) throws IOException { OpImageRegistrar.registerMrGeoOps(); final TIFFEncodeParam param = new TIFFEncodeParam(); // The version of GDAL that Legion is using requires a tile size > 1 param.setTileSize(image.getTileWidth(), image.getTileHeight()); param.setWriteTiled(true);/* www. j a va 2 s . c o m*/ // if the image only has 1 pixel, the value of this pixel changes after compressing (especially // if this pixel is no data value. e.g -9999 changes to -8192 when read the image back). // So don't do compress if the image has only 1 pixel. if (image.getWidth() > 1 && image.getHeight() > 1) { // Deflate lossless compression (also known as "Zip-in-TIFF") param.setCompression(TIFFEncodeParam.COMPRESSION_DEFLATE); param.setDeflateLevel(Deflater.BEST_COMPRESSION); } final GeoTIFFDirectory dir = new GeoTIFFDirectory(); // GTModelTypeGeoKey : using geographic coordinate system. dir.addGeoKey(new XTIFFField(1024, XTIFFField.TIFF_SHORT, 1, new char[] { 2 })); // GTRasterTypeGeoKey : pixel is point dir.addGeoKey(new XTIFFField(1025, XTIFFField.TIFF_SHORT, 1, new char[] { 1 })); // GeographicTypeGeoKey : 4326 WGS84 dir.addGeoKey(new XTIFFField(2048, XTIFFField.TIFF_SHORT, 1, new char[] { 4326 })); dir.addGeoKey(new XTIFFField(2049, XTIFFField.TIFF_ASCII, 7, new String[] { "WGS 84" })); // GeogAngularUnitsGeoKey : Angular Degree dir.addGeoKey(new XTIFFField(2054, XTIFFField.TIFF_SHORT, 1, new char[] { 9102 })); if (xmp != null) { final byte[] b = xmp.getBytes("UTF8"); dir.addField(new XTIFFField(700, XTIFFField.TIFF_BYTE, b.length, b)); } dir.getFields(); final double[] tiePoints = new double[6]; tiePoints[0] = 0.0; tiePoints[1] = 0.0; tiePoints[2] = 0.0; tiePoints[3] = bounds.getMinX(); tiePoints[4] = bounds.getMaxY(); tiePoints[5] = 0.0; dir.setTiepoints(tiePoints); final double[] pixelScale = new double[3]; pixelScale[0] = bounds.getWidth() / image.getWidth(); pixelScale[1] = bounds.getHeight() / image.getHeight(); pixelScale[2] = 0; dir.setPixelScale(pixelScale); final Vector<TIFFField> fields = toTiffField(dir.getFields()); RenderedImage output = image; final String[] nullValues = new String[1]; switch (image.getSampleModel().getDataType()) { case DataBuffer.TYPE_DOUBLE: nullValues[0] = Double.toString(nodata.doubleValue()); if (replaceNan) { output = ReplaceNanDescriptor.create(image, nodata.doubleValue()); } // Tiff exporter doesn't handle doubles. Yuck! output = ConvertToFloatDescriptor.create(output); // Double.NaN (our default nodata on ingest) should not be written out as nodata on export // (i.e. GeoTiffs imported without NODATA metadata field should be exported as such) if (!Double.isNaN(nodata.doubleValue())) { fields.add(new TIFFField(NULL_TAG, XTIFFField.TIFF_ASCII, 1, nullValues)); } break; case DataBuffer.TYPE_FLOAT: nullValues[0] = Double.toString(nodata.floatValue()); if (replaceNan) { output = ReplaceNanDescriptor.create(image, nodata.floatValue()); } // Float.NaN (our default nodata on ingest) should not be written out as nodata on export // (i.e. GeoTiffs imported without NODATA metadata field should be exported as such) if (!Float.isNaN(nodata.floatValue())) { fields.add(new TIFFField(NULL_TAG, XTIFFField.TIFF_ASCII, 1, nullValues)); } break; case DataBuffer.TYPE_INT: case DataBuffer.TYPE_USHORT: case DataBuffer.TYPE_SHORT: case DataBuffer.TYPE_BYTE: nullValues[0] = Integer.toString(nodata.intValue()); fields.add(new TIFFField(NULL_TAG, XTIFFField.TIFF_ASCII, 1, nullValues)); break; } param.setExtraFields(fields.toArray(new TIFFField[0])); EncodeDescriptor.create(output, os, "TIFF", param, null); }
From source file:com.bigdata.dastor.utils.FBUtilities.java
public static void compressToStream(byte[] input, ByteArrayOutputStream bos) throws IOException { // Create the compressor with highest level of compression Deflater compressor = new Deflater(); compressor.setLevel(Deflater.BEST_COMPRESSION); // Give the compressor the data to compress compressor.setInput(input);//from ww w . j a va 2 s. c o m compressor.finish(); // Write the compressed data to the stream byte[] buf = new byte[1024]; while (!compressor.finished()) { int count = compressor.deflate(buf); bos.write(buf, 0, count); } }
From source file:com.l2jfree.sql.L2DataSource.java
protected static final boolean writeBackup(String databaseName, InputStream in) throws IOException { FileUtils.forceMkdir(new File("backup/database")); final Date time = new Date(); final L2TextBuilder tb = new L2TextBuilder(); tb.append("backup/database/DatabaseBackup_"); tb.append(new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date())); tb.append("_uptime-").append(L2Config.getShortUptime()); tb.append(".zip"); final File backupFile = new File(tb.moveToString()); int written = 0; ZipOutputStream out = null;//from w ww . ja v a 2 s.co m try { out = new ZipOutputStream(new FileOutputStream(backupFile)); out.setMethod(ZipOutputStream.DEFLATED); out.setLevel(Deflater.BEST_COMPRESSION); out.setComment("L2jFree Schema Backup Utility\r\n\r\nBackup date: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS z").format(new Date())); out.putNextEntry(new ZipEntry(databaseName + ".sql")); byte[] buf = new byte[4096]; for (int read; (read = in.read(buf)) != -1;) { out.write(buf, 0, read); written += read; } } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); } if (written == 0) { backupFile.delete(); return false; } _log.info("DatabaseBackupManager: Database `" + databaseName + "` backed up successfully in " + (System.currentTimeMillis() - time.getTime()) / 1000 + " s."); return true; }
From source file:com.ibm.jaggr.core.impl.cache.GzipCacheImpl.java
@Override public InputStream getInputStream(final String key, final URI source, final MutableInt retLength) throws IOException { final String sourceMethod = "getInputStream"; //$NON-NLS-1$ final boolean isTraceLogging = log.isLoggable(Level.FINER); if (isTraceLogging) { log.entering(sourceClass, sourceMethod, new Object[] { key, source, retLength }); }//from w w w . j a va2s.c o m InputStream in = null, result = null; CacheEntry tryCacheEntry = (CacheEntry) super.get(key); URLConnection connection = source.toURL().openConnection(); try { long lastModified = connection.getLastModified(); if (tryCacheEntry != null) { // Make local copies of volatile CacheEntry fields byte[] bytes = tryCacheEntry.bytes; File file = tryCacheEntry.file; if (bytes != null) { // Important - CacheEntry.lastModified is set before CacheEntry.bytes so we can // safely // check CacheEntry.lastModified here even though we're not synchronized. if (lastModified != tryCacheEntry.lastModified) { // stale cache entry. Remove it and create a new one below cacheMap.remove(key, tryCacheEntry); } else { retLength.setValue(tryCacheEntry.bytes.length); result = new ByteArrayInputStream(tryCacheEntry.bytes); } } else if (file != null) { // Some platforms round file last modified times to nearest second. if (Math.abs(lastModified - file.lastModified()) > 1000) { // Stale cache entry, remove it and create a new one below cacheMap.remove(key, tryCacheEntry); // also delete the associated cache file asynchronously. cacheManager.deleteFileDelayed(file.getName()); } else { try { retLength.setValue(file.length()); result = new FileInputStream(file); } catch (FileNotFoundException ex) { // File doesn't exist (was probably deleted outside this program) // Not fatal, just fall through and create it again. cacheMap.remove(key, tryCacheEntry); } } } if (result != null) { // found result in cache. Return it. log.exiting(sourceClass, sourceMethod, result); return result; } } // Result not in cache (or we removed it). Try to create a new cache entry. CacheEntry newCacheEntry = new CacheEntry(); CacheEntry oldCacheEntry = (CacheEntry) cacheMap.putIfAbsent(key, newCacheEntry); final CacheEntry cacheEntry = oldCacheEntry != null ? oldCacheEntry : newCacheEntry; // Synchronize on the cache entry so that more than one thread won't try to create the // zipped content. synchronized (cacheEntry) { if (cacheEntry.ex != null) { // An exception occurred trying to create the gzip response in another thread. // Re-throw the exception here. throw cacheEntry.ex; } // First, check to make sure that another thread didn't beat us to the punch. // Even though we're synchronized on the cacheEntry object, cacheEntry.bytes can be // cleared by the createCacheFileAsync callback, so we need to copy this volatile // field // to a local variable and access it from there. byte[] bytes = cacheEntry.bytes; if (bytes != null) { retLength.setValue(bytes.length); result = new ByteArrayInputStream(bytes); } else if (cacheEntry.file != null) { // once set, cacheEntry.file does not change // by convention retLength.setValue(cacheEntry.file.length()); result = new FileInputStream(cacheEntry.file); } else { // Gzip encode the resource and save the result in the cache entry until the // cache // file is written asynchronously. try { in = connection.getInputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); VariableGZIPOutputStream compress = new VariableGZIPOutputStream(bos, 10240); compress.setLevel(Deflater.BEST_COMPRESSION); CopyUtil.copy(in, compress); // Important - CacheEntry.lastModified must be set before cacheEntry.bytes cacheEntry.lastModified = lastModified; cacheEntry.bytes = bos.toByteArray(); result = new ByteArrayInputStream(cacheEntry.bytes); retLength.setValue(cacheEntry.bytes.length); // Call the cache manager to asynchronously save the gzipped response to // disk // Include the filename part of the source URI in the cached filename String path = source.getPath(); int idx = path.lastIndexOf("/"); //$NON-NLS-1$ String fname = (idx != -1) ? path.substring(idx + 1) : path; cacheManager.createCacheFileAsync(fname + ".gzip.", //$NON-NLS-1$ new ByteArrayInputStream(cacheEntry.bytes), new ICacheManager.CreateCompletionCallback() { @Override public void completed(String filename, Exception e) { if (e != null && log.isLoggable(Level.SEVERE)) { // Exception occurred saving file. Not much we can do // except log the error log.logp(Level.SEVERE, sourceClass, sourceMethod, e.getMessage(), e); return; } File cacheFile = new File(cacheManager.getCacheDir(), filename); cacheFile.setLastModified(cacheEntry.lastModified); // Important - cacheEntry.file must be set before clearing // cacheEntry.bytes cacheEntry.file = cacheFile; cacheEntry.bytes = null; } }); } catch (Throwable t) { cacheEntry.ex = (t instanceof IOException) ? (IOException) t : new IOException(t); cacheMap.remove(key, cacheEntry); throw cacheEntry.ex; } } } } finally { // URLConnection doesn't have a close method. The only way to make sure a connection is // closed is to close the input or output stream which is obtained from the connection. if (in != null) { in.close(); } else { connection.getInputStream().close(); } } if (isTraceLogging) { log.exiting(sourceClass, sourceClass, result); } return result; }