Example usage for java.util.zip Deflater BEST_SPEED

List of usage examples for java.util.zip Deflater BEST_SPEED

Introduction

In this page you can find the example usage for java.util.zip Deflater BEST_SPEED.

Prototype

int BEST_SPEED

To view the source code for java.util.zip Deflater BEST_SPEED.

Click Source Link

Document

Compression level for fastest compression.

Usage

From source file:Main.java

public static byte[] compressZLIB(byte[] input) throws IOException {
    // Create the compressor with highest level of compression
    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_SPEED);
    // Give the compressor the data to compress
    compressor.setInput(input);/*  w w  w .j  a va  2s  . c  o m*/
    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
    byte[] compressedData = bos.toByteArray();
    return compressedData;
}

From source file:Main.java

private static byte[] compressBytesInflateDeflate(byte[] inBytes) {
    Deflater deflater = new Deflater(Deflater.BEST_SPEED);
    deflater.setInput(inBytes);/*from   w w  w . j ava2  s . co m*/
    ByteArrayOutputStream bos = new ByteArrayOutputStream(inBytes.length);
    deflater.finish();
    byte[] buffer = new byte[1024 * 8];
    while (!deflater.finished()) {
        int count = deflater.deflate(buffer);
        bos.write(buffer, 0, count);
    }
    byte[] output = bos.toByteArray();
    return output;
}

From source file:io.undertow.server.handlers.encoding.GzipContentEncodingSimpleObjectPoolTestCase.java

@BeforeClass
public static void setup() {
    final ObjectPool<Deflater> deflaterPool = DeflatingStreamSinkConduit.simpleDeflaterPool(50,
            Deflater.BEST_SPEED);
    final EncodingHandler handler = new EncodingHandler(new ContentEncodingRepository().addEncodingHandler(
            "gzip", new GzipEncodingProvider(deflaterPool), 50, Predicates.parse("max-content-size[5]")))
                    .setNext(new HttpHandler() {
                        @Override
                        public void handleRequest(final HttpServerExchange exchange) throws Exception {
                            exchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, message.length() + "");
                            exchange.getResponseSender().send(message, IoCallback.END_EXCHANGE);
                        }//from  www  . j a v  a  2  s  .  co m
                    });

    DefaultServer.setRootHandler(handler);
}

From source file:com.thoughtworks.go.util.ZipUtil.java

public void zipFolderContents(File destDir, File destZipFile) throws IOException {
    zipFolderContents(destDir, destZipFile, Deflater.BEST_SPEED);
}

From source file:org.apache.tez.common.TezUtils.java

/**
 * Convert a Configuration to compressed ByteString using Protocol buffer
 *
 * @param conf/*from w ww.  j a v a2s  .co m*/
 *          : Configuration to be converted
 * @return PB ByteString (compressed)
 * @throws java.io.IOException
 */
public static ByteString createByteStringFromConf(Configuration conf) throws IOException {
    Preconditions.checkNotNull(conf, "Configuration must be specified");
    ByteString.Output os = ByteString.newOutput();
    DeflaterOutputStream compressOs = new DeflaterOutputStream(os, new Deflater(Deflater.BEST_SPEED));
    try {
        writeConfInPB(compressOs, conf);
    } finally {
        if (compressOs != null) {
            compressOs.close();
        }
    }
    return os.toByteString();
}

From source file:net.yacy.cora.protocol.http.GzipCompressingEntity.java

@Override
public void writeTo(final OutputStream outstream) throws IOException {
    if (outstream == null) {
        throw new IllegalArgumentException("Output stream may not be null");
    }//w  w w.j a va 2  s  .c o m
    GZIPOutputStream gzip = new GZIPOutputStream(outstream, 65536) {
        {
            def.setLevel(Deflater.BEST_SPEED);
        }
    };
    wrappedEntity.writeTo(gzip);
    gzip.finish();
}

From source file:org.apache.nifi.processors.standard.util.PGPUtil.java

