Example usage for java.util.zip Deflater Deflater

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

Introduction

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

Prototype

public Deflater(int level) 

Source Link

Document

Creates a new compressor using the specified compression level.

Usage

From source file:spade.storage.CompressedTextFile.java

@Override
public boolean initialize(String arguments) {
    clock = System.currentTimeMillis();
    scaffoldInMemory = new HashMap<Integer, Pair<SortedSet<Integer>, SortedSet<Integer>>>();
    edgesInMemory = 0;//from  w  w w  .j  a v  a  2  s . c o m
    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;
    }
}

From source file:NCDSearch.DistributedNCDSearch.java

public static float NCD(byte[] file, byte[] target, int compression) {

    Deflater compressor = new Deflater(compression);

    //This is where we dump our compressed bytes.  All we need is the size of this thing.  
    //TODO: In theory the compressed bytes could exceed the length of the target files...
    byte[] outputtrash = new byte[file.length + target.length];

    int bothcompressedsize;
    int filecompressedsize;
    int targetcompressedsize;

    //puts the target file and the searched file together.
    byte[] both = new byte[file.length + target.length];
    for (int i = 0; i < file.length; i++) {
        both[i] = file[i];// w  ww.  j  a va  2 s  .  co  m
    }
    for (int i = 0; i < target.length; i++) {
        both[i + file.length] = target[i];
    }

    compressor.setInput(file);
    compressor.finish();
    filecompressedsize = compressor.deflate(outputtrash);
    compressor.reset();
    compressor.setInput(target);
    compressor.finish();
    targetcompressedsize = compressor.deflate(outputtrash);
    compressor.reset();
    compressor.setInput(both);
    compressor.finish();
    bothcompressedsize = compressor.deflate(outputtrash);
    compressor.reset();

    return (float) (bothcompressedsize - filecompressedsize) / (float) targetcompressedsize;
}

From source file:com.uber.hoodie.common.TestRawTripPayload.java

private byte[] compressData(String jsonData) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DeflaterOutputStream dos = new DeflaterOutputStream(baos, new Deflater(Deflater.BEST_COMPRESSION), true);
    try {/*w ww  .  j  a va 2  s.c om*/
        dos.write(jsonData.getBytes());
    } finally {
        dos.flush();
        dos.close();
    }
    return baos.toByteArray();
}

From source file:org.dragonet.net.ClientChunkManager.java

/**
 * Send a single chunk to the client/*from  ww  w  . j  a  va  2s .c  om*/
 *
 * @param chunkX The chunk X coordinate
 * @param chunkZ The chunk Z coordinate
 */
private synchronized void sendChunk(int chunkX, int chunkZ) {
    try {
        GlowChunkSnapshot chunk = this.getSession().getPlayer().getWorld().getChunkAt(chunkX, chunkZ)
                .getChunkSnapshot();
        ByteArrayOutputStream totalData = new ByteArrayOutputStream();
        PEBinaryWriter writer = new PEBinaryWriter(totalData);
        if (writer.getEndianness() == PEBinaryUtils.BIG_ENDIAN) {
            writer.switchEndianness();
        }
        writer.writeInt(chunkX);
        writer.writeInt(chunkZ);
        for (int x = 0; x < 16; x++) {
            for (int z = 0; z < 16; z++) {
                for (int y = 0; y < 128; y++) {
                    writer.writeByte((byte) (this.getSession().getTranslator()
                            .translateBlockToPE(chunk.getBlockTypeId(x, y, z)) & 0xFF));
                }
            }
        }
        writer.write(new byte[16384]);
        for (int i = 0; i < 16384; i++) {
            writer.writeByte((byte) 0xF0);
        }
        for (int i = 0; i < 16384; i++) {
            writer.writeByte((byte) 0x11);
        }
        for (int i = 0; i < 256; i++) {
            writer.writeByte((byte) 0x00);
        }
        for (int i = 0; i < 256; i++) {
            writer.writeByte((byte) 0x00);
            writer.writeByte((byte) 0x85);
            writer.writeByte((byte) 0xB2);
            writer.writeByte((byte) 0x4A);
        }
        Deflater deflater = new Deflater(2);
        deflater.reset();
        deflater.setInput(totalData.toByteArray());
        deflater.finish();
        byte[] bufferDeflate = new byte[65536];
        int deflatedSize = deflater.deflate(bufferDeflate);
        FullChunkPacket packet = new FullChunkPacket();
        packet.compressedData = ArrayUtils.subarray(bufferDeflate, 0, deflatedSize);
        this.getSession().send(packet);
    } catch (IOException e) {
    }
}

