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

The following examples show how to use java.util.zip.Deflater#setStrategy() . 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: 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 2
Source File: BinaryCasSerDes4.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Method:
 *   write with deflation into a single byte array stream
 *     skip if not worth deflating
 *     skip the Slot_Control stream
 *     record in the Slot_Control stream, for each deflated stream:
 *       the Slot index
 *       the number of compressed bytes
 *       the number of uncompressed bytes
 *   add to header:  
 *     nbr of compressed entries
 *     the Slot_Control stream size
 *     the Slot_Control stream
 *     all the zipped streams
 *
 * @throws IOException passthru
 */
private void collectAndZip() throws IOException {
  ByteArrayOutputStream baosZipped = new ByteArrayOutputStream(4096);
  Deflater deflater = new Deflater(compressLevel.lvl, true);
  deflater.setStrategy(compressStrategy.strat);
  int nbrEntries = 0;
  
  List<Integer> idxAndLen = new ArrayList<>();

  for (int i = 0; i < baosZipSources.length; i++) {
    ByteArrayOutputStream baos = baosZipSources[i];
    if (baos != null) {
      nbrEntries ++;
      dosZipSources[i].close();
      long startTime = System.currentTimeMillis();
      int zipBufSize = Math.max(1024, baos.size() / 100);
      deflater.reset();
      DeflaterOutputStream cds = new DeflaterOutputStream(baosZipped, deflater, zipBufSize);       
      baos.writeTo(cds);
      cds.close();
      idxAndLen.add(i);
      if (doMeasurement) {
        idxAndLen.add((int) (sm.statDetails[i].afterZip = deflater.getBytesWritten()));
        idxAndLen.add((int) (sm.statDetails[i].beforeZip = deflater.getBytesRead()));
        sm.statDetails[i].zipTime = System.currentTimeMillis() - startTime;
      } else {
        idxAndLen.add((int) deflater.getBytesWritten());
        idxAndLen.add((int) deflater.getBytesRead());
      }
    } 
  }
  serializedOut.writeInt(nbrEntries);                     // write number of entries
  for (int i = 0; i < idxAndLen.size();) {
    serializedOut.write(idxAndLen.get(i++));
    serializedOut.writeInt(idxAndLen.get(i++));
    serializedOut.writeInt(idxAndLen.get(i++));
  }
  baosZipped.writeTo(serializedOut);                      // write Compressed info
}
 
Example 3
Source File: IOHelper.java    From Launcher with GNU General Public License v3.0 4 votes vote down vote up
public static Deflater newDeflater() {
    Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
    deflater.setStrategy(Deflater.DEFAULT_STRATEGY);
    return deflater;
}
 
Example 4
Source File: PGZIPOutputStream.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
protected Deflater newDeflater() {
    Deflater def = new Deflater(level, true);
    def.setStrategy(strategy);
    return def;
}
 
Example 5
Source File: PGZIPOutputStream.java    From FastAsyncWorldedit with GNU General Public License v3.0 4 votes vote down vote up
@Nonnull
protected Deflater newDeflater() {
    Deflater def = new Deflater(level, true);
    def.setStrategy(strategy);
    return def;
}
 
Example 6
Source File: PGZIPOutputStream.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
protected Deflater newDeflater() {
    Deflater def = new Deflater(level, true);
    def.setStrategy(strategy);
    return def;
}
 
Example 7
Source File: DeflaterTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * java.util.zip.Deflater#setStrategy(int)
 */
