Java Code Examples for java.util.zip.Deflater#BEST_COMPRESSION

The following examples show how to use java.util.zip.Deflater#BEST_COMPRESSION . 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: StringCompressUtils.java    From opencards with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private static String compress2(String s) {
    Deflater defl = new Deflater(Deflater.BEST_COMPRESSION);
    defl.setInput(s.getBytes());
    defl.finish();
    boolean done = false;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    while (!done) {
        byte[] buf = new byte[256];
        int bufnum = defl.deflate(buf);
        bos.write(buf, 0, bufnum);
        if (bufnum < buf.length)
            done = true;
    }
    try {
        bos.flush();
        bos.close();
    } catch (IOException ioe) {
        System.err.println(ioe.toString());
    }
    return toHexString(bos.toByteArray());
}
 
Example 2
Source File: OldAndroidDeflateTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
private void bigTest(int step, int expectedAdler)
        throws UnsupportedEncodingException, DataFormatException {
    byte[] input = new byte[128 * 1024];
    byte[] comp = new byte[128 * 1024 + 512];
    byte[] output = new byte[128 * 1024 + 512];
    Inflater inflater = new Inflater(false);
    Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION, false);

    createSample(input, step);

    compress(deflater, input, comp);
    expand(inflater, comp, (int) deflater.getBytesWritten(), output);

    assertEquals(inflater.getBytesWritten(), input.length);
    assertEquals(deflater.getAdler(), inflater.getAdler());
    assertEquals(deflater.getAdler(), expectedAdler);
}
 
Example 3
Source File: ByteArray.java    From red5-io with Apache License 2.0 6 votes vote down vote up
/**
 * Compress contents using zlib.
 */
public void compress() {
    IoBuffer tmp = IoBuffer.allocate(0);
    tmp.setAutoExpand(true);
    byte[] tmpData = new byte[data.limit()];
    data.position(0);
    data.get(tmpData);
    try (DeflaterOutputStream deflater = new DeflaterOutputStream(tmp.asOutputStream(), new Deflater(Deflater.BEST_COMPRESSION))) {
        deflater.write(tmpData);
        deflater.finish();
    } catch (IOException e) {
        //docs state that free is optional
        tmp.free();
        throw new RuntimeException("could not compress data", e);
    }
    data.free();
    data = tmp;
    data.flip();
    prepareIO();
}
 
Example 4
Source File: CompressingFilterContext.java    From ziplet with Apache License 2.0 6 votes vote down vote up
private static int readCompressionLevelValue(FilterConfig filterConfig)
    throws ServletException {
    String compressionLevelString = filterConfig.getInitParameter("compressionLevel");
    int value;
    if (compressionLevelString != null) {
        try {
            value = Integer.parseInt(compressionLevelString);
        } catch (NumberFormatException nfe) {
            throw new ServletException("Invalid compression level: " + compressionLevelString,
                nfe);
        }
        if (value == -1) {
            value = DEFAULT_COMPRESSION_LEVEL;
        } else if (value < 0) {
            throw new ServletException("Compression level cannot be negative, unless it is -1");
        } else if (value > Deflater.BEST_COMPRESSION) {
            throw new ServletException(
                "Compression level cannot be greater than " + Deflater.BEST_COMPRESSION);
        }
    } else {
        value = DEFAULT_COMPRESSION_LEVEL;
    }
    return value;
}
 
Example 5
Source File: GZMaxFileCompressor.java    From Quelea with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Compresses the given file with maximum compression.
 *
 * @param input the input file to compress.
 * @param output the compressed file.
 * @return true if the operation completed successfully, false otherwise.
 */
public boolean compress(File input, File output) {
    try(FileInputStream inputStream = new FileInputStream(input);
            GZipOutputStreamEx outputStream = new GZipOutputStreamEx(new FileOutputStream(output), 1024, Deflater.BEST_COMPRESSION)) {
        byte[] buf = new byte[1024];
        int length;
        while ((length = inputStream.read(buf)) > 0) {
            outputStream.write(buf, 0 ,length);
        }
    }
    catch (IOException ex) {
        ex.printStackTrace();
        return false;
    }
    System.out.println("Compressed " + input.getAbsolutePath() + " to " + output.getAbsolutePath() + " successfully.");
    return true;
}
 
