Skip to content

Commit

Permalink
Implementation compression module
Browse files Browse the repository at this point in the history
  • Loading branch information
ChKemper committed Jun 10, 2024
1 parent f37190d commit 75a44a4
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
3 changes: 3 additions & 0 deletions compression/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.2'
}
tasks.named('test', Test) {
useJUnitPlatform()
}
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;
}
}
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);
}
}
4 changes: 2 additions & 2 deletions settings.gradle
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'

0 comments on commit 75a44a4

Please sign in to comment.