Java Code Examples for java.util.zip.Deflater#setLevel()

The following examples show how to use java.util.zip.Deflater#setLevel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ZlibThreadLocal.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public byte[] deflate(byte[] data, int level) throws IOException {
    Deflater deflater = DEFLATER.get();
    deflater.reset();
    deflater.setLevel(level);
    deflater.setInput(data);
    deflater.finish();
    FastByteArrayOutputStream bos = ThreadCache.fbaos.get();
    bos.reset();
    byte[] buffer = BUFFER.get();
    while (!deflater.finished()) {
        int i = deflater.deflate(buffer);
        bos.write(buffer, 0, i);
    }
    //Deflater::end is called the time when the process exits.
    return bos.toByteArray();
}
 
Example 2
Source File: Preprocessor.java    From JuiceboxLegacy with MIT License 6 votes vote down vote up
public Preprocessor(File outputFile, String genomeId, List<Chromosome> chromosomes) {
    this.genomeId = genomeId;
    this.outputFile = outputFile;
    this.matrixPositions = new LinkedHashMap<String, IndexEntry>();

    this.chromosomes = chromosomes;
    chromosomeIndexes = new Hashtable<String, Integer>();
    for (int i = 0; i < chromosomes.size(); i++) {
        chromosomeIndexes.put(chromosomes.get(i).getName(), i);
    }

    compressor = new Deflater();
    compressor.setLevel(Deflater.DEFAULT_COMPRESSION);

    this.tmpDir = null;  // TODO -- specify this

}
 
Example 3
Source File: CommonCompression.java    From lumongo with Apache License 2.0 6 votes vote down vote up
public static byte[] compressZlib(byte[] bytes, CompressionLevel compressionLevel) {
	Deflater compressor = new Deflater();
	compressor.setLevel(compressionLevel.getLevel());
	compressor.setInput(bytes);
	compressor.finish();

	int bufferLength = Math.max(bytes.length / 10, 16);
	byte[] buf = new byte[bufferLength];
	ByteArrayOutputStream bos = new ByteArrayOutputStream(bufferLength);
	while (!compressor.finished()) {
		int count = compressor.deflate(buf);
		bos.write(buf, 0, count);
	}
	try {
		bos.close();
	}
	catch (Exception e) {

	}
	compressor.end();
	return bos.toByteArray();
}
 
Example 4
Source File: Compression.java    From lumongo with Apache License 2.0 6 votes vote down vote up
public static byte[] compressZlib(byte[] bytes, CompressionLevel compressionLevel) {
	Deflater compressor = new Deflater();
	compressor.setLevel(compressionLevel.getLevel());
	compressor.setInput(bytes);
	compressor.finish();

	int bufferLength = Math.max(bytes.length / 10, 16);
	byte[] buf = new byte[bufferLength];
	ByteArrayOutputStream bos = new ByteArrayOutputStream(bufferLength);
	while (!compressor.finished()) {
		int count = compressor.deflate(buf);
		bos.write(buf, 0, count);
	}
	try {
		bos.close();
	}
	catch (Exception e) {

	}
	compressor.end();
	return bos.toByteArray();
}
 
Example 5
Source File: ByteZipUtil.java    From one-net with Apache License 2.0 5 votes vote down vote up
public static byte[] zipBytes(byte[] input) {
    Deflater compresser = new Deflater();
    compresser.setLevel(Deflater.BEST_COMPRESSION);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    compresser.setInput(input);
    byte[] output = new byte[4096];
    compresser.finish();
    int compressedDataLength;
    do {
        compressedDataLength = compresser.deflate(output);
        baos.write(output, 0, compressedDataLength);
    } while (!compresser.finished());
    compresser.end();
    return baos.toByteArray();
}
 
