BAEL-7106-compress-and-uncompress-bytes-2 (#15398)
This commit is contained in:
parent
9dcd9c9c20
commit
51f2ed495b
@ -23,6 +23,23 @@ public class CompressByteArrayUtil {
|
|||||||
return outputStream.toByteArray();
|
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 {
|
public static byte[] decompress(byte[] input) throws DataFormatException {
|
||||||
Inflater inflater = new Inflater();
|
Inflater inflater = new Inflater();
|
||||||
inflater.setInput(input);
|
inflater.setInput(input);
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user