-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
91 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
compression/src/main/java/de/afrouper/compression/NumberCompression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package de.afrouper.compression; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.Base64; | ||
|
||
public class NumberCompression { | ||
|
||
private static final byte BYTES_SHORT = 2; | ||
private static final byte BYTES_INTEGER = 4; | ||
|
||
private final Base64.Encoder encoder = Base64.getEncoder(); | ||
private final Base64.Decoder decoder = Base64.getDecoder(); | ||
|
||
public String compressShort(short[] numbers) { | ||
byte[] bytes = new byte[numbers.length * BYTES_SHORT]; | ||
for (int numberIndex = 0, byteIndex = 0; numberIndex < numbers.length; numberIndex++, byteIndex = byteIndex + BYTES_SHORT) { | ||
bytes[byteIndex] = (byte)(numbers[numberIndex] >> 8); | ||
bytes[byteIndex+1] = (byte)numbers[numberIndex]; | ||
} | ||
return encoder.encodeToString(bytes); | ||
} | ||
|
||
public String compressInteger(int[] numbers) { | ||
byte[] bytes = new byte[numbers.length * BYTES_INTEGER]; | ||
for (int numberIndex = 0, byteIndex = 0; numberIndex < numbers.length; numberIndex++, byteIndex = byteIndex + BYTES_INTEGER) { | ||
bytes[byteIndex] = (byte)(numbers[numberIndex] >> 24); | ||
bytes[byteIndex+1] = (byte)(numbers[numberIndex] >> 16); | ||
bytes[byteIndex+2] = (byte)(numbers[numberIndex] >> 8); | ||
bytes[byteIndex+3] = (byte)numbers[numberIndex]; | ||
} | ||
return encoder.encodeToString(bytes); | ||
} | ||
|
||
public short[] uncompressShort(String data) { | ||
byte[] bytes = decoder.decode(data.getBytes(StandardCharsets.UTF_8)); | ||
short[] numbers = new short[bytes.length / BYTES_SHORT]; | ||
for (int numberIndex = 0, byteIndex = 0; numberIndex < numbers.length; numberIndex++, byteIndex = byteIndex + BYTES_SHORT) { | ||
numbers[numberIndex] = (short) (((bytes[byteIndex] & 0xFF) << 8 ) | (bytes[byteIndex+1] & 0xFF) ); | ||
} | ||
return numbers; | ||
} | ||
|
||
public int[] uncompressInteger(String data) { | ||
byte[] bytes = decoder.decode(data.getBytes(StandardCharsets.UTF_8)); | ||
int[] numbers = new int[bytes.length / BYTES_INTEGER]; | ||
for (int numberIndex = 0, byteIndex = 0; numberIndex < numbers.length; numberIndex++, byteIndex = byteIndex + BYTES_INTEGER) { | ||
numbers[numberIndex] = ( | ||
((bytes[byteIndex] & 0xFF) << 24 ) | | ||
((bytes[byteIndex+1] & 0xFF) << 16 ) | | ||
((bytes[byteIndex+2] & 0xFF) << 8 ) | | ||
(bytes[byteIndex+3] & 0xFF)); | ||
} | ||
return numbers; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
compression/src/test/java/de/afrouper/compression/NumberCompressionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package de.afrouper.compression; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class NumberCompressionTest { | ||
|
||
private static final short[] NUMBERS_SHORT = new short[]{4711,815,88,1,9999,8000,7514,6941,1254,-500,0}; | ||
private static final int[] NUMBERS_INTEGER = new int[]{4711,815,88,1,9999,8000,7514,6941,1254, 326547,-500,0}; | ||
|
||
private final NumberCompression numberCompression = new NumberCompression(); | ||
|
||
@Test | ||
void testCompressionCycleShort() { | ||
String compressed = numberCompression.compressShort(NUMBERS_SHORT); | ||
assertNotNull(compressed); | ||
System.out.println("Bytes: " + compressed.length()); | ||
short[] uncompressed = numberCompression.uncompressShort(compressed); | ||
assertArrayEquals(NUMBERS_SHORT, uncompressed); | ||
} | ||
|
||
@Test | ||
void testCompressionCycleInteger() { | ||
String compressed = numberCompression.compressInteger(NUMBERS_INTEGER); | ||
assertNotNull(compressed); | ||
System.out.println("Bytes: " + compressed.length()); | ||
int[] uncompressed = numberCompression.uncompressInteger(compressed); | ||
assertArrayEquals(NUMBERS_INTEGER, uncompressed); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
rootProject.name = 'Playground' | ||
rootProject.name = 'playground' | ||
|
||
include 'compression' | ||
include 'BCEL' | ||
include 'backtracking' | ||
include 'jzos_batch' | ||
include 'JNI' | ||
include 'xmp' | ||
include 'compression' |