public static void encrypt(InputStream in, OutputStream out, String algorithm, String provider, int cipher,
        String filename, PGPKeyEncryptionMethodGenerator encryptionMethodGenerator)
        throws IOException, PGPException {
    if (StringUtils.isEmpty(algorithm)) {
        throw new IllegalArgumentException("The algorithm must be specified");
    }//from   ww  w .j ava 2  s  . c o m
    final boolean isArmored = EncryptContent.isPGPArmoredAlgorithm(algorithm);
    OutputStream output = out;
    if (isArmored) {
        output = new ArmoredOutputStream(out);
    }

    // Default value, do not allow null encryption
    if (cipher == PGPEncryptedData.NULL) {
        logger.warn("Null encryption not allowed; defaulting to AES-128");
        cipher = PGPEncryptedData.AES_128;
    }

    try {
        // TODO: Can probably hard-code provider to BC and remove one method parameter
        PGPEncryptedDataGenerator encryptedDataGenerator = new PGPEncryptedDataGenerator(
                new JcePGPDataEncryptorBuilder(cipher).setWithIntegrityPacket(true)
                        .setSecureRandom(new SecureRandom()).setProvider(provider));

        encryptedDataGenerator.addMethod(encryptionMethodGenerator);

        try (OutputStream encryptedOut = encryptedDataGenerator.open(output, new byte[BUFFER_SIZE])) {
            PGPCompressedDataGenerator compressedDataGenerator = new PGPCompressedDataGenerator(
                    PGPCompressedData.ZIP, Deflater.BEST_SPEED);
            try (OutputStream compressedOut = compressedDataGenerator.open(encryptedOut,
                    new byte[BUFFER_SIZE])) {
                PGPLiteralDataGenerator literalDataGenerator = new PGPLiteralDataGenerator();
                try (OutputStream literalOut = literalDataGenerator.open(compressedOut, PGPLiteralData.BINARY,
                        filename, new Date(), new byte[BUFFER_SIZE])) {

                    final byte[] buffer = new byte[BLOCK_SIZE];
                    int len;
                    while ((len = in.read(buffer)) > -1) {
                        literalOut.write(buffer, 0, len);
                    }
                }
            }
        }
    } finally {
        if (isArmored) {
            output.close();
        }
    }
}

From source file:de.hofuniversity.iisys.neo4j.websock.query.encoding.safe.TSafeDeflateJsonQueryHandler.java

public TSafeDeflateJsonQueryHandler(final String compression) {
    fLogger = Logger.getLogger(this.getClass().getName());
    fDebug = (fLogger.getLevel() == Level.FINEST);

    int tmpComp = DEFAULT_COMPRESSION_LEVEL;

    if (WebsockConstants.FASTEST_COMPRESSION.equals(compression)) {
        tmpComp = Deflater.BEST_SPEED;
    } else if (WebsockConstants.BEST_COMPRESSION.equals(compression)) {
        tmpComp = Deflater.BEST_COMPRESSION;
    } else {//from   w  w  w .j a v a2s .  c o  m
        fLogger.log(Level.WARNING, "unknown compression level '" + compression + "'; using default.");
    }

    fCompression = tmpComp;
}

From source file:org.fastcatsearch.ir.document.DocumentWriter.java

public DocumentWriter(SchemaSetting schemaSetting, File dir, IndexConfig indexConfig)
        throws IOException, IRException {

    compressor = new Deflater(Deflater.BEST_SPEED);
    fields = schemaSetting.getFieldSettingList();

    docOutput = new BufferedFileOutput(dir, IndexFileNames.docStored);
    positionOutput = new BufferedFileOutput(dir, IndexFileNames.docPosition);

    fbaos = new BytesDataOutput(3 * 1024 * 1024); // 3Mb .
    workingBuffer = new byte[1024];
    docOutput.writeInt(0); // document count

    inflaterOutput = new ByteRefArrayOutputStream(INFLATE_BUFFER_INIT_SIZE);

}

From source file:de.hofuniversity.iisys.neo4j.websock.query.encoding.unsafe.DeflateJsonQueryHandler.java

public DeflateJsonQueryHandler(final String compression) {
    fLogger = Logger.getLogger(this.getClass().getName());
    fDebug = (fLogger.getLevel() == Level.FINEST);

    fInflater = new Inflater(true);

    int tmpComp = DEFAULT_COMPRESSION_LEVEL;

    if (WebsockConstants.FASTEST_COMPRESSION.equals(compression)) {
        tmpComp = Deflater.BEST_SPEED;
    } else if (WebsockConstants.BEST_COMPRESSION.equals(compression)) {
        tmpComp = Deflater.BEST_COMPRESSION;
    } else {/*w  w w  .j a  v a 2  s  . com*/
        fLogger.log(Level.WARNING, "unknown compression level '" + compression + "'; using default.");
    }

    fCompression = tmpComp;
}