Make code more robust
This commit is contained in:
parent
8eb6c6b0ca
commit
f7a2398a75
2
pom.xml
2
pom.xml
@ -61,7 +61,7 @@
|
||||
<!-- Tools -->
|
||||
<dependency>
|
||||
<groupId>eu.mikroskeem</groupId>
|
||||
<artifactId>shuriken.common</artifactId>
|
||||
<artifactId>shuriken.instrumentation</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
package eu.mikroskeem.uurimustoo.algoritmidetest;
|
||||
|
||||
import eu.mikroskeem.shuriken.instrumentation.validate.Validate;
|
||||
import eu.mikroskeem.shuriken.reflect.Reflect;
|
||||
import eu.mikroskeem.uurimustoo.algoritmidetest.compressors.AbstractCompressor;
|
||||
import io.github.lukehutch.fastclasspathscanner.FastClasspathScanner;
|
||||
@ -34,7 +35,18 @@ public class Utils {
|
||||
|
||||
|
||||
public static CompressorResult executeCompressor(byte[] input, AbstractCompressor compressor){
|
||||
return new CompressorResult(compressor.getName(), compressor.compress(input).length);
|
||||
String compressorName = compressor.getName();
|
||||
byte[] compressed = compressor.compress(input);
|
||||
/* Verify for sure */
|
||||
try {
|
||||
byte[] decompressed = compressor.decompress(compressed);
|
||||
Validate.checkGeneratedClass(decompressed);
|
||||
assert decompressed.length == input.length;
|
||||
} catch (Throwable e){
|
||||
System.out.println(String.format("Tekkis viga %s algoritmi lahtipakkimisel! %s", compressorName, e.toString()));
|
||||
compressorName = compressorName + " (katki)";
|
||||
}
|
||||
return new CompressorResult(compressorName, compressed.length);
|
||||
}
|
||||
|
||||
public static List<CompressorResult> executeAllAndSort(byte[] input){
|
||||
|
@ -14,7 +14,7 @@ public abstract class AbstractCompressor {
|
||||
try {
|
||||
ByteArrayInputStream bais = new ByteArrayInputStream(input);
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
pipe0(bais, baos);
|
||||
compress0(bais, baos);
|
||||
return baos.toByteArray();
|
||||
} catch (IOException e){
|
||||
SneakyThrow.throwException(e);
|
||||
@ -22,13 +22,34 @@ public abstract class AbstractCompressor {
|
||||
return new byte[0]; // Never reaches here anyway
|
||||
}
|
||||
|
||||
public void pipe0(InputStream inputStream, OutputStream outputStream) throws IOException {
|
||||
try(OutputStream out = pipe(outputStream)) {
|
||||
public final byte[] decompress(byte[] input){
|
||||
try {
|
||||
ByteArrayInputStream bais = new ByteArrayInputStream(input);
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
decompress0(bais, baos);
|
||||
return baos.toByteArray();
|
||||
} catch (IOException e){
|
||||
SneakyThrow.throwException(e);
|
||||
}
|
||||
return new byte[0]; // Never reaches here anyway
|
||||
}
|
||||
|
||||
public void compress0(InputStream inputStream, OutputStream outputStream) throws IOException {
|
||||
try(OutputStream out = compress(outputStream)) {
|
||||
byte[] buf = new byte[8192];
|
||||
int s; while ((s = inputStream.read(buf)) != -1) out.write(buf, 0, s);
|
||||
out.flush();
|
||||
}
|
||||
}
|
||||
|
||||
public abstract OutputStream pipe(OutputStream outputStream) throws IOException;
|
||||
public void decompress0(InputStream inputStream, OutputStream outputStream) throws IOException {
|
||||
try(InputStream is = decompress(inputStream)) {
|
||||
byte[] buf = new byte[8192];
|
||||
int s; while ((s = is.read(buf)) != -1) outputStream.write(buf, 0, s);
|
||||
outputStream.flush();
|
||||
}
|
||||
}
|
||||
|
||||
public abstract OutputStream compress(OutputStream outputStream) throws IOException;
|
||||
public abstract InputStream decompress(InputStream inputStream) throws IOException;
|
||||
}
|
||||
|
@ -3,7 +3,9 @@ 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.GZIPInputStream;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
/**
|
||||
@ -13,7 +15,12 @@ public class CompressGZip extends AbstractCompressor {
|
||||
@Getter private final String name = "GZip";
|
||||
|
||||
@Override
|
||||
public OutputStream pipe(OutputStream outputStream) throws IOException {
|
||||
public OutputStream compress(OutputStream outputStream) throws IOException {
|
||||
return new GZIPOutputStream(outputStream);
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream decompress(InputStream inputStream) throws IOException {
|
||||
return new GZIPInputStream(inputStream);
|
||||
}
|
||||
}
|
||||
|
@ -2,9 +2,11 @@ package eu.mikroskeem.uurimustoo.algoritmidetest.compressors;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.tukaani.xz.LZMA2Options;
|
||||
import org.tukaani.xz.XZInputStream;
|
||||
import org.tukaani.xz.XZOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
@ -14,8 +16,13 @@ public class CompressXZ extends AbstractCompressor {
|
||||
@Getter private final String name = "XZ";
|
||||
|
||||
@Override
|
||||
public OutputStream pipe(OutputStream outputStream) throws IOException {
|
||||
public OutputStream compress(OutputStream outputStream) throws IOException {
|
||||
LZMA2Options options = new LZMA2Options();
|
||||
return new XZOutputStream(outputStream, options);
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream decompress(InputStream inputStream) throws IOException {
|
||||
return new XZInputStream(inputStream);
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
/**
|
||||
@ -15,7 +16,7 @@ public class CompressZip extends AbstractCompressor {
|
||||
@Getter private final String name = "ZIP";
|
||||
|
||||
@Override
|
||||
public void pipe0(InputStream inputStream, OutputStream outputStream) throws IOException {
|
||||
public void compress0(InputStream inputStream, OutputStream outputStream) throws IOException {
|
||||
try(ZipOutputStream out = new ZipOutputStream(outputStream)){
|
||||
ZipEntry ze = new ZipEntry("Test.class");
|
||||
out.putNextEntry(ze);
|
||||
@ -30,7 +31,26 @@ public class CompressZip extends AbstractCompressor {
|
||||
}
|
||||
|
||||
@Override
|
||||
public OutputStream pipe(OutputStream outputStream) throws IOException {
|
||||
throw new UnsupportedOperationException("See void pipe0()");
|
||||
public void decompress0(InputStream inputStream, OutputStream outputStream) throws IOException {
|
||||
try(ZipInputStream in = new ZipInputStream(inputStream)){
|
||||
ZipEntry ze = in.getNextEntry();
|
||||
assert ze.getName().equals("Test.class");
|
||||
byte[] buf = new byte[8192];
|
||||
int s;
|
||||
while((s = in.read(buf)) != -1){
|
||||
outputStream.write(buf, 0, s);
|
||||
}
|
||||
outputStream.flush();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public OutputStream compress(OutputStream outputStream) throws IOException {
|
||||
throw new UnsupportedOperationException("See void compress0()");
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream decompress(InputStream inputStream) throws IOException {
|
||||
throw new UnsupportedOperationException("See void decompress0()");
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user