Deflater getTotalOut() function in Java with examples


The getTotalOut() function of the Deflater class in Java returns the total number of compressed bytes output so far by the deflater. This includes bytes that have been written to the output buffer as well as any bytes that may be pending in the deflater's internal buffer.

Here's an example of how to use the getTotalOut() function:

import java.util.zip.Deflater;

public class Example {
    public static void main(String[] args) {
        Deflater deflater = new Deflater();
        byte[] input = "Hello, world!".getBytes();
        byte[] output = new byte[100];
        deflater.setInput(input);
        deflater.finish();
        int compressedSize = deflater.deflate(output);
        int totalCompressedSize = deflater.getTotalOut();
        System.out.println("Compressed size: " + compressedSize);
        System.out.println("Total compressed size: " + totalCompressedSize);
    }
}

In this example, we create a Deflater object and set its input to the string "Hello, world!". We then call finish() to indicate that we're done with input and want to finalize the compression process. We then call deflate() to compress the input and write the compressed bytes to the output buffer. Finally, we call getTotalOut() to get the total number of compressed bytes output by the deflater.

Another example of using getTotalOut() is when compressing a file:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;

public class Example {
    public static void main(String[] args) throws Exception {
        FileInputStream inputFile = new FileInputStream("input.txt");
        FileOutputStream outputFile = new FileOutputStream("output.txt");
        Deflater deflater = new Deflater();
        DeflaterOutputStream deflaterStream = new DeflaterOutputStream(outputFile, deflater);
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = inputFile.read(buffer)) != -1) {
            deflaterStream.write(buffer, 0, bytesRead);
        }
        deflaterStream.finish();
        inputFile.close();
        outputFile.close();
        int totalCompressedSize = deflater.getTotalOut();
        System.out.println("Total compressed size: " + totalCompressedSize);
    }
}

In this example, we create a FileInputStream to read from an input file and a FileOutputStream to write to an output file. We then create a Deflater object and a DeflaterOutputStream object, passing the output file and deflater as parameters to the constructor. We then read from the input file in chunks of 1024 bytes, write the compressed output to the output file, and repeat until we've read the entire input file. Finally, we call finish() on the DeflaterOutputStream to finalize the compression process, close the input and output files, and call getTotalOut() to get the total number of compressed bytes output by the deflater.



About the author

William Pham is the Admin and primary author of Howto-Code.com. With over 10 years of experience in programming. William Pham is fluent in several programming languages, including Python, PHP, JavaScript, Java, C++.