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

The following examples show how to use java.util.zip.Deflater#deflate() . 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: 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 2
Source File: Compression.java    From lumongo with Apache License 2.0 6 votes vote down vote up
public static byte[] compressZlib(byte[] bytes, CompressionLevel compressionLevel) {
	Deflater compressor = new Deflater();
	compressor.setLevel(compressionLevel.getLevel());
	compressor.setInput(bytes);
	compressor.finish();

	int bufferLength = Math.max(bytes.length / 10, 16);
	byte[] buf = new byte[bufferLength];
	ByteArrayOutputStream bos = new ByteArrayOutputStream(bufferLength);
	while (!compressor.finished()) {
		int count = compressor.deflate(buf);
		bos.write(buf, 0, count);
	}
	try {
		bos.close();
	}
	catch (Exception e) {

	}
	compressor.end();
	return bos.toByteArray();
}
 
Example 3
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 4
Source File: SDKUtil.java    From unionpay with MIT License 6 votes vote down vote up
/**
 * 压缩.
 * 
 * @param inputByte
 *            需要解压缩的byte[]数组
 * @return 压缩后的数据
 * @throws IOException
 */
public static byte[] deflater(final byte[] inputByte) throws IOException {
	int compressedDataLength = 0;
	Deflater compresser = new Deflater();
	compresser.setInput(inputByte);
	compresser.finish();
	ByteArrayOutputStream o = new ByteArrayOutputStream(inputByte.length);
	byte[] result = new byte[1024];
	try {
		while (!compresser.finished()) {
			compressedDataLength = compresser.deflate(result);
			o.write(result, 0, compressedDataLength);
		}
	} finally {
		o.close();
	}
	compresser.end();
	return o.toByteArray();
}
 
Example 5
Source File: ZlibThreadLocal.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public byte[] deflate(byte[] data, int level) throws IOException {
    Deflater deflater = DEFLATER.get();
    deflater.reset();
    deflater.setLevel(level);
    deflater.setInput(data);
    deflater.finish();
    FastByteArrayOutputStream bos = ThreadCache.fbaos.get();
    bos.reset();
    byte[] buffer = BUFFER.get();
    while (!deflater.finished()) {
        int i = deflater.deflate(buffer);
        bos.write(buffer, 0, i);
    }
    //Deflater::end is called the time when the process exits.
    return bos.toByteArray();
}
 
Example 6
Source File: Debug.java    From openbd-core with GNU General Public License v3.0 6 votes vote down vote up
public static void saveClass( OutputStream _out, Object _class, boolean _compress ) throws IOException{
  if ( _compress ){
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream   OOS  = new ObjectOutputStream(bos);
    OOS.writeObject( _class );
  
    byte [] dataArray = bos.toByteArray();
    byte [] test = new byte[ dataArray.length ]; // this is where the byte array gets compressed to
    Deflater def = new Deflater( Deflater.BEST_COMPRESSION );
    def.setInput( dataArray );
    def.finish();
    def.deflate( test );
    _out.write( test, 0, def.getTotalOut() ); 
  }else{
    ObjectOutputStream   OS = new ObjectOutputStream(_out);
    OS.writeObject( _class );
  }
}
 
Example 7
Source File: ZipPlugin.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
static byte[] compress(byte[] bytesIn) {
    Deflater deflater = new Deflater();
    deflater.setInput(bytesIn);
    ByteArrayOutputStream stream = new ByteArrayOutputStream(bytesIn.length);
    byte[] buffer = new byte[1024];

    deflater.finish();
    while (!deflater.finished()) {
        int count = deflater.deflate(buffer);
        stream.write(buffer, 0, count);
    }

    try {
        stream.close();
    } catch (IOException ex) {
        return bytesIn;
    }

    byte[] bytesOut = stream.toByteArray();
    deflater.end();

    return bytesOut;
}
 
