Add more algorithms and ignore flush exceptions

This commit is contained in:
Mark Vainomaa 2017-03-26 19:05:30 +03:00
parent 79becd205e
commit 590f5f7d11
4 changed files with 67 additions and 2 deletions

View File

@ -82,6 +82,13 @@
<!-- ######## Compressors ######## -->
<!-- Apache compressor collection -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.13</version>
</dependency>
<!-- XZ -->
<dependency>
<groupId>org.tukaani</groupId>

View File

@ -38,7 +38,10 @@ public abstract class AbstractAlgorithm {
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();
try {
outputStream.flush();
}
catch (Throwable ignored) {} // Some compressors may not support flushing, like LZMA
}
}
@ -46,8 +49,11 @@ public abstract class AbstractAlgorithm {
try(InputStream is = createDecompressor(inputStream)) {
byte[] buf = new byte[8192];
int s; while ((s = is.read(buf)) != -1) outputStream.write(buf, 0, s);
try {
outputStream.flush();
}
catch (Throwable ignored) {} // Some compressors may not support flushing, like LZMA
}
}
public OutputStream createCompressor(OutputStream outputStream) throws IOException {

View File

@ -0,0 +1,26 @@
package eu.mikroskeem.uurimustoo.algoritmidetest.algoritmid;
import lombok.Getter;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* @author Mark Vainomaa
*/
public class BZip2 extends AbstractAlgorithm {
@Getter private final String name = "BZip2";
@Override
public OutputStream createCompressor(OutputStream outputStream) throws IOException {
return new BZip2CompressorOutputStream(outputStream);
}
@Override
public InputStream createDecompressor(InputStream inputStream) throws IOException {
return new BZip2CompressorInputStream(inputStream);
}
}

View File

@ -0,0 +1,26 @@
package eu.mikroskeem.uurimustoo.algoritmidetest.algoritmid;
import lombok.Getter;
import org.apache.commons.compress.compressors.lzma.LZMACompressorInputStream;
import org.apache.commons.compress.compressors.lzma.LZMACompressorOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* @author Mark Vainomaa
*/
public class LZMA extends AbstractAlgorithm {
@Getter private final String name = "LZMA";
@Override
public OutputStream createCompressor(OutputStream outputStream) throws IOException {
return new LZMACompressorOutputStream(outputStream);
}
@Override
public InputStream createDecompressor(InputStream inputStream) throws IOException {
return new LZMACompressorInputStream(inputStream);
}
}