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 = "GeeksforGeeks" , 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 :GeeksforGeeksGeeksforGeeksGeeksforGeeksGeeksforGeeks 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()