Search in sources :

Example 31 with Deflater

use of java.util.zip.Deflater in project inbot-utils by Inbot.

the class CompressionUtils method compress.

public static byte[] compress(byte[] uncompressed) {
    Deflater d = new Deflater();
    d.setLevel(Deflater.BEST_COMPRESSION);
    d.setInput(uncompressed);
    d.finish();
    ByteArrayOutputStream bos = new ByteArrayOutputStream(uncompressed.length);
    // Compress the data
    byte[] buf = new byte[1024];
    while (!d.finished()) {
        int count = d.deflate(buf);
        bos.write(buf, 0, count);
    }
    try {
        bos.close();
    } catch (IOException e) {
        throw new IllegalStateException("could not compress");
    }
    return bos.toByteArray();
}
Also used : Deflater(java.util.zip.Deflater) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 32 with Deflater

use of java.util.zip.Deflater in project hive by apache.

the class InputJobInfo method writeObject.

/**
 * Serialize this object, compressing the partitions which can exceed the
 * allowed jobConf size.
 * @see <a href="https://issues.apache.org/jira/browse/HCATALOG-453">HCATALOG-453</a>
 */
private void writeObject(ObjectOutputStream oos) throws IOException {
    oos.defaultWriteObject();
    Deflater def = new Deflater(Deflater.BEST_COMPRESSION);
    ObjectOutputStream partInfoWriter = new ObjectOutputStream(new DeflaterOutputStream(oos, def));
    partInfoWriter.writeObject(partitions);
    partInfoWriter.close();
}
Also used : Deflater(java.util.zip.Deflater) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) ObjectOutputStream(java.io.ObjectOutputStream)

Example 33 with Deflater

use of java.util.zip.Deflater in project cxf by apache.

the class CompressionUtils method deflate.

public static byte[] deflate(byte[] tokenBytes, int level, boolean nowrap) {
    Deflater compresser = new Deflater(level, nowrap);
    compresser.setInput(tokenBytes);
    compresser.finish();
    byte[] output = new byte[tokenBytes.length * 2];
    int compressedDataLength = compresser.deflate(output);
    byte[] result = new byte[compressedDataLength];
    System.arraycopy(output, 0, result, 0, compressedDataLength);
    return result;
}
Also used : Deflater(java.util.zip.Deflater)

Example 34 with Deflater

use of java.util.zip.Deflater in project FlareBot by FlareBot.

the class ApiRequester method getRequest.

private static Request getRequest(ApiRoute route, String bodyContent, MediaType type, boolean compressed) {
    Request.Builder request = new Request.Builder().url(route.getFullUrl());
    request.addHeader("Authorization", FlareBot.instance().getApiKey());
    request.addHeader("User-Agent", "Mozilla/5.0 FlareBot");
    RequestBody body = RequestBody.create(JSON, bodyContent);
    if (compressed && !bodyContent.isEmpty()) {
        request.addHeader("Content-Encoding", "gzip");
        logger.debug("Starting compression: " + System.currentTimeMillis());
        byte[] output = new byte[100];
        Deflater deflater = new Deflater();
        deflater.setInput(bodyContent.getBytes());
        deflater.finish();
        deflater.deflate(output);
        deflater.end();
        logger.debug("Finished compression: " + System.currentTimeMillis());
        body = RequestBody.create(type, output);
    }
    logger.debug("Sending " + route.getRoute() + ", Type: " + type.toString() + ", Compressed: " + compressed);
    switch(route.getMethod()) {
        case GET:
            request = request.get();
            break;
        case PATCH:
            request = request.patch(body);
            break;
        case POST:
            request = request.post(body);
            break;
        case PUT:
            request = request.put(body);
            break;
        case DELETE:
            request = request.delete(body);
            break;
        default:
            throw new IllegalArgumentException("The route " + route.name() + " is using an unsupported method! Method: " + route.getMethod().name());
    }
    return request.build();
}
Also used : Deflater(java.util.zip.Deflater) Request(okhttp3.Request) RequestBody(okhttp3.RequestBody)

Example 35 with Deflater

use of java.util.zip.Deflater in project bnd by bndtools.

the class CAFS method write.

/**
	 * Store an input stream in the CAFS while calculating and returning the
	 * SHA-1 code.
	 * 
	 * @param in The input stream to store.
	 * @return The SHA-1 code.
	 * @throws Exception if anything goes wrong
	 */
public SHA1 write(InputStream in) throws Exception {
    Deflater deflater = new Deflater();
    MessageDigest md = MessageDigest.getInstance(ALGORITHM);
    DigestInputStream din = new DigestInputStream(in, md);
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    DeflaterOutputStream dout = new DeflaterOutputStream(bout, deflater);
    copy(din, dout);
    synchronized (store) {
        // First check if it already exists
        SHA1 sha1 = new SHA1(md.digest());
        long search = index.search(sha1.digest());
        if (search > 0)
            return sha1;
        byte[] compressed = bout.toByteArray();
        // we need to append this file to our store,
        // which requires a lock. However, we are in a race
        // so others can get the lock between us getting
        // the length and someone else getting the lock.
        // So we must verify after we get the lock that the
        // length was unchanged.
        FileLock lock = null;
        try {
            long insertPoint;
            int recordLength = compressed.length + HEADERLENGTH;
            while (true) {
                insertPoint = store.length();
                lock = channel.lock(insertPoint, recordLength, false);
                if (store.length() == insertPoint)
                    break;
                // We got the wrong lock, someone else
                // got in between reading the length
                // and locking
                lock.release();
            }
            int totalLength = deflater.getTotalIn();
            store.seek(insertPoint);
            update(sha1.digest(), compressed, totalLength);
            index.insert(sha1.digest(), insertPoint);
            return sha1;
        } finally {
            if (lock != null)
                lock.release();
        }
    }
}
Also used : SHA1(aQute.libg.cryptography.SHA1) Deflater(java.util.zip.Deflater) DigestInputStream(java.security.DigestInputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) FileLock(java.nio.channels.FileLock) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MessageDigest(java.security.MessageDigest)

Aggregations

Deflater (java.util.zip.Deflater)85 ByteArrayOutputStream (java.io.ByteArrayOutputStream)33 DeflaterOutputStream (java.util.zip.DeflaterOutputStream)25 IOException (java.io.IOException)18 Test (org.junit.Test)13 Inflater (java.util.zip.Inflater)9 OutputStream (java.io.OutputStream)7 InflaterInputStream (java.util.zip.InflaterInputStream)7 InputStream (java.io.InputStream)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 EOFException (java.io.EOFException)3 FileOutputStream (java.io.FileOutputStream)3 Field (java.lang.reflect.Field)3 CRC32 (java.util.zip.CRC32)3 BufferedOutputStream (java.io.BufferedOutputStream)2 CharArrayReader (java.io.CharArrayReader)2 DataOutputStream (java.io.DataOutputStream)2 FileInputStream (java.io.FileInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 Reader (java.io.Reader)2