Example 8
Source File: DeflateCompressor.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public int compress(byte[] input, int inputOffset, int inputLength, ICompressor.WrappedArray output, int outputOffset)
{
    Deflater def = deflater.get();
    def.reset();
    def.setInput(input, inputOffset, inputLength);
    def.finish();
    if (def.needsInput())
        return 0;

    int offs = outputOffset;
    while (true)
    {
        offs += def.deflate(output.buffer, offs, output.buffer.length - offs);
        if (def.finished())
        {
            return offs - outputOffset;
        }
        else
        {
            // We're not done, output was too small. Increase it and continue
            byte[] newBuffer = new byte[(output.buffer.length*4)/3 + 1];
            System.arraycopy(output.buffer, 0, newBuffer, 0, offs);
            output.buffer = newBuffer;
        }
    }
}
 
Example 9
Source File: StringCompressUtils.java    From opencards with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private static String compress2(String s) {
    Deflater defl = new Deflater(Deflater.BEST_COMPRESSION);
    defl.setInput(s.getBytes());
    defl.finish();
    boolean done = false;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    while (!done) {
        byte[] buf = new byte[256];
        int bufnum = defl.deflate(buf);
        bos.write(buf, 0, bufnum);
        if (bufnum < buf.length)
            done = true;
    }
    try {
        bos.flush();
        bos.close();
    } catch (IOException ioe) {
        System.err.println(ioe.toString());
    }
    return toHexString(bos.toByteArray());
}
 
Example 10
Source File: TransferUtil.java    From Tatala-RPC with Apache License 2.0 5 votes vote down vote up
private static byte[] getOutputByCompress(byte[] toByteArray){
int compressedLength = 0;
int unCompressedLength = 0;

// out
unCompressedLength = toByteArray.length;
Deflater deflater = new Deflater();
   deflater.setInput(toByteArray);
   deflater.finish();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
   byte[] buf = new byte[1024];
   while (!deflater.finished()) {
       int byteCount = deflater.deflate(buf);
       baos.write(buf, 0, byteCount);
   }
   deflater.end();
   byte[] output = baos.toByteArray();
   compressedLength = output.length;

//send return data just once
byte[] sendData = new byte[TransferUtil.getLengthOfByte() + 
                           TransferUtil.getLengthOfInt() + TransferUtil.getLengthOfInt() + output.length];
//set compress flag and server call flag
sendData[0] = TransferObject.COMPRESS_FLAG;

TransferOutputStream fos = new TransferOutputStream(sendData);
fos.skipAByte();
fos.writeInt(unCompressedLength);
fos.writeInt(compressedLength);
System.arraycopy(output, 0, sendData, 
		TransferUtil.getLengthOfByte() + TransferUtil.getLengthOfInt() + TransferUtil.getLengthOfInt(), output.length);

return sendData;
  }
 
Example 11
Source File: CompressionUtilTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testInflaterReader() throws Exception {
   String inputString = "blahblahblah??blahblahblahblahblah??blablahblah??blablahblah??bla";
   byte[] input = inputString.getBytes(StandardCharsets.UTF_8);
   byte[] output = new byte[30];
   Deflater compressor = new Deflater();
   compressor.setInput(input);
   compressor.finish();
   int compressedDataLength = compressor.deflate(output);

   byte[] zipBytes = new byte[compressedDataLength];

   System.arraycopy(output, 0, zipBytes, 0, compressedDataLength);
   ByteArrayInputStream byteInput = new ByteArrayInputStream(zipBytes);

   ArrayList<Integer> holder = new ArrayList<>();
   try (InflaterReader inflater = new InflaterReader(byteInput)) {
      int read = inflater.read();

      while (read != -1) {
         holder.add(read);
         read = inflater.read();
      }
   }

   byte[] result = new byte[holder.size()];

   for (int i = 0; i < result.length; i++) {
      result[i] = holder.get(i).byteValue();
   }

   String txt = new String(result);

   assertEquals(inputString, txt);
}
 
Example 12
Source File: FontData.java    From ChatUI with MIT License 5 votes vote down vote up
public String asciiToBase64() {
    byte[] data = new byte[this.asciiCharWidths.length >>> 1];
    for (int i = 0; i < this.asciiCharWidths.length; i += 2) {
        data[i >>> 1] = (byte) (((this.asciiCharWidths[i] & 0xF) << 4) | (this.asciiCharWidths[i + 1] & 0xF));
    }
    Deflater deflater = new Deflater();
    deflater.setInput(data);
    deflater.finish();
    byte[] deflated = new byte[this.asciiCharWidths.length];
    int len = deflater.deflate(deflated);
    return BaseEncoding.base64().encode(deflated, 0, len);
}
 