Example 6
Source File: GzipCommonsCompressor.java    From yosegi with Apache License 2.0 5 votes vote down vote up
private int getCompressLevel( final CompressionPolicy compressionPolicy ) {
  switch ( compressionPolicy ) {
    case BEST_SPEED:
      return Deflater.BEST_SPEED;
    case SPEED:
      return 6 - 2;
    case DEFAULT:
      return 6;
    case BEST_COMPRESSION:
      return Deflater.BEST_COMPRESSION;
    default:
      return 6;
  }
}
 
Example 7
Source File: TestJavaSerializer.java    From copper-engine with Apache License 2.0 5 votes vote down vote up
public void setCompressorMaxSize(int compressorMaxSize) {
    this.compressorMaxSize = compressorMaxSize;
    compressorTL = new ThreadLocal<Compressor>() {
        @Override
        protected Compressor initialValue() {
            return new Compressor(Deflater.BEST_COMPRESSION, TestJavaSerializer.this.compressorMaxSize);
        }
    };
}
 
Example 8
Source File: BestGZIPOutputStream.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Constructs a new {@code GZIPOutputStream} to write data in GZIP format to
 * the given stream with the given internal buffer size and
 * flushing behavior (see {@link DeflaterOutputStream#flush}).
 *
 * @since 1.7
 */
public BestGZIPOutputStream(OutputStream os, int bufferSize, boolean syncFlush) throws IOException {
    super(os, new Deflater(Deflater.BEST_COMPRESSION, true), bufferSize, syncFlush);
    writeShort(GZIPInputStream.GZIP_MAGIC);
    out.write(Deflater.DEFLATED);
    out.write(0); // flags
    writeLong(0); // mod time
    out.write(0); // extra flags
    out.write(0); // operating system
}
 
Example 9
Source File: PackedTransaction.java    From EosCommander with MIT License 5 votes vote down vote up
private byte[] compress( byte[] uncompressedBytes, CompressType compressType) {
    if ( compressType == null || !CompressType.zlib.equals( compressType)) {
        return uncompressedBytes;
    }

    // zip!
    Deflater deflater = new Deflater( Deflater.BEST_COMPRESSION );
    deflater.setInput( uncompressedBytes );

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream( uncompressedBytes.length);
    deflater.finish();
    byte[] buffer = new byte[1024];
    while (!deflater.finished()) {
        int count = deflater.deflate(buffer); // returns the generated code... index
        outputStream.write(buffer, 0, count);
    }

    try {
        outputStream.close();
    }
    catch (IOException e) {
        e.printStackTrace();
        return uncompressedBytes;
    }

    return outputStream.toByteArray();
}
 
Example 10
Source File: HoodieJsonPayload.java    From hudi with Apache License 2.0 5 votes vote down vote up
private byte[] compressData(String jsonData) throws IOException {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
  DeflaterOutputStream dos = new DeflaterOutputStream(baos, deflater, true);
  try {
    dos.write(jsonData.getBytes());
  } finally {
    dos.flush();
    dos.close();
    // Its important to call this.
    // Deflater takes off-heap native memory and does not release until GC kicks in
    deflater.end();
  }
  return baos.toByteArray();
}
 
Example 11
Source File: TestRawTripPayload.java    From hudi with Apache License 2.0 5 votes vote down vote up
private byte[] compressData(String jsonData) throws IOException {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  DeflaterOutputStream dos = new DeflaterOutputStream(baos, new Deflater(Deflater.BEST_COMPRESSION), true);
  try {
    dos.write(jsonData.getBytes());
  } finally {
    dos.flush();
    dos.close();
  }
  return baos.toByteArray();
}
 
Example 12
Source File: Gzip.java    From divolte-collector with Apache License 2.0 5 votes vote down vote up
/**
 * Compress some data as much as possible using the Gzip algorithm.
 * @param input the data to compress.
 * @param offset the offset within <code>input</code> of the data to compress.
 * @param length the length of the data within <code>input</code> to compress.
 * @return the compressed data (including Gzip container), or empty
 *      if the compressed data wasn't smaller than the input.
 */
public static Optional<ByteBuffer> compress(final byte[] input,
                                            final int offset,
                                            final int length) {
    // Step 1: Perform the actual compression.
    final Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION, true);
    deflater.setInput(input, offset, length);
    deflater.finish();
    final byte[] payload = new byte[input.length - GZIP_FOOTER_SIZE];
    final int payloadSize = deflater.deflate(payload);
    deflater.end();
    // Step 2: Calculate the CRC for the trailer.
    final CRC32 checksum = new CRC32();
    checksum.update(input);
    // Step 3: Assemble the Gzip container with the compressed data.
    final Optional<ByteBuffer> output;
    final int outputSize = GZIP_OVERHEAD + payloadSize;
    if (outputSize < input.length) {
        final ByteBuffer bb = ByteBuffer.allocate(outputSize)
                                        .order(ByteOrder.LITTLE_ENDIAN);
        bb.put(GZIP_HEADER)
          .put(payload, 0, payloadSize)
          .putInt((int)checksum.getValue())
          .putInt(input.length);
        bb.flip();
        output = Optional.of(bb);
    } else {
        // The output wasn't smaller than the input.
        output = Optional.empty();
    }
    return output;
}
 
Example 13
Source File: mxPngImageEncoder.java    From blog-codes with Apache License 2.0 5 votes vote down vote up
private void writeZTXT() throws IOException
{
	if (param.isCompressedTextSet())
	{
		String[] text = param.getCompressedText();

		for (int i = 0; i < text.length / 2; i++)
		{
			byte[] keyword = text[2 * i].getBytes();
			byte[] value = text[2 * i + 1].getBytes();

			ChunkStream cs = new ChunkStream("zTXt");

			cs.write(keyword, 0, Math.min(keyword.length, 79));
			cs.write(0);
			cs.write(0);

			DeflaterOutputStream dos = new DeflaterOutputStream(cs,
					new Deflater(Deflater.BEST_COMPRESSION, true));
			dos.write(value);
			dos.finish();

			cs.writeToStream(dataOutput);
			cs.close();
		}
	}
}
 
Example 14
Source File: GzipCompressor.java    From yosegi with Apache License 2.0 5 votes vote down vote up
private int getCompressLevel( final CompressionPolicy compressionPolicy ) {
  switch ( compressionPolicy ) {
    case BEST_SPEED:
      return Deflater.BEST_SPEED;
    case SPEED:
      return 6 - 2;
    case DEFAULT:
      return 6;
    case BEST_COMPRESSION:
      return Deflater.BEST_COMPRESSION;
    default:
      return 6;
  }
}
 
Example 15
Source File: CompressedTextFile.java    From SPADE with GNU General Public License v3.0 5 votes vote down vote up
@Override
  public boolean initialize(String arguments) {
clock = System.currentTimeMillis();
  	scaffoldInMemory = new HashMap<Integer, Pair<SortedSet<Integer>, SortedSet<Integer>>>();
edgesInMemory = 0;
hashToID = new HashMap<String, Integer>();
alreadyRenamed = new Vector<String>();
compresser = new Deflater(Deflater.BEST_COMPRESSION);
W=10;
L=5;
nextVertexID = 0;
      try {
          if (arguments == null) {
              return false;
          }
          annotationsFile = new File(filePath + "/annotations.txt");
          scaffoldFile = new File(filePath + "/scaffold.txt");
          annotationsScanner = new Scanner(annotationsFile);
          scaffoldScanner = new Scanner(scaffoldFile);
	benchmarks = new PrintWriter("/Users/melanie/Documents/benchmarks/compression_time_TextFile.txt", "UTF-8");
          filePath = arguments;
          scaffoldWriter = new FileWriter(filePath + "/scaffold.txt", false);
          scaffoldWriter.write("[BEGIN]\n");
          annotationsWriter = new FileWriter(filePath + "/annotations.txt", false);
          annotationsWriter.write("[BEGIN]\n");
          
          return true;
      } catch (Exception ex) {
      	logger.log(Level.SEVERE, "Compressed Storage Initialized not successful!", ex);
      	return false;
      }
  }
 
Example 16
Source File: ControlFlowGraphPlantUMLWriter.java    From jd-core with GNU General Public License v3.0 5 votes vote down vote up
public static String writePlantUMLUrl(String plantuml) throws Exception {
    byte[] input = plantuml.getBytes("UTF-8");

    // Compress
    Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION, true);

    deflater.setInput(input);
    deflater.finish();

    byte[] output = new byte[input.length * 2];
    int compressedDataLength = deflater.deflate(output);

    if (deflater.finished()) {
        // Encode
        final StringBuilder sb = new StringBuilder((output.length*4 + 2) / 3);

        for (int i=0; i<compressedDataLength; i+=3) {
            append3bytes(
                sb,
                output[i] & 0xFF,
                i+1 < output.length ? output[i+1] & 0xFF : 0,
                i+2 < output.length ? output[i+2] & 0xFF : 0);
        }

        return PLANTUML_URL_PREFIX + sb.toString();
    } else {
        return null;
    }
}
 