public void test_setStrategyI() throws Exception {
    byte[] byteArray = new byte[100];
    InputStream inFile = Support_Resources.getStream("hyts_checkInput.txt");
    inFile.read(byteArray);
    inFile.close();

    for (int i = 0; i < 3; i++) {
        byte outPutBuf[] = new byte[500];
        MyDeflater mdefl = new MyDeflater();

        if (i == 0) {
            mdefl.setStrategy(mdefl.getDefStrategy());
        } else if (i == 1) {
            mdefl.setStrategy(mdefl.getHuffman());
        } else {
            mdefl.setStrategy(mdefl.getFiltered());
        }

        mdefl.setInput(byteArray);
        while (!mdefl.needsInput()) {
            mdefl.deflate(outPutBuf);
        }
        mdefl.finish();
        while (!mdefl.finished()) {
            mdefl.deflate(outPutBuf);
        }

        if (i == 0) {
            // System.out.println(mdefl.getTotalOut());
            // ran JDK and found that getTotalOut() = 86 for this particular
            // file
            assertEquals("getTotalOut() for the default strategy did not correspond with JDK",
                    86, mdefl.getTotalOut());
        } else if (i == 1) {
            // System.out.println(mdefl.getTotalOut());
            // ran JDK and found that getTotalOut() = 100 for this
            // particular file
            assertEquals("getTotalOut() for the Huffman strategy did not correspond with JDK",
                    100, mdefl.getTotalOut());
        } else {
            // System.out.println(mdefl.getTotalOut());
            // ran JDK and found that totalOut = 93 for this particular file
            assertEquals("Total Out for the Filtered strategy did not correspond with JDK",
                    93, mdefl.getTotalOut());
        }
        mdefl.end();
    }

    // Attempting to setStrategy to an invalid value
    Deflater defl = new Deflater();
    try {
        defl.setStrategy(-412);
        fail("IllegalArgumentException not thrown when setting strategy to an invalid value.");
    } catch (IllegalArgumentException e) {
    }
    defl.end();
}
 
Example 8
Source File: BinaryCasSerDes6.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/**
 * Method:
 *   write with deflation into a single byte array stream
 *     skip if not worth deflating
 *     skip the Slot_Control stream
 *     record in the Slot_Control stream, for each deflated stream:
 *       the Slot index
 *       the number of compressed bytes
 *       the number of uncompressed bytes
 *   add to header:  
 *     nbr of compressed entries
 *     the Slot_Control stream size
 *     the Slot_Control stream
 *     all the zipped streams
 *
 * @throws IOException passthru
 */
private void collectAndZip() throws IOException {
  ByteArrayOutputStream baosZipped = new ByteArrayOutputStream(4096);
  Deflater deflater = new Deflater(compressLevel.lvl, true);
  deflater.setStrategy(compressStrategy.strat);
  int nbrEntries = 0;
  
  List<Integer> idxAndLen = new ArrayList<>();

  for (int i = 0; i < baosZipSources.length; i++) {
    ByteArrayOutputStream baos = baosZipSources[i];
    if (baos != null) {
      nbrEntries ++;
      dosZipSources[i].close();
      long startTime = System.currentTimeMillis();
      int zipBufSize = Math.max(1024, baos.size() / 100);
      deflater.reset();
      DeflaterOutputStream cds = new DeflaterOutputStream(baosZipped, deflater, zipBufSize);       
      baos.writeTo(cds);
      cds.close();
      idxAndLen.add(i);
      if (doMeasurements) {
        idxAndLen.add((int)(sm.statDetails[i].afterZip = deflater.getBytesWritten()));            
        idxAndLen.add((int)(sm.statDetails[i].beforeZip = deflater.getBytesRead()));
        sm.statDetails[i].zipTime = System.currentTimeMillis() - startTime;
      } else {
        idxAndLen.add((int)deflater.getBytesWritten());            
        idxAndLen.add((int)deflater.getBytesRead());
      }
    } 
  } // end of for loop
  
  /** 
   * format of serialized data, as DataOutputStream:
   *   - number of written kinds of sources (may be less than baosZipSources.length if some are not used)
   *   - Triples, for each non-null baosZipSources:
   *     - the index of the baosZipSource
   *     - the number of bytes in the deflated stream for this source
   *     - the number of uncompressed bytes for this stream
   *   - the compressed bytes for all the non-null baosZipSources streams, in order   
   */
  serializedOut.writeInt(nbrEntries);                     // write number of entries
  for (int i = 0; i < idxAndLen.size();) {
    serializedOut.write(idxAndLen.get(i++));
    serializedOut.writeInt(idxAndLen.get(i++));
    serializedOut.writeInt(idxAndLen.get(i++));
  }
  baosZipped.writeTo(serializedOut);                      // write Compressed info
}
 
Example 9
Source File: TestHuffmanEncoder.java    From database with GNU General Public License v2.0 3 votes vote down vote up
public HuffmanEncoder() {

            deflater = new Deflater(Deflater.BEST_SPEED);
            
            deflater.setStrategy(Deflater.HUFFMAN_ONLY);

        }