56 lines
1.9 KiB
Java
56 lines
1.9 KiB
Java
package eu.mikroskeem.uurimustoo.algoritmidetest.algoritmid;
|
|
|
|
import eu.mikroskeem.shuriken.common.SneakyThrow;
|
|
|
|
import java.io.*;
|
|
|
|
/**
|
|
* @author Mark Vainomaa
|
|
*/
|
|
public abstract class AbstractAlgorithm {
|
|
public abstract String getName();
|
|
|
|
public final byte[] compress(byte[] input){
|
|
try {
|
|
ByteArrayInputStream bais = new ByteArrayInputStream(input);
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
compress0(bais, baos);
|
|
return baos.toByteArray();
|
|
} catch (IOException e){
|
|
SneakyThrow.throwException(e);
|
|
}
|
|
return new byte[0]; // Never reaches here anyway
|
|
}
|
|
|
|
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 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;
|
|
}
|