Java 8 Deflater - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 Deflater class.

This code example shows how to use the following methods:end
 
  @Override 
  public final void write(DataOutput out) throws IOException {
    if (compressed == null) {
      ByteArrayOutputStream deflated = new ByteArrayOutputStream();
      Deflater deflater = new Deflater(Deflater.BEST_SPEED);
      DataOutputStream dout =
        new DataOutputStream(new DeflaterOutputStream(deflated, deflater));
      writeCompressed(dout);
      dout.close();
      deflater.end();
      compressed = deflated.toByteArray();
    } 
    out.writeInt(compressed.length);
    out.write(compressed);
  } 
 
  /** Subclasses implement this instead of {@link #write(DataOutput)}. */ 
  protected abstract void writeCompressed(DataOutput out) throws IOException;
 
7
CodeRank
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

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
See Code Examples for Java 8 Deflater Methods: