Initial commit: ZIP & XZ
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
package eu.mikroskeem.uurimustoo.algoritmidetest;
|
||||
|
||||
import eu.mikroskeem.shuriken.common.streams.ByteArrays;
|
||||
|
||||
/**
|
||||
* @author Mark Vainomaa
|
||||
*/
|
||||
public class Main {
|
||||
public static void main(String... args){
|
||||
byte[] classFile = ByteArrays.fromInputStream(Main.class.getResourceAsStream("/Test.class"));
|
||||
Utils.generateReport(classFile);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package eu.mikroskeem.uurimustoo.algoritmidetest;
|
||||
|
||||
import eu.mikroskeem.shuriken.reflect.Reflect;
|
||||
import eu.mikroskeem.uurimustoo.algoritmidetest.compressors.AbstractCompressor;
|
||||
import io.github.lukehutch.fastclasspathscanner.FastClasspathScanner;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author Mark Vainomaa
|
||||
*/
|
||||
public class Utils {
|
||||
public static List<AbstractCompressor> getAllCompressors(){
|
||||
return new FastClasspathScanner(AbstractCompressor.class.getPackage().getName())
|
||||
.scan().getNamesOfSubclassesOf(AbstractCompressor.class)
|
||||
.stream()
|
||||
.map(className -> {
|
||||
try {
|
||||
return (AbstractCompressor)Reflect.getClass(className)
|
||||
.get().construct().getClassInstance();
|
||||
} catch (Exception e){
|
||||
return null;
|
||||
}
|
||||
})
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
||||
public static CompressorResult executeCompressor(byte[] input, AbstractCompressor compressor){
|
||||
return new CompressorResult(compressor.getName(), compressor.compress(input).length);
|
||||
}
|
||||
|
||||
public static List<CompressorResult> executeAllAndSort(byte[] input){
|
||||
List<CompressorResult> compressorResults = getAllCompressors().stream()
|
||||
.map(c -> executeCompressor(input, c))
|
||||
.collect(Collectors.toList());
|
||||
compressorResults.add(new CompressorResult("Orginaal", input.length)); // Inject original size
|
||||
Collections.sort(compressorResults);
|
||||
Collections.reverse(compressorResults);
|
||||
return compressorResults;
|
||||
}
|
||||
|
||||
public static void generateReport(byte[] input){
|
||||
|
||||
System.out.println("---------------------------------");
|
||||
System.out.println("| Algoritm | Suurus |");
|
||||
/* Show other results */
|
||||
executeAllAndSort(input).forEach(compressorResult -> {
|
||||
System.out.println("|----------------|--------------|");
|
||||
System.out.format("| %-12s| %-10s|%n",
|
||||
compressorResult.getCompressorName(),
|
||||
compressorResult.getSize()
|
||||
);
|
||||
});
|
||||
System.out.println("|-------------------------------|");
|
||||
}
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Getter
|
||||
public static class CompressorResult implements Comparable<CompressorResult> {
|
||||
private final String compressorName;
|
||||
private final int size;
|
||||
|
||||
@Override
|
||||
public int compareTo(@NotNull Utils.CompressorResult o) {
|
||||
return new Integer(size).compareTo(o.getSize());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package eu.mikroskeem.uurimustoo.algoritmidetest.compressors;
|
||||
|
||||
import eu.mikroskeem.shuriken.common.SneakyThrow;
|
||||
import org.tukaani.xz.FinishableOutputStream;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* @author Mark Vainomaa
|
||||
*/
|
||||
public abstract class AbstractCompressor {
|
||||
public abstract String getName();
|
||||
|
||||
public final byte[] compress(byte[] input){
|
||||
try {
|
||||
ByteArrayInputStream bais = new ByteArrayInputStream(input);
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
pipe0(bais, baos);
|
||||
return baos.toByteArray();
|
||||
} catch (IOException e){
|
||||
SneakyThrow.throwException(e);
|
||||
}
|
||||
return new byte[0]; // Never reaches here anyway
|
||||
}
|
||||
|
||||
public void pipe0(InputStream inputStream, OutputStream outputStream) throws IOException {
|
||||
OutputStream out = pipe(outputStream);
|
||||
byte[] buf = new byte[8192];
|
||||
int s; while((s = inputStream.read()) != -1) out.write(buf, 0, s);
|
||||
if(out instanceof FinishableOutputStream)
|
||||
((FinishableOutputStream) out).finish();
|
||||
}
|
||||
|
||||
public abstract OutputStream pipe(OutputStream outputStream) throws IOException;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package eu.mikroskeem.uurimustoo.algoritmidetest.compressors;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.tukaani.xz.LZMA2Options;
|
||||
import org.tukaani.xz.XZOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
* @author Mark Vainomaa
|
||||
*/
|
||||
public class CompressXZ extends AbstractCompressor {
|
||||
@Getter private final String name = "XZ";
|
||||
|
||||
@Override
|
||||
public OutputStream pipe(OutputStream outputStream) throws IOException {
|
||||
LZMA2Options options = new LZMA2Options();
|
||||
return new XZOutputStream(outputStream, options);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package eu.mikroskeem.uurimustoo.algoritmidetest.compressors;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
/**
|
||||
* @author Mark Vainomaa
|
||||
*/
|
||||
public class CompressZip extends AbstractCompressor {
|
||||
@Getter private final String name = "ZIP";
|
||||
|
||||
@Override
|
||||
public void pipe0(InputStream inputStream, OutputStream outputStream) throws IOException {
|
||||
try(ZipOutputStream out = new ZipOutputStream(outputStream)){
|
||||
ZipEntry ze = new ZipEntry("Test.class");
|
||||
out.putNextEntry(ze);
|
||||
byte[] buf = new byte[8192];
|
||||
int s;
|
||||
while((s = inputStream.read()) != -1){
|
||||
out.write(buf, 0, s);
|
||||
}
|
||||
out.closeEntry();
|
||||
out.finish();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public OutputStream pipe(OutputStream outputStream) throws IOException {
|
||||
throw new UnsupportedOperationException("See void pipe0()");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user