Example 13
Source File: ByteZipUtil.java    From one-net with Apache License 2.0 5 votes vote down vote up
public static byte[] zipBytes(byte[] input) {
    Deflater compresser = new Deflater();
    compresser.setLevel(Deflater.BEST_COMPRESSION);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    compresser.setInput(input);
    byte[] output = new byte[4096];
    compresser.finish();
    int compressedDataLength;
    do {
        compressedDataLength = compresser.deflate(output);
        baos.write(output, 0, compressedDataLength);
    } while (!compresser.finished());
    compresser.end();
    return baos.toByteArray();
}
 
Example 14
Source File: OldAndroidDeflateTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private void compress(Deflater deflater, byte[] inBuf, byte[] outBuf) {
    int inCount = inBuf.length;        // use all
    int inPosn;
    int outPosn;

    inPosn = outPosn = 0;

    //System.out.println("### starting compress");

    while (!deflater.finished()) {
        int want = -1, got;

        // only read if the input buffer is empty
        if (deflater.needsInput() && inCount != 0) {
            want = (inCount < LOCAL_BUF_SIZE) ? inCount : LOCAL_BUF_SIZE;

            deflater.setInput(inBuf, inPosn, want);

            inCount -= want;
            inPosn += want;
            if (inCount == 0) {
                deflater.finish();
            }
        }

        // deflate to current position in output buffer
        int compCount;

        compCount = deflater.deflate(outBuf, outPosn, LOCAL_BUF_SIZE);
        outPosn += compCount;

        //System.out.println("Compressed " + want + ", output " + compCount);
    }
}
 
Example 15
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 16
Source File: ByteBuffer.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Write the entire content into the given file using Flate compression (see
 * RFC1951) then return the number of bytes written.
 */
public long dumpFlate(RandomAccessFile os) throws IOException {
    Deflater zip = new Deflater(Deflater.BEST_COMPRESSION);
    byte[] output = new byte[8192];
    Iterator<byte[]> it = list.iterator(); // when null, that means we have
                                          // told the Deflater that no
                                          // more input would be coming
    long ans = 0; // the number of bytes written out so far
    while (true) {
        if (it != null && zip.needsInput() && it.hasNext()) {
            byte[] in = it.next();
            if (in == list.getLast()) {
                zip.setInput(in, 0, n);
                it = null;
                zip.finish();
            } else {
                zip.setInput(in, 0, SIZE);
            }
        }
        if (it == null && zip.finished())
            break;
        int count = zip.deflate(output);
        if (count > 0) {
            ans = ans + count;
            if (ans < 0)
                throw new IOException("Data too large to be written to the output file.");
            os.write(output, 0, count);
        }
    }
    return ans;
}
 
Example 17
Source File: PackedTransaction.java    From EosProxyServer with GNU Lesser General Public License v3.0 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 18
Source File: CompressionUtil.java    From commons-jcs with Apache License 2.0 5 votes vote down vote up
/**
 * Compress the byte array passed
 * <p>
 * @param input byte array
 * @param bufferLength buffer length
 * @return compressed byte array
 * @throws IOException thrown if we can't close the output stream
 */
public static byte[] compressByteArray( byte[] input, int bufferLength )
    throws IOException
{
    // Compressor with highest level of compression
    Deflater compressor = new Deflater();
    compressor.setLevel( Deflater.BEST_COMPRESSION );

    // Give the compressor the data to compress
    compressor.setInput( input );
    compressor.finish();

    // Create an expandable byte array to hold the compressed data.
    // It is not necessary that the compressed data will be smaller than
    // the uncompressed data.
    ByteArrayOutputStream bos = new ByteArrayOutputStream( input.length );

    // Compress the data
    byte[] buf = new byte[bufferLength];
    while ( !compressor.finished() )
    {
        int count = compressor.deflate( buf );
        bos.write( buf, 0, count );
    }

    // JCS-136 ( Details here : http://www.devguli.com/blog/eng/java-deflater-and-outofmemoryerror/ )
    compressor.end();
    bos.close();

    // Get the compressed data
    return bos.toByteArray();

}
 