Example 17
Source File: EntryAccounting.java    From buck with Apache License 2.0 4 votes vote down vote up
public long writeLocalFileHeader(OutputStream out) throws IOException {
  if (method == Method.DEFLATE && entry instanceof CustomZipEntry) {
    // See http://www.pkware.com/documents/casestudies/APPNOTE.TXT (section 4.4.4)
    // Essentially, we're about to set bits 1 and 2 to indicate to tools such as zipinfo which
    // level of compression we're using. If we've not set a compression level, then we're using
    // the default one, which is right. It turns out. For your viewing pleasure:
    //
    // +----------+-------+-------+
    // | Level    | Bit 1 | Bit 2 |
    // +----------+-------+-------+
    // | Fastest  |   0   |   1   |
    // | Normal   |   0   |   0   |
    // | Best     |   1   |   0   |
    // +----------+-------+-------+
    int level = ((CustomZipEntry) entry).getCompressionLevel();
    switch (level) {
      case Deflater.BEST_COMPRESSION:
        flags |= (1 << 1);
        break;

      case Deflater.BEST_SPEED:
        flags |= (1 << 2);
        break;
    }
  }

  if (requiresDataDescriptor()) {
    flags |= DATA_DESCRIPTOR_FLAG;
  }

  CountingOutputStream stream = new CountingOutputStream(out);
  ByteIo.writeInt(stream, ZipEntry.LOCSIG);

  boolean useZip64;
  if (!requiresDataDescriptor() && entry.getSize() >= ZipConstants.ZIP64_MAGICVAL) {
    useZip64 = true;
  } else {
    useZip64 = false;
  }

  ByteIo.writeShort(stream, getRequiredExtractVersion(useZip64));
  ByteIo.writeShort(stream, flags);
  ByteIo.writeShort(stream, getCompressionMethod());
  ByteIo.writeInt(stream, getTime());

  // If we don't know the size or CRC of the data in advance (such as when in deflate mode),
  // we write zeros now, and append the actual values (the data descriptor) after the entry
  // bytes has been fully written.
  if (requiresDataDescriptor()) {
    ByteIo.writeInt(stream, 0);
    ByteIo.writeInt(stream, 0);
    ByteIo.writeInt(stream, 0);
  } else {
    ByteIo.writeInt(stream, entry.getCrc());
    if (entry.getSize() >= ZipConstants.ZIP64_MAGICVAL) {
      ByteIo.writeInt(stream, ZipConstants.ZIP64_MAGICVAL);
      ByteIo.writeInt(stream, ZipConstants.ZIP64_MAGICVAL);
    } else {
      ByteIo.writeInt(stream, entry.getSize());
      ByteIo.writeInt(stream, entry.getSize());
    }
  }

  byte[] nameBytes = entry.getName().getBytes(Charsets.UTF_8);
  ByteIo.writeShort(stream, nameBytes.length);
  ByteIo.writeShort(stream, useZip64 ? ZipConstants.ZIP64_LOCHDR : 0);
  stream.write(nameBytes);
  if (useZip64) {
    ByteIo.writeShort(stream, ZipConstants.ZIP64_EXTID);
    ByteIo.writeShort(stream, 16);
    ByteIo.writeLong(stream, entry.getSize());
    ByteIo.writeLong(stream, entry.getSize());
  }

  return stream.getCount();
}
 
Example 18
Source File: ZippedByteArrayOutputStream.java    From dsl-devkit with Eclipse Public License 1.0 4 votes vote down vote up
public ZippedByteArrayOutputStream(final int size) {
  super(new ByteArrayOutputStream(size), new Deflater(Deflater.BEST_COMPRESSION));
}
 
Example 19
Source File: TezCommonUtils.java    From tez with Apache License 2.0 4 votes vote down vote up
@Private
public static Deflater newBestCompressionDeflater() {
  return new Deflater(Deflater.BEST_COMPRESSION, NO_WRAP);
}
 
Example 20
Source File: TestRecordCompressor_BestCompression.java    From database with GNU General Public License v2.0 2 votes vote down vote up
public IRecordCompressor getInstance() {

        return new RecordCompressor(Deflater.BEST_COMPRESSION);

    }