Deflater deflate - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Deflater deflate method.

This code example shows how to use the following methods:deflate, finish, setInput
		// Encode a String into bytes 
		byte[] input = baos.toByteArray();
 
		// Compress the bytes 
		byte[] output1 = new byte[input.length];
		Deflater compresser = new Deflater();
		compresser.setInput(input);
		compresser.finish();
		int compressedDataLength = compresser.deflate(output1);
 
		System.out.println("input len="+input.length+" output len="+compressedDataLength);
		byte[] compressed2 = Arrays.copyOfRange(output1, 0, compressedDataLength);
		String result2 = Base64.encodeBase64URLSafeString(compressed2);
		System.out.println("url param length=" + result2.length() + " original len="+all.length());
		System.out.println("url param="+result2);
		byte[] afterBase64 = Base64.decodeBase64(result2);
 
		System.out.println("len bytes=" + afterBase64.length + " before="
				+ compressed2.length + " output1=" + output1.length
				+ " compressLen=" + compressedDataLength);
7
CodeRank
This code example shows how to use the following methods:deflate, end, finish, setInput, setStrategy
            throw DbException.get(ErrorCode.UNSUPPORTED_COMPRESSION_OPTIONS_1, options);
        } 
    } 
 
    public int compress(byte[] in, int inLen, byte[] out, int outPos) {
        Deflater deflater = new Deflater(level);
        deflater.setStrategy(strategy);
        deflater.setInput(in, 0, inLen);
        deflater.finish();
        int compressed = deflater.deflate(out, outPos, out.length - outPos);
        while (compressed == 0) {
            // the compressed length is 0, meaning compression didn't work 
            // (sounds like a JDK bug) 
            // try again, using the default strategy and compression level 
            strategy = Deflater.DEFAULT_STRATEGY;
            level = Deflater.DEFAULT_COMPRESSION;
            return compress(in, inLen, out, outPos);
        } 
        deflater.end();
        return outPos + compressed;

7
CodeRank
This code example shows how to use the following methods:deflate, end, finish, setInput, setStrategy
        } 
    } 
 
    @Override 
    public int compress(byte[] in, int inLen, byte[] out, int outPos) {
        Deflater deflater = new Deflater(level);
        deflater.setStrategy(strategy);
        deflater.setInput(in, 0, inLen);
        deflater.finish();
        int compressed = deflater.deflate(out, outPos, out.length - outPos);
        while (compressed == 0) {
            // the compressed length is 0, meaning compression didn't work 
            // (sounds like a JDK bug) 
            // try again, using the default strategy and compression level 
            strategy = Deflater.DEFAULT_STRATEGY;
            level = Deflater.DEFAULT_COMPRESSION;
            return compress(in, inLen, out, outPos);
        } 
        deflater.end();
        return outPos + compressed;

7
CodeRank
This code example shows how to use the following methods:deflate, finish, setInput
        return String.format(LOGOUT_REQUEST_TEMPLATE, new Date(), sessionIndex);
    } 
 
    public static String generateFrontChannelLogoutMessage(final String sessionIndex) {
        final String logoutMessage = generateBackChannelLogoutMessage(sessionIndex);
        final Deflater deflater = new Deflater();
        deflater.setInput(logoutMessage.getBytes(Charset.forName("ASCII")));
        deflater.finish();
        final byte[] buffer = new byte[logoutMessage.length()];
        final int resultSize = deflater.deflate(buffer);
        final byte[] output = new byte[resultSize];
        System.arraycopy(buffer, 0, output, 0, resultSize);
        return Base64.encodeBase64String(output);
    } 
} 
6
CodeRank
This code example shows how to use the following methods:deflate, finish, getAdler, setInput
        String inputString = "blahblahblah??";
        byte[] input = inputString.getBytes("UTF-8");
 
        // Compress the bytes 
        byte[] output = new byte[100];
        Deflater compresser = new Deflater();
        compresser.setInput(input);
        compresser.finish();
        int compressedDataLength = compresser.deflate(output);
 
        // Decompress the bytes 
        Inflater decompresser = new Inflater();
        decompresser.setInput(output, 0, compressedDataLength);
        byte[] result = new byte[100];
        int resultLength = decompresser.inflate(result);
 
        // Decode the bytes into a String 
        String outputString = new String(result, 0, resultLength, "UTF-8");
 
        assertEquals(inputString, outputString);

