Merge branch 'eugenp:master' into danielmcnally285_return_first_non_null
This commit is contained in:
@@ -29,7 +29,6 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.5.1</version>
|
||||
<configuration>
|
||||
<source>17</source>
|
||||
<target>17</target>
|
||||
@@ -50,7 +49,7 @@
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<mapstruct.version>1.5.5.Final</mapstruct.version>
|
||||
<mapstruct.version>1.6.0.Beta1</mapstruct.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package com.baeldung.compressbytes;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.zip.DataFormatException;
|
||||
import java.util.zip.Deflater;
|
||||
import java.util.zip.Inflater;
|
||||
|
||||
public class CompressByteArrayUtil {
|
||||
|
||||
public static byte[] compress(byte[] input) {
|
||||
Deflater deflater = new Deflater();
|
||||
deflater.setInput(input);
|
||||
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);
|
||||
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
byte[] buffer = new byte[1024];
|
||||
|
||||
while (!inflater.finished()) {
|
||||
int decompressedSize = inflater.inflate(buffer);
|
||||
outputStream.write(buffer, 0, decompressedSize);
|
||||
}
|
||||
|
||||
return outputStream.toByteArray();
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
String inputString = "Baeldung helps developers explore the Java ecosystem and simply be better engineers. " +
|
||||
"We publish to-the-point guides and courses, with a strong focus on building web applications, Spring, " +
|
||||
"Spring Security, and RESTful APIs";
|
||||
byte[] input = inputString.getBytes();
|
||||
|
||||
// Compression
|
||||
byte[] compressedData = compress(input);
|
||||
|
||||
// Decompression
|
||||
byte[] decompressedData = decompress(compressedData);
|
||||
|
||||
System.out.println("Original: " + input.length + " bytes");
|
||||
System.out.println("Compressed: " + compressedData.length + " bytes");
|
||||
System.out.println("Decompressed: " + decompressedData.length + " bytes");
|
||||
}
|
||||
|
||||
}
|
||||
+1
-3
@@ -4,9 +4,7 @@ public class InterruptThread extends Thread {
|
||||
@Override
|
||||
public void run() {
|
||||
while (!isInterrupted()) {
|
||||
if (isInterrupted()) {
|
||||
break;
|
||||
}
|
||||
break;
|
||||
// business logic
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user