Example 19
Source File: AthenaUDFHandler.java    From aws-athena-query-federation with Apache License 2.0 5 votes vote down vote up
/**
 * Compresses a valid UTF-8 String using the zlib compression library.
 * Encodes bytes with Base64 encoding scheme.
 *
 * @param input the String to be compressed
 * @return the compressed String
 */
public String compress(String input)
{
    byte[] inputBytes = input.getBytes(StandardCharsets.UTF_8);

    // create compressor
    Deflater compressor = new Deflater();
    compressor.setInput(inputBytes);
    compressor.finish();

    // compress bytes to output stream
    byte[] buffer = new byte[4096];
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(inputBytes.length);
    while (!compressor.finished()) {
        int bytes = compressor.deflate(buffer);
        byteArrayOutputStream.write(buffer, 0, bytes);
    }

    try {
        byteArrayOutputStream.close();
    }
    catch (IOException e) {
        throw new RuntimeException("Failed to close ByteArrayOutputStream", e);
    }

    // return encoded string
    byte[] compressedBytes = byteArrayOutputStream.toByteArray();
    return Base64.getEncoder().encodeToString(compressedBytes);
}
 
Example 20
Source File: DefaultQCloudClient.java    From wakeup-qcloud-sdk with Apache License 2.0 4 votes vote down vote up
@Override
	public String getUserSig(String identifier, long expire)throws QCloudException {
		try {
			 	Security.addProvider(new BouncyCastleProvider());
		        Reader reader = new CharArrayReader(imConfig.getPrivateKey().toCharArray());
		        JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
		        PEMParser parser = new PEMParser(reader);
		        Object obj = parser.readObject();
		        parser.close();
		    	PrivateKey privKeyStruct = converter.getPrivateKey((PrivateKeyInfo) obj);
				
				String jsonString = "{" 
				+ "\"TLS.account_type\":\"" + 0 +"\","
				+"\"TLS.identifier\":\"" + identifier +"\","
				+"\"TLS.appid_at_3rd\":\"" + 0 +"\","
			    +"\"TLS.sdk_appid\":\"" + imConfig.getSdkAppId() +"\","
				+"\"TLS.expire_after\":\"" + expire +"\""
//		        +"\"TLS.version\": \"201512300000\""
				+"}";
				
				String time = String.valueOf(System.currentTimeMillis()/1000);
				String SerialString = 
					"TLS.appid_at_3rd:" + 0 + "\n" +
					"TLS.account_type:" + 0 + "\n" +
					"TLS.identifier:" + identifier + "\n" + 
					"TLS.sdk_appid:" + imConfig.getSdkAppId() + "\n" + 
					"TLS.time:" + time + "\n" +
					"TLS.expire_after:" + expire +"\n";
				
				//Create Signature by SerialString
				Signature signature = Signature.getInstance("SHA256withECDSA", "BC");
				signature.initSign(privKeyStruct);
				signature.update(SerialString.getBytes(Charset.forName("UTF-8")));
				byte[] signatureBytes = signature.sign();
				
				String sigTLS = Base64.encodeBase64String(signatureBytes);
				
				//Add TlsSig to jsonString
			    JSONObject jsonObject= JSON.parseObject(jsonString);
			    jsonObject.put("TLS.sig", (Object)sigTLS);
			    jsonObject.put("TLS.time", (Object)time);
			    jsonString = jsonObject.toString();
			    
			    //compression
			    Deflater compresser = new Deflater();
			    compresser.setInput(jsonString.getBytes(Charset.forName("UTF-8")));

			    compresser.finish();
			    byte [] compressBytes = new byte [512];
			    int compressBytesLength = compresser.deflate(compressBytes);
			    compresser.end();
			    return new String(Base64Url.base64EncodeUrl(Arrays.copyOfRange(compressBytes,0,compressBytesLength)));
		}catch (Exception e) {
			throw new  QCloudException(e);
		}
	}