Restructure abstract class and add LZ4 (fast) compressor

This commit is contained in:
Mark Vainomaa 2017-03-26 18:17:07 +03:00
parent 202eec3547
commit c04b7a7869
6 changed files with 54 additions and 26 deletions

View File

@ -88,5 +88,12 @@
<artifactId>xz</artifactId>
<version>1.6</version>
</dependency>
<!-- LZ4 -->
<dependency>
<groupId>net.jpountz.lz4</groupId>
<artifactId>lz4</artifactId>
<version>1.3.0</version>
</dependency>
</dependencies>
</project>

View File

@ -10,11 +10,11 @@ import java.io.*;
public abstract class AbstractAlgorithm {
public abstract String getName();
public final byte[] compress(byte[] input){
public byte[] compress(byte[] input){
try {
ByteArrayInputStream bais = new ByteArrayInputStream(input);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
compress0(bais, baos);
compress(bais, baos);
return baos.toByteArray();
} catch (IOException e){
SneakyThrow.throwException(e);
@ -22,11 +22,11 @@ public abstract class AbstractAlgorithm {
return new byte[0]; // Never reaches here anyway
}
public final byte[] decompress(byte[] input){
public byte[] decompress(byte[] input){
try {
ByteArrayInputStream bais = new ByteArrayInputStream(input);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
decompress0(bais, baos);
decompress(bais, baos);
return baos.toByteArray();
} catch (IOException e){
SneakyThrow.throwException(e);
@ -34,22 +34,27 @@ public abstract class AbstractAlgorithm {
return new byte[0]; // Never reaches here anyway
}
public void compress0(InputStream inputStream, OutputStream outputStream) throws IOException {
try(OutputStream out = compress(outputStream)) {
public void compress(InputStream inputStream, OutputStream outputStream) throws IOException {
try(OutputStream out = createCompressor(outputStream)) {
byte[] buf = new byte[8192];
int s; while ((s = inputStream.read(buf)) != -1) out.write(buf, 0, s);
out.flush();
}
}
public void decompress0(InputStream inputStream, OutputStream outputStream) throws IOException {
try(InputStream is = decompress(inputStream)) {
public void decompress(InputStream inputStream, OutputStream outputStream) throws IOException {
try(InputStream is = createDecompressor(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;
public OutputStream createCompressor(OutputStream outputStream) throws IOException {
throw new UnsupportedOperationException("Not implemented yet");
}
public InputStream createDecompressor(InputStream inputStream) throws IOException {
throw new UnsupportedOperationException("Not implemented yet");
}
}

View File

@ -15,12 +15,12 @@ public class GZip extends AbstractAlgorithm {
@Getter private final String name = "GZip";
@Override
public OutputStream compress(OutputStream outputStream) throws IOException {
public OutputStream createCompressor(OutputStream outputStream) throws IOException {
return new GZIPOutputStream(outputStream);
}
@Override
public InputStream decompress(InputStream inputStream) throws IOException {
public InputStream createDecompressor(InputStream inputStream) throws IOException {
return new GZIPInputStream(inputStream);
}
}

View File

@ -0,0 +1,26 @@
package eu.mikroskeem.uurimustoo.algoritmidetest.algoritmid;
import lombok.Getter;
import net.jpountz.lz4.LZ4Compressor;
import net.jpountz.lz4.LZ4Factory;
import net.jpountz.lz4.LZ4SafeDecompressor;
/**
* @author Mark Vainomaa
*/
public class LZ4 extends AbstractAlgorithm {
@Getter private final String name = "LZ4";
private final LZ4Factory factory = LZ4Factory.fastestInstance();
@Override
public byte[] compress(byte[] input) {
LZ4Compressor compressor = factory.fastCompressor();
return compressor.compress(input);
}
@Override
public byte[] decompress(byte[] input) {
LZ4SafeDecompressor decompressor = factory.safeDecompressor();
return decompressor.decompress(input, 8192);
}
}

View File

@ -16,13 +16,13 @@ public class XZ extends AbstractAlgorithm {
@Getter private final String name = "XZ";
@Override
public OutputStream compress(OutputStream outputStream) throws IOException {
public OutputStream createCompressor(OutputStream outputStream) throws IOException {
LZMA2Options options = new LZMA2Options();
return new XZOutputStream(outputStream, options);
}
@Override
public InputStream decompress(InputStream inputStream) throws IOException {
public InputStream createDecompressor(InputStream inputStream) throws IOException {
return new XZInputStream(inputStream);
}
}

View File

@ -16,7 +16,7 @@ public class Zip extends AbstractAlgorithm {
@Getter private final String name = "ZIP";
@Override
public void compress0(InputStream inputStream, OutputStream outputStream) throws IOException {
public void compress(InputStream inputStream, OutputStream outputStream) throws IOException {
try(ZipOutputStream out = new ZipOutputStream(outputStream)){
ZipEntry ze = new ZipEntry("Test.class");
out.putNextEntry(ze);
@ -31,7 +31,7 @@ public class Zip extends AbstractAlgorithm {
}
@Override
public void decompress0(InputStream inputStream, OutputStream outputStream) throws IOException {
public void decompress(InputStream inputStream, OutputStream outputStream) throws IOException {
try(ZipInputStream in = new ZipInputStream(inputStream)){
ZipEntry ze = in.getNextEntry();
assert ze.getName().equals("Test.class");
@ -43,14 +43,4 @@ public class Zip extends AbstractAlgorithm {
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()");
}
}