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

The following examples show how to use java.util.zip.Deflater#setInput() . 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: Zlib.java    From Nemisys with GNU General Public License v3.0 6 votes vote down vote up
public static byte[] deflate(byte[] data, int level) throws Exception {
    Deflater deflater = new Deflater(level);
    deflater.reset();
    deflater.setInput(data);
    deflater.finish();
    ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
    byte[] buf = new byte[1024];
    try {
        while (!deflater.finished()) {
            int i = deflater.deflate(buf);
            bos.write(buf, 0, i);
        }
    } finally {
        deflater.end();
    }
    return bos.toByteArray();
}
 
Example 2
Source File: CassandraInputData.java    From learning-hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * Compress a CQL query
 * 
 * @param queryStr the CQL query
 * @param compression compression option (GZIP is the only option - so far)
 * @return an array of bytes containing the compressed query
 */
public static byte[] compressQuery(String queryStr, Compression compression) {
  byte[] data = queryStr.getBytes(Charset
      .forName(CassandraColumnMetaData.UTF8));

  Deflater compressor = new Deflater();
  compressor.setInput(data);
  compressor.finish();

  ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
  byte[] buffer = new byte[1024];

  while (!compressor.finished()) {
    int size = compressor.deflate(buffer);
    byteArray.write(buffer, 0, size);
  }

  return byteArray.toByteArray();
}
 
Example 3
Source File: CompressUtil.java    From krpc with MIT License 6 votes vote down vote up
public static byte[] compress(byte input[]) {
	ByteArrayOutputStream bos = new ByteArrayOutputStream();
	Deflater compressor = new Deflater(1);
	try {
		compressor.setInput(input);
		compressor.finish();
		final byte[] buf = new byte[2048];
		while (!compressor.finished()) {
			int count = compressor.deflate(buf);
			bos.write(buf, 0, count);
		}
	} finally {
		compressor.end();
	}
	return bos.toByteArray();
}
 
Example 4
Source File: DeflaterTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * @throws DataFormatException
 * @throws UnsupportedEncodingException
 * java.util.zip.Deflater#getBytesRead()
 */
public void test_getBytesWritten() throws DataFormatException,
        UnsupportedEncodingException {
    // Regression test for HARMONY-158
    Deflater def = new Deflater();
    assertEquals(0, def.getTotalIn());
    assertEquals(0, def.getTotalOut());
    assertEquals(0, def.getBytesWritten());
    // Encode a String into bytes
    String inputString = "blahblahblah??";
    byte[] input = inputString.getBytes("UTF-8");

    // Compress the bytes
    byte[] output = new byte[100];
    def.setInput(input);
    def.finish();
    int compressedDataLength = def.deflate(output);
    assertEquals(14, def.getTotalIn());
    assertEquals(compressedDataLength, def.getTotalOut());
    assertEquals(compressedDataLength, def.getBytesWritten());
    def.end();
}
 
Example 5
Source File: JoH.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
public static String compressString(String source) {
    try {

        Deflater deflater = new Deflater();
        deflater.setInput(source.getBytes(Charset.forName("UTF-8")));
        deflater.finish();

        byte[] buf = new byte[source.length() + 256];
        int count = deflater.deflate(buf);
        // check count
        deflater.end();
        return Base64.encodeToString(buf, 0, count, Base64.NO_WRAP);
    } catch (Exception e) {
        return null;
    }
}
 
Example 6
Source File: Slice.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
private void storeData(ByteBuffer data) {
    try {
        final byte[] input = data.array();
        FileOutputStream fos = new FileOutputStream(file);

        final Deflater deflater = new Deflater(Deflater.BEST_SPEED, true);
        deflater.setInput(input, data.arrayOffset(), data.remaining());
        deflater.finish();

        byte[] buf = new byte[1024];
        while (!deflater.finished()) {
            int byteCount = deflater.deflate(buf);
            fos.write(buf, 0, byteCount);
        }
        deflater.end();

        fos.close();
    } catch (Exception e) {
        FileLog.e(e);
    }
}
 
Example 7
Source File: JoH.java    From xDrip with GNU General Public License v3.0 6 votes vote down vote up
public static String compressString(String source) {
    try {

        Deflater deflater = new Deflater();
        deflater.setInput(source.getBytes(Charset.forName("UTF-8")));
        deflater.finish();

        byte[] buf = new byte[source.length() + 256];
        int count = deflater.deflate(buf);
        // check count
        deflater.end();
        return Base64.encodeToString(buf, 0, count, Base64.NO_WRAP);
    } catch (Exception e) {
        return null;
    }
}
 
Example 8
Source File: CassandraOutputData.java    From learning-hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * Compress a CQL query
 * 
 * @param queryStr
 *            the CQL query
 * @param compression
 *            compression option (GZIP is the only option - so far)
 * @return an array of bytes containing the compressed query
 */
public static byte[] compressQuery(String queryStr, Compression compression) {
	byte[] data = queryStr.getBytes(Charset
			.forName(CassandraColumnMetaData.UTF8));

	Deflater compressor = new Deflater();
	compressor.setInput(data);
	compressor.finish();

	ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
	byte[] buffer = new byte[1024];

	while (!compressor.finished()) {
		int size = compressor.deflate(buffer);
		byteArray.write(buffer, 0, size);
	}

	return byteArray.toByteArray();
}
 