From source file:org.dragonet.proxy.protocol.packet.LoginPacket.java

@Override
public void encode() {
    try {/* w  w  w.  j  a  v  a 2 s. c o m*/
        setChannel(NetworkChannel.CHANNEL_PRIORITY);

        // Basic info
        JSONObject jsonBasic = new JSONObject();
        String b64Signature;
        String b64User;
        {
            JSONObject jsonSignature = new JSONObject();
            jsonSignature.put("alg", "ES384");
            jsonSignature.put("x5u",
                    "MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAE+SKWsy2I5ecBTPZEfNgwgvuin2/iqqi3haMxXPpknsVKINpp5E7jlygXqBmSJad5VG+1uq75V9irGEtmpUINP5xhYiOrlEma+2aBJIUe17UT/r50yTnDhDrPoOY/eAHL");
            b64Signature = Base64.getEncoder().encodeToString(jsonSignature.toString().getBytes("UTF-8"));
        }
        {
            JSONObject jsonUser = new JSONObject();
            jsonUser.put("exp", System.currentTimeMillis() / 1000L + (60 * 10));
            JSONObject jsonUserInfo = new JSONObject();
            jsonUserInfo.put("displayName", username);
            jsonUserInfo.put("identity", clientUuid.toString());
            jsonUser.put("extraData", jsonUserInfo);
            jsonUser.put("identityPublicKey", ""); // publicKey
            jsonUser.put("nbf", System.currentTimeMillis() / 1000L);
            b64User = Base64.getEncoder().encodeToString(jsonUser.toString().getBytes("UTF-8"));
            System.out.println(jsonUser.toString());
        }
        String b64Basic = b64Signature + "." + b64User;

        // Meta info
        JSONObject jsonMeta = new JSONObject();
        String strMeta;
        {
            jsonMeta.put("ClientRandomId", clientID);
            jsonMeta.put("ServerAddress", serverAddress);
            jsonMeta.put("SkinId", skin.getModel());
            jsonMeta.put("SkinData", Base64.getEncoder().encodeToString(skin.getData()));
            strMeta = Base64.getEncoder().encodeToString(jsonMeta.toString().getBytes("UTF-8"));
        }
        String b64Meta = b64Signature + "." + strMeta;

        byte[] chainData;
        {
            byte[] dataBasic = b64Basic.getBytes("UTF-8");
            byte[] dataMeta = b64Meta.getBytes("UTF-8");
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            PEBinaryWriter writer = new PEBinaryWriter(bos);
            writer.switchEndianness();
            writer.writeInt(dataBasic.length);
            writer.switchEndianness();
            writer.write(dataBasic);
            writer.switchEndianness();
            writer.writeInt(dataMeta.length);
            writer.switchEndianness();
            writer.write(dataMeta);

            chainData = bos.toByteArray();
        }

        JSONObject jsonChain = new JSONObject();
        jsonChain.put("chain", chainData);
        String strChain = jsonChain.toString();
        byte[] b64Chain = Base64.getEncoder().encode(strChain.getBytes("UTF-8"));
        Deflater deflater = new Deflater(7);
        deflater.setInput(b64Chain);
        deflater.finish();
        byte[] buff = new byte[40960];
        int deflated = deflater.deflate(buff);
        buff = ArrayUtils.subarray(buff, 0, deflated);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        PEBinaryWriter writer = new PEBinaryWriter(bos);
        writer.writeByte((byte) (this.pid() & 0xFF));
        writer.writeInt(protocol);
        writer.writeInt(buff.length);
        writer.write(buff);
        this.setData(bos.toByteArray());
    } catch (IOException | JSONException e) {
    }
}

