diff --git a/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/compressbytes/CompressByteArrayUtil.java b/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/compressbytes/CompressByteArrayUtil.java index 88e2283985..5a7ea2da58 100644 --- a/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/compressbytes/CompressByteArrayUtil.java +++ b/core-java-modules/core-java-lang-6/src/main/java/com/baeldung/compressbytes/CompressByteArrayUtil.java @@ -23,6 +23,23 @@ public class CompressByteArrayUtil { return outputStream.toByteArray(); } + public static byte[] compressWithCustomLevel(byte[] input, int level) { + Deflater deflater = new Deflater(); + deflater.setInput(input); + deflater.setLevel(level); + deflater.finish(); + + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + byte[] buffer = new byte[1024]; + + while (!deflater.finished()) { + int compressedSize = deflater.deflate(buffer); + outputStream.write(buffer, 0, compressedSize); + } + + return outputStream.toByteArray(); + } + public static byte[] decompress(byte[] input) throws DataFormatException { Inflater inflater = new Inflater(); inflater.setInput(input); diff --git a/core-java-modules/core-java-lang-6/src/test/java/com/baeldung/compressbytes/CompressByteArrayUnitTest.java b/core-java-modules/core-java-lang-6/src/test/java/com/baeldung/compressbytes/CompressByteArrayUnitTest.java new file mode 100644 index 0000000000..822ad5063c --- /dev/null +++ b/core-java-modules/core-java-lang-6/src/test/java/com/baeldung/compressbytes/CompressByteArrayUnitTest.java @@ -0,0 +1,39 @@ +package com.baeldung.compressbytes; + +import org.junit.jupiter.api.Test; + +import java.util.zip.DataFormatException; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class CompressByteArrayUnitTest { + + private static final String INPUT_STRING = "Building a REST API is not a trivial task – from the high-level RESTful " + + "constraints down to the nitty-gritty of making everything work and work well." + + "Spring has made REST a first-class citizen and the platform has been maturing in leaps and bounds." + + "With this guide, my aim is to organize the mountains of information that are available on the subject and " + + "guide you through properly building an API." + + "The guide starts with the basics – bootstrapping the REST API, the Spring MVC Configuration, and basic customization."; + + @Test + void givenInputString_whenCompressWithDefaultLevel_thenDecompressWithSameSize() throws DataFormatException { + byte[] input = INPUT_STRING.getBytes(); + byte[] compressedData = CompressByteArrayUtil.compress(input); + byte[] decompressedData = CompressByteArrayUtil.decompress(compressedData); + System.out.println("Original: " + input.length + " bytes"); + System.out.println("Compressed: " + compressedData.length + " bytes"); + System.out.println("Decompressed: " + decompressedData.length + " bytes"); + assertEquals(input.length, decompressedData.length); + } + + @Test + void givenInputString_whenCompressWithCustomLevel_thenDecompressWithSameSize() throws DataFormatException { + byte[] input = INPUT_STRING.getBytes(); + byte[] compressedData = CompressByteArrayUtil.compressWithCustomLevel(input, 1); + byte[] decompressedData = CompressByteArrayUtil.decompress(compressedData); + System.out.println("Original: " + input.length + " bytes"); + System.out.println("Compressed: " + compressedData.length + " bytes"); + System.out.println("Decompressed: " + decompressedData.length + " bytes"); + assertEquals(input.length, decompressedData.length); + } +}