Example 9
Source File: PacketCustom.java    From CodeChickenLib with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Compresses the payload ByteBuf after the type byte
 */
private void do_compress() {
    Deflater deflater = new Deflater();
    try {
        readerIndex(1);
        int len = readableBytes();
        deflater.setInput(array(), readerIndex(), len);
        deflater.finish();
        byte[] out = new byte[len];
        int clen = deflater.deflate(out);
        if (clen >= len - 5 || !deflater.finished())//not worth compressing, gets larger
            return;
        clear();
        writeByte(type | 0x80);
        writeVarInt(len);
        writeByteArray(out);
    } catch (Exception e) {
        throw new EncoderException(e);
    } finally {
        readerIndex(0);
        deflater.end();
    }
}
 
Example 10
Source File: Deflate.java    From cstc with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected byte[] perform(byte[] input) throws Exception {
    Deflater deflater = new Deflater();
    deflater.setInput(input);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(input.length);   
    deflater.finish();

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

    outputStream.close();
    return outputStream.toByteArray();
}
 
Example 11
Source File: DeflateCompressor.java    From presto with Apache License 2.0 6 votes vote down vote up
@Override
public int compress(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, int maxOutputLength)
{
    int maxCompressedLength = maxCompressedLength(inputLength);
    if (maxOutputLength < maxCompressedLength) {
        throw new IllegalArgumentException("Output buffer must be at least " + maxCompressedLength + " bytes");
    }

    Deflater deflater = new Deflater(COMPRESSION_LEVEL, true);
    try {
        deflater.setInput(input, inputOffset, inputLength);
        deflater.finish();

        int compressedDataLength = deflater.deflate(output, outputOffset, maxOutputLength, FULL_FLUSH);
        if (!deflater.finished()) {
            throw new IllegalStateException("maxCompressedLength formula is incorrect, because deflate produced more data");
        }
        return compressedDataLength;
    }
    finally {
        deflater.end();
    }
}
 
Example 12
Source File: CompressionManager.java    From minecraft-world-downloader with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Compresses the stream if the size is greater than the compression limit.
 * Source: https://dzone.com/articles/how-compress-and-uncompress
 */
public static byte[] zlibCompress(byte[] input) throws IOException {
    Deflater deflater = new Deflater();
    deflater.setInput(input);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(input.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);
    }
    outputStream.close();
    return outputStream.toByteArray();
}
 
Example 13
Source File: TezUtilsInternal.java    From tez with Apache License 2.0 5 votes vote down vote up
private static byte[] compressBytesInflateDeflate(byte[] inBytes) {
  Deflater deflater = new Deflater(Deflater.BEST_SPEED);
  deflater.setInput(inBytes);
  NonSyncByteArrayOutputStream bos = new NonSyncByteArrayOutputStream(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;
}
 
Example 14
Source File: CompressionUtils.java    From springboot-shiro-cas-mybatis with MIT License 5 votes vote down vote up
/**
 * Deflate the given string via a {@link java.util.zip.Deflater}.
 * The result will be base64 encoded with {@link #UTF8_ENCODING}.
 *
 * @param data the data
 * @return base64 encoded string
 */
public static String deflate(final String data) {
    try {
        final Deflater deflater = new Deflater();
        deflater.setInput(data.getBytes(UTF8_ENCODING));
        deflater.finish();
        final byte[] buffer = new byte[data.length()];
        final int resultSize = deflater.deflate(buffer);
        final byte[] output = new byte[resultSize];
        System.arraycopy(buffer, 0, output, 0, resultSize);
        return encodeBase64(output);
    } catch (final UnsupportedEncodingException e) {
        throw new RuntimeException("Cannot find encoding:" + UTF8_ENCODING, e);
    }
}
 
Example 15
Source File: DeflaterTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * java.util.zip.Deflater#needsInput()
 */
public void test_needsInput() {
    Deflater defl = new Deflater();
    assertTrue(
            "needsInput give the wrong boolean value as a result of no input buffer",
            defl.needsInput());
    byte byteArray[] = { 1, 2, 3 };
    defl.setInput(byteArray);
    assertFalse(
            "needsInput give wrong boolean value as a result of a full input buffer",
            defl.needsInput());
    byte[] outPutBuf = new byte[50];
    while (!defl.needsInput()) {
        defl.deflate(outPutBuf);
    }
    byte emptyByteArray[] = new byte[0];
    defl.setInput(emptyByteArray);
    assertTrue(
            "needsInput give wrong boolean value as a result of an empty input buffer",
            defl.needsInput());
    defl.setInput(byteArray);
    defl.finish();
    while (!defl.finished()) {
        defl.deflate(outPutBuf);
    }
    // needsInput should NOT return true after finish() has been
    // called.
    if (System.getProperty("java.vendor").startsWith("IBM")) {
        assertFalse(
                "needsInput gave wrong boolean value as a result of finish() being called",
                defl.needsInput());
    }
    defl.end();
}
 
Example 16
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 17
Source File: Zlib.java    From krpc with Apache License 2.0 5 votes vote down vote up
public byte[] zip(byte[] input) throws IOException {
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream();) {
        byte[] buff = tlBuff.get();
        Deflater defl = new Deflater();
        defl.setInput(input);
        defl.finish();
        while (!defl.finished()) {
            int len = defl.deflate(buff);
            bos.write(buff, 0, len);
        }
        defl.end();
        bos.flush();
        return bos.toByteArray();
    }
}
 
