Saturday, October 25, 2025
HomeLanguagesJavaDeflater finished() function in Java with examples

Deflater finished() function in Java with examples

The finished() function of the Deflater class in java.util.zip returns true if the end of compression data output stream has been reached.

Function Signature:

public boolean finished()

Syntax:

d.finished();

Parameter: The function requires no parameter

Return Type: The function returns boolean value i.e. true if all the input is compressed and stored in the given buffer, else false.

Exception: The function does not throw any exception

Example 1: To demonstrate the use of finished() function




// Java program to demonstrate
// the use of finished() function
  
import java.util.zip.*;
import java.io.UnsupportedEncodingException;
  
class GFG {
    public static void main(String args[])
        throws UnsupportedEncodingException
    {
        // deflater
        Deflater d = new Deflater();
  
        // get the text
        String pattern = "Lazyroar", text = "";
  
        // generate the text
        for (int i = 0; i < 4; i++)
            text += pattern;
  
        // set the input for deflator
        d.setInput(text.getBytes("UTF-8"));
  
        // finish
        d.finish();
  
        // output of finished function
        System.out.println("end of compressed data "
                           + "output stream reached :"
                           + d.finished());
  
        // output bytes
        byte output[] = new byte[1024];
  
        // compress the data
        int size = d.deflate(output);
  
        // compressed String
        System.out.println("Compressed String :"
                           + new String(output)
                           + "\n Size " + size);
  
        // original String
        System.out.println("Original String :" + text
                           + "\n Size " + text.length());
  
        // output of finished function
        System.out.println("end of compressed data "
                           + "output stream reached :"
                           + d.finished());
  
        // end
        d.end();
    }
}


Output:

end of compressed data output stream reached :false
Compressed String :x?sOM?.N?/r???q??
 Size 21
Original String :LazyroarLazyroarLazyroarLazyroar
 Size 52
end of compressed data output stream reached :true

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/zip/Deflater.html#finished()

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS