Initial commit: ZIP & XZ

This commit is contained in:
Mark Vainomaa 2017-03-26 16:46:37 +03:00
commit 8c84a16075
7 changed files with 294 additions and 0 deletions

21
.gitignore vendored Normal file
View File

@ -0,0 +1,21 @@
# ---> Java
*.class
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.ear
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
# IntelliJ IDEA
*.iml
.idea/
# Maven
target/
dependency-reduced-pom.xml

92
pom.xml Normal file
View File

@ -0,0 +1,92 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>eu.mikroskeem.uurimustoo</groupId>
<artifactId>algoritmidetest</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- Project properties -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.java.version>1.8</project.java.version>
</properties>
<!-- Repositories -->
<repositories>
<repository>
<id>mikroskeem-repo</id>
<name>mikroskeem Maven Repository</name>
<url>https://repo.wut.ee/repository/mikroskeem-repo/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<build>
<plugins>
<!-- Compiler configuration -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>${project.java.version}</source>
<target>${project.java.version}</target>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<!-- Execute directly -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<mainClass>eu.mikroskeem.uurimustoo.algoritmidetest.Main</mainClass>
</configuration>
</plugin>
</plugins>
<defaultGoal>exec:java</defaultGoal>
</build>
<dependencies>
<!-- Tools -->
<dependency>
<groupId>eu.mikroskeem</groupId>
<artifactId>shuriken.common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.16</version>
<scope>provided</scope>
</dependency>
<!-- Classpath scanner -->
<dependency>
<groupId>io.github.lukehutch</groupId>
<artifactId>fast-classpath-scanner</artifactId>
<version>LATEST</version>
</dependency>
<!-- ######## Compressors ######## -->
<!-- XZ -->
<dependency>
<groupId>org.tukaani</groupId>
<artifactId>xz</artifactId>
<version>1.6</version>
</dependency>
</dependencies>
</project>

View File

@ -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);
}
}

View File

@ -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());
}
}
}

View File

@ -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;
}

View File

@ -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);
}
}

View File

@ -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()");
}
}