Example 18
Source File: TezUtils.java    From incubator-tez with Apache License 2.0 5 votes vote down vote up
private static byte[] compressBytesInflateDeflate(byte[] inBytes) {
  Deflater deflater = new Deflater(Deflater.BEST_SPEED);
  deflater.setInput(inBytes);
  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;
}
 
Example 19
Source File: MatrixZoomDataPP.java    From Juicebox with MIT License 5 votes vote down vote up
/**
 * todo should this be synchronized?
 *
 * @param data
 * @param compressor
 * @return
 */
protected byte[] compress(byte[] data, Deflater compressor) {

    // Give the compressor the data to compress
    compressor.reset();
    compressor.setInput(data);
    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(data.length);

    // Compress the data
    byte[] buf = new byte[1024];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }
    try {
        bos.close();
    } catch (IOException e) {
        System.err.println("Error clossing ByteArrayOutputStream");
        e.printStackTrace();
    }

    return bos.toByteArray();
}
 
Example 20
Source File: LoadedSnapshot.java    From visualvm with GNU General Public License v2.0 4 votes vote down vote up
public void save(DataOutputStream dos) throws IOException, OutOfMemoryError {
    // todo [performance] profile memory use during the save operation
    // there is ~80MB bytes used for byte[], for the length of uncompressed data ~20MB
    Properties props = new Properties();
    settings.store(props);

    if (LOGGER.isLoggable(Level.FINEST)) {
        LOGGER.finest("save properties: --------------------------------------------------------------"); // NOI18N
        LOGGER.finest(settings.debug());
        LOGGER.finest("-------------------------------------------------------------------------------"); // NOI18N
    }

    ByteArrayOutputStream baos = new ByteArrayOutputStream(1000000); // ~1MB pre-allocated
    DataOutputStream snapshotDataStream = new DataOutputStream(baos);

    ByteArrayOutputStream baos2 = new ByteArrayOutputStream(10000); // ~10kB pre-allocated
    DataOutputStream settingsDataStream = new DataOutputStream(baos2);

    try {
        snapshot.writeToStream(snapshotDataStream);
        snapshotDataStream.flush();
        props.store(settingsDataStream, ""); //NOI18N
        settingsDataStream.flush();

        byte[] snapshotBytes = baos.toByteArray();
        byte[] compressedBytes = new byte[snapshotBytes.length];

        Deflater d = new Deflater();
        d.setInput(snapshotBytes);
        d.finish();

        int compressedLen = d.deflate(compressedBytes);
        int uncompressedLen = snapshotBytes.length;

        // binary file format:
        // 1. magic number: "nbprofiler"
        // 2. int type
        // 3. int length of snapshot data size
        // 4. snapshot data bytes
        // 5. int length of settings data size
        // 6. settings data bytes (.properties plain text file format)
        // 7. String (UTF) custom comments
        if (LOGGER.isLoggable(Level.FINEST)) {
            LOGGER.finest("save version:" + SNAPSHOT_FILE_VERSION_MAJOR //NOI18N
                          + "." + SNAPSHOT_FILE_VERSION_MINOR); // NOI18N
            LOGGER.finest("save type:" + getType()); // NOI18N
            LOGGER.finest("length of uncompressed snapshot data:" + uncompressedLen); // NOI18N
            LOGGER.finest("save length of snapshot data:" + compressedLen); // NOI18N
            LOGGER.finest("length of settings data:" + baos2.size()); // NOI18N
        }

        dos.writeBytes(PROFILER_FILE_MAGIC_STRING); // 1. magic number: "nbprofiler"
        dos.writeByte(SNAPSHOT_FILE_VERSION_MAJOR); // 2. file version
        dos.writeByte(SNAPSHOT_FILE_VERSION_MINOR); // 3. file version
        dos.writeInt(getType()); // 4. int type
        dos.writeInt(compressedLen); // 5. int length of compressed snapshot data size
        dos.writeInt(uncompressedLen); // 5. int length of compressed snapshot data size
        dos.write(compressedBytes, 0, compressedLen); // 6. compressed snapshot data bytes
        dos.writeInt(baos2.size()); // 7. int length of settings data size
        dos.write(baos2.toByteArray()); // 8. settings data bytes (.properties plain text file format)
        dos.writeUTF(userComments);
    } catch (OutOfMemoryError e) {
        baos = null;
        snapshotDataStream = null;
        baos2 = null;
        settingsDataStream = null;

        throw e;
    } finally {
        if (snapshotDataStream != null) {
            snapshotDataStream.close();
        }

        if (settingsDataStream != null) {
            settingsDataStream.close();
        }
    }
}