From source file:org.jbpm.bpel.persistence.db.type.ElementType.java

public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
    // easy way out: null value
    if (value == null) {
        st.setNull(index, SQL_TYPES[0]);
        if (log.isTraceEnabled())
            log.trace("binding null to parameter: " + index);
    } else {/*from w ww.j a  v a  2 s  . c  o m*/
        Element element = (Element) value;
        try {
            // create identity transformer
            Transformer idTransformer = XmlUtil.getTransformerFactory().newTransformer();

            // allocate memory result stream
            ByteArrayOutputStream byteStream = new ByteArrayOutputStream();

            // deflate if requested
            Integer deflateLevel = getXmlDeflateLevel();
            if (deflateLevel != null) {
                // introduce deflater stream
                Deflater deflater = new Deflater(deflateLevel.intValue());
                OutputStream deflaterStream = new DeflaterOutputStream(byteStream, deflater);
                // write element to stream
                idTransformer.transform(new DOMSource(element), new StreamResult(deflaterStream));
                // release resources
                try {
                    deflaterStream.close();
                } catch (IOException e) {
                    // should not happen
                    throw new AssertionError(e);
                }
                deflater.end();
            } else {
                // write element to stream
                idTransformer.transform(new DOMSource(element), new StreamResult(byteStream));
                // noop
                // byteStream.close();
            }

            // extract contents of result stream
            st.setBytes(index, byteStream.toByteArray());
            if (log.isTraceEnabled())
                log.trace("binding '" + byteStream + "' to parameter: " + index);
        } catch (TransformerException e) {
            throw new HibernateException("could not transform to xml stream: " + element, e);
        }
    }
}

From source file:ubicrypt.core.provider.RemoteRepository.java

private Observable<Boolean> saveSerial(final FileProvenience fp) {
    //acquire permission
    final AtomicReference<FileEvent.Type> fileEventType = new AtomicReference<>();
    return create(acquirer).flatMap(releaser -> {
        releaserRef.set(releaser);/*  w ww. j  a v  a2s  .  c  om*/
        final RemoteConfig remoteConfig = releaser.getRemoteConfig();
        UbiFile<UbiFile> file = fp.getFile();
        Optional<RemoteFile> rfile = remoteConfig.getRemoteFiles().stream().filter(file1 -> file1.equals(file))
                .findFirst();
        if (!rfile.isPresent()) {
            if (!Utils.ignoredFiles.test(file)) {
                return just(false);
            }
            //create new one remotely
            RemoteFile rf = RemoteFile.createFrom(file);
            final byte[] key = AESGCM.rndKey();
            rf.setKey(new Key(key));
            fileEventType.set(FileEvent.Type.created);
            return fp.getOrigin().get(file)
                    .flatMap(is -> provider
                            .post(AESGCM.encryptIs(key,
                                    new DeflaterInputStream(monitor(fp, is), new Deflater(BEST_COMPRESSION))))
                            .map(name -> {
                                log.info("created file:{}, to provider:{}", rf.getPath(), provider);
                                //add name and add to config
                                rf.setRemoteName(name);
                                remoteConfig.getRemoteFiles().add(rf);
                                return true;
                            }))
                    .defaultIfEmpty(false).filter(BooleanUtils::isTrue)
                    .doOnCompleted(fileEvents(fp, fileEventType.get()));
        }
        //update already present file
        if (file.compare(rfile.get()) == VClock.Comparison.newer) {
            //coming file is new version
            log.debug("file:{} newer than:{} on provider:{}", file.getPath(), rfile.get(), provider);
            rfile.get().copyFrom(file);
            if (rfile.get().isDeleted() || rfile.get().isRemoved()) {
                //delete remotely
                fileEventType.set(rfile.get().isDeleted() ? FileEvent.Type.deleted : FileEvent.Type.removed);
                return provider.delete(rfile.get().getName())
                        .doOnNext(saved -> log.info("deleted:{} file:{}, to provider:{}", saved,
                                rfile.get().getPath(), provider))
                        .filter(BooleanUtils::isTrue).doOnCompleted(fileEvents(fp, fileEventType.get()));
            }
            //update remotely
            fileEventType.set(FileEvent.Type.updated);
            return fp.getOrigin().get(file).flatMap(is -> {
                //renew encryption key
                rfile.get().setKey(new Key(AESGCM.rndKey(), UbiFile.KeyType.aes));
                return provider
                        .put(rfile.get().getName(),
                                AESGCM.encryptIs(rfile.get().getKey().getBytes(),
                                        new DeflaterInputStream(monitor(fp, is),
                                                new Deflater(BEST_COMPRESSION))))
                        .doOnNext(saved -> log.info("updated:{} file:{}, to provider:{}", saved,
                                rfile.get().getPath(), provider))
                        .filter(BooleanUtils::isTrue).doOnCompleted(fileEvents(fp, fileEventType.get()));
            });
        }
        log.debug("no update file:{} for provider:{}", file.getPath(), provider);
        return Observable.just(false);
    }).doOnError(releaserRef.get() != null ? err -> releaserRef.get().getReleaser().call() : Actions.empty())
            .doOnError(err -> progressEvents.onNext(new ProgressFile(fp, this, false, true)))
            .onErrorReturn(err -> {
                log.error(err.getMessage(), err);
                return false;
            })
            .doOnCompleted(releaserRef.get() != null ? releaserRef.get().getReleaser()::call : Actions.empty());
}