6
CodeRank
This code example shows how to use the following methods:deflate, end, finish, setInput, setStrategy
            throw DbException.get(ErrorCode.UNSUPPORTED_COMPRESSION_OPTIONS_1, options);
        } 
    } 
 
    public int compress(byte[] in, int inLen, byte[] out, int outPos) {
        Deflater deflater = new Deflater(level);
        deflater.setStrategy(strategy);
        deflater.setInput(in, 0, inLen);
        deflater.finish();
        int compressed = deflater.deflate(out, outPos, out.length - outPos);
        while (compressed == 0) {
            // the compressed length is 0, meaning compression didn't work 
            // (sounds like a JDK bug) 
            // try again, using the default strategy and compression level 
            strategy = Deflater.DEFAULT_STRATEGY;
            level = Deflater.DEFAULT_COMPRESSION;
            return compress(in, inLen, out, outPos);
        } 
        deflater.end();
        return outPos + compressed;

6
CodeRank
This code example shows how to use the following methods:deflate, finish, finished, reset, setInput
            super.addFrame(clearFlag(flags,1),opcode,content,offset,length);
            return; 
        } 
 
        // prepare the uncompressed input 
        _deflater.reset();
        _deflater.setInput(content,offset,length);
        _deflater.finish();
 
        // prepare the output buffer 
        byte[] out= new byte[length];
        int out_offset=0;
 
        // write the uncompressed length 
        if (length>0xffff)
        { 
            out[out_offset++]=0x7f;
            out[out_offset++]=(byte)0;
            out[out_offset++]=(byte)0;
            out[out_offset++]=(byte)0;

6
CodeRank
This code example shows how to use the following methods:deflate, end, finish, setInput
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Compresser.class.getName()).log(Level.SEVERE, null, ex);
        } 
        byte[] compressedXml = new byte[xmlBytes.length + 1000]; // Insure a minimum size.
        m.setCompressedXml(compressedXml);
        Deflater compresser = new Deflater();
        compresser.setInput(xmlBytes);
        compresser.finish();
        int compressedXmlLength = compresser.deflate(compressedXml);
        m.setCompressedXmlLength(compressedXmlLength);
        compresser.end();
    } 
     
    static void decompress(Message m) {
        byte[] compressedXml = m.getCompressedXml();
        int compressedXmlLength = m.getCompressedXmlLength();
        Inflater decompresser = new Inflater();
        decompresser.setInput(compressedXml, 0, compressedXmlLength);
        byte[] inflatedBytes = new byte[60000];
        int resultLength = 0;
6
CodeRank
This code example shows how to use the following methods:deflate, finish, setInput
    public static byte[] deflate(byte[] tokenBytes) {
        return deflate(tokenBytes, true);
    } 
     
    public static byte[] deflate(byte[] tokenBytes, boolean nowrap) {
        Deflater compresser = new Deflater(Deflater.DEFLATED, 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;
    } 
} 
6
CodeRank
This code example shows how to use the following methods:deflate, finish, setInput
    } 
 
    // Compress the test string into compressedBytes 
    private int compressToBytes(String string) {
        byte[] input = string.getBytes();
        Deflater deflater = new Deflater();
        deflater.setInput(input);
        deflater.finish();
        return deflater.deflate(compressedBytes);
    } 
 
} 
6
CodeRank
deflate Method Overview
Compresses the input data and fills specified buffer with compressed data.
See Code Examples for other Java 8 Deflater Methods: