Java Deflate Byte Array deflate(String inString)

Here you can find the source of deflate(String inString)

Description

Applies a standard deflate algo to the input String

License

Open Source License

Parameter

Parameter Description
inString the String to deflate

Return

the deflated byte array

Declaration

public static byte[] deflate(String inString) throws IOException 

Method Source Code

//package com.java2s;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import java.util.zip.Deflater;

public class Main {
    /**// w  w  w .  j a va  2s .c o  m
     * 
     */
    protected static final int IO_BUFFER_SIZE = 4 * 1024;

    /**
     * Applies a standard deflate algo to the input String
     * @param inString the String to deflate
     * @return the deflated byte array
     * 
     */
    public static byte[] deflate(String inString) throws IOException {
        Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
        byte[] inBytes = inString.getBytes("UTF-8");
        deflater.setInput(inBytes);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(
                inBytes.length);
        deflater.finish();
        byte[] buffer = new byte[IO_BUFFER_SIZE];

        while (!deflater.finished()) {
            int count = deflater.deflate(buffer); // returns the generated code... index  
            outputStream.write(buffer, 0, count);
        }

        outputStream.close();
        byte[] output = outputStream.toByteArray();

        return output;
    }
}

Related

  1. deflate(byte[] data)
  2. deflate(byte[] data, byte[] dictionary)
  3. deflate(byte[] in)
  4. deflate(byte[] input)
  5. deflate(final byte[] pInput)
  6. deflate(String text, String encode)
  7. deflateBuffer(byte[] uncompressedBuffer)
  8. deflateByteArray(final byte[] array)
  9. deflateGzip(byte[] inputBytes)