From source file:org.apache.flex.compiler.internal.embedding.transcoders.JPEGTranscoder.java

public static byte[] deflate(byte[] buf) throws IOException {
    Deflater deflater = new Deflater(Deflater.BEST_SPEED);
    DAByteArrayOutputStream out = new DAByteArrayOutputStream();
    DeflaterOutputStream deflaterStream = new DeflaterOutputStream(out, deflater);
    try {// w w w.  jav a 2  s  . c  o  m
        deflaterStream.write(buf, 0, buf.length);
        deflaterStream.finish();
        deflater.end();
    } finally {
        IOUtils.closeQuietly(deflaterStream);
    }
    return out.getDirectByteArray();
}

From source file:org.apache.qpid.multiconsumer.AMQTest.java

private String compressString(String string) throws Exception {
    long start = System.currentTimeMillis();
    byte[] input = string.getBytes();
    Deflater compressor = new Deflater(Deflater.BEST_COMPRESSION);
    compressor.setInput(input);//  w  w w.j  a va2  s.  c o  m
    compressor.finish();

    // Get byte array from output of compressor
    ByteArrayOutputStream baos = new ByteArrayOutputStream(input.length);
    byte[] buf = new byte[1024];
    while (!compressor.finished()) {
        int cnt = compressor.deflate(buf);
        baos.write(buf, 0, cnt);
    }
    baos.close();
    byte[] output = baos.toByteArray();

    // Convert byte array into String
    byte[] base64 = Base64.encodeBase64(output);
    String sComp = new String(base64, UTF8);

    long diff = System.currentTimeMillis() - start;
    System.out.println(
            "Compressed text from " + input.length + " to " + sComp.getBytes().length + " in " + diff + " ms");
    System.out.println("Compressed text = '" + sComp + "'");

    return sComp;
}

From source file:com.igormaznitsa.jcp.expression.functions.FunctionBINFILE.java

@Nonnull
private static byte[] deflate(@Nonnull final byte[] data) throws IOException {
    final Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
    deflater.setInput(data);/* w ww. j  a v a 2s  .  c om*/

    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);

    deflater.finish();
    final byte[] buffer = new byte[1024];
    while (!deflater.finished()) {
        final int count = deflater.deflate(buffer);
        outputStream.write(buffer, 0, count);
    }
    outputStream.close();
    final byte[] output = outputStream.toByteArray();

    deflater.end();

    return output;
}