Example 6
Source File: BiliDanmukuCompressionTools.java    From HeroVideo-master with Apache License 2.0 5 votes vote down vote up
public static byte[] compress(byte[] value, int offset, int length, int compressionLevel)
{

    ByteArrayOutputStream bos = new ByteArrayOutputStream(length);

    Deflater compressor = new Deflater();

    try
    {
        compressor.setLevel(compressionLevel); // 将当前压缩级别设置为指定值。
        compressor.setInput(value, offset, length);
        compressor.finish(); // 调用时,指示压缩应当以输入缓冲区的当前内容结尾。

        // Compress the data
        final byte[] buf = new byte[1024];
        while (!compressor.finished())
        {
            // 如果已到达压缩数据输出流的结尾,则返回 true。
            int count = compressor.deflate(buf);
            // 使用压缩数据填充指定缓冲区。
            bos.write(buf, 0, count);
        }
    } finally
    {
        compressor.end(); // 关闭解压缩器并放弃所有未处理的输入。
    }

    return bos.toByteArray();
}
 
Example 7
Source File: ByteTools.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
public static String compressBase64String(byte[] data) throws Exception {
	try (ByteArrayOutputStream baos = new ByteArrayOutputStream(data.length)) {
		Deflater compressor = new Deflater();
		compressor.setLevel(Deflater.BEST_COMPRESSION);
		compressor.setInput(data);
		compressor.finish();
		byte[] buf = new byte[1024];
		while (!compressor.finished()) {
			int count = compressor.deflate(buf);
			baos.write(buf, 0, count);
		}
		return Base64.encodeBase64String(baos.toByteArray());
	}
}
 
Example 8
Source File: BiliDanmukuCompressionTools.java    From HeroVideo-master with Apache License 2.0 5 votes vote down vote up
public static byte[] compress(byte[] value, int offset, int length, int compressionLevel)
{

    ByteArrayOutputStream bos = new ByteArrayOutputStream(length);

    Deflater compressor = new Deflater();

    try
    {
        compressor.setLevel(compressionLevel); // 将当前压缩级别设置为指定值。
        compressor.setInput(value, offset, length);
        compressor.finish(); // 调用时,指示压缩应当以输入缓冲区的当前内容结尾。

        // Compress the data
        final byte[] buf = new byte[1024];
        while (!compressor.finished())
        {
            // 如果已到达压缩数据输出流的结尾,则返回 true。
            int count = compressor.deflate(buf);
            // 使用压缩数据填充指定缓冲区。
            bos.write(buf, 0, count);
        }
    } finally
    {
        compressor.end(); // 关闭解压缩器并放弃所有未处理的输入。
    }

    return bos.toByteArray();
}
 
Example 9
Source File: Compression.java    From gepard with MIT License 5 votes vote down vote up
public static byte[] compressByteArray(byte[] input) {
   
	// 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.
	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) {
    }
   
    //return the compressed data
    return bos.toByteArray();
}
 
Example 10
Source File: DefaultDeflateCompressionDiviner.java    From archive-patcher with Apache License 2.0 5 votes vote down vote up
/**
 * Determines the original {@link JreDeflateParameters} that were used to compress a given piece
 * of deflated delivery.
 *
 * @param compressedDataInputStreamFactory a {@link MultiViewInputStreamFactory} that can provide
 *     multiple independent {@link InputStream} instances for the compressed delivery.
 * @return the parameters that can be used to replicate the compressed delivery in the {@link
 *     DefaultDeflateCompatibilityWindow}, if any; otherwise <code>null</code>. Note that <code>
 *     null</code> is also returned in the case of <em>corrupt</em> zip delivery since, by definition,
 *     it cannot be replicated via any combination of normal deflate parameters.
 * @throws IOException if there is a problem reading the delivery, i.e. if the file contents are
 *     changed while reading
 */
public JreDeflateParameters divineDeflateParameters(
    MultiViewInputStreamFactory compressedDataInputStreamFactory) throws IOException {
  byte[] copyBuffer = new byte[32 * 1024];
  // Iterate over all relevant combinations of nowrap, strategy and level.
  for (boolean nowrap : new boolean[] {true, false}) {
    Inflater inflater = new Inflater(nowrap);
    Deflater deflater = new Deflater(0, nowrap);

    strategy_loop:
    for (int strategy : new int[] {0, 1, 2}) {
      deflater.setStrategy(strategy);
      for (int level : LEVELS_BY_STRATEGY.get(strategy)) {
        deflater.setLevel(level);
        inflater.reset();
        deflater.reset();
        try {
          if (matches(inflater, deflater, compressedDataInputStreamFactory, copyBuffer)) {
            end(inflater, deflater);
            return JreDeflateParameters.of(level, strategy, nowrap);
          }
        } catch (ZipException e) {
          // Parse error in input. The only possibilities are corruption or the wrong nowrap.
          // Skip all remaining levels and strategies.
          break strategy_loop;
        }
      }
    }
    end(inflater, deflater);
  }
  return null;
}
 
Example 11
Source File: ObjectByteConverter.java    From scheduling with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Convert the given Serializable Object into a byte array.
 * <p>
 * The returned byteArray can be compressed by setting compress boolean argument value to <code>true</code>.
 *
 * @param obj      the Serializable object to be compressed
 * @param compress true if the returned byteArray must be also compressed, false if no compression is required.
 * @return a compressed (or not) byteArray representing the Serialization of the given object.
 */
public static final byte[] objectToByteArray(Object obj, boolean compress) {
    if (obj == null) {
        return null;
    }
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos)) {
        oos.writeObject(obj);
        oos.flush();
        if (!compress) {
            // Return the UNCOMPRESSED data
            return baos.toByteArray();
        } else {
            // Compressor with highest level of compression
            Deflater compressor = new Deflater();
            compressor.setLevel(Deflater.BEST_COMPRESSION);
            // Give the compressor the data to compress
            compressor.setInput(baos.toByteArray());
            compressor.finish();

            // Create an expandable byte array to hold the compressed data.
            try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {

                // Compress the data
                byte[] buf = new byte[512];
                while (!compressor.finished()) {
                    int count = compressor.deflate(buf);
                    bos.write(buf, 0, count);
                }
                // Return the COMPRESSED data
                return bos.toByteArray();
            }
        }
    } catch (IOException e) {
        throw new RuntimeException("Could not convert to byte array object ", e);
    }
}
 
Example 12
Source File: CompressUtil.java    From s3-bucket-loader with Apache License 2.0 5 votes vote down vote up
public static String compressAndB64EncodeUTF8Bytes(byte[] bytes) throws Exception{
	
	byte[] input = bytes;
	
	// 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.
    // It is not necessary that the compressed data will be smaller than
    // the uncompressed data.
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
    
    // Compress the data
    byte[] buf = new byte[32];
    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 new String(Base64.encode(compressedData),"UTF-8");
}
 
Example 13
Source File: Misc.java    From iaf with Apache License 2.0 5 votes vote down vote up
public static byte[] compress(byte[] input) 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);
		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);
		}
		bos.close();

		// Get the compressed data
		return bos.toByteArray();
	}
 
Example 14
Source File: CompressionUtil.java    From commons-jcs with Apache License 2.0 5 votes vote down vote up
/**
 * Compress the byte array passed
 * <p>
 * @param input byte array
 * @param bufferLength buffer length
 * @return compressed byte array
 * @throws IOException thrown if we can't close the output stream
 */
public static byte[] compressByteArray( byte[] input, int bufferLength )
    throws IOException
{
    // 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.
    // It is not necessary that the compressed data will be smaller than
    // the uncompressed data.
    ByteArrayOutputStream bos = new ByteArrayOutputStream( input.length );

    // Compress the data
    byte[] buf = new byte[bufferLength];
    while ( !compressor.finished() )
    {
        int count = compressor.deflate( buf );
        bos.write( buf, 0, count );
    }

    // JCS-136 ( Details here : http://www.devguli.com/blog/eng/java-deflater-and-outofmemoryerror/ )
    compressor.end();
    bos.close();

    // Get the compressed data
    return bos.toByteArray();

}
 
Example 15
Source File: Preprocessor.java    From Juicebox with MIT License 4 votes vote down vote up
protected Deflater getDefaultCompressor() {
    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.DEFAULT_COMPRESSION);
    return compressor;
}
 
Example 16
Source File: ZipUtils.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 4 votes vote down vote up
public Compresser(int level) {
	deflater = new Deflater();
	deflater.setLevel(level);
	outBuf = null;
	outStream = null;
}