35 lines
1.0 KiB
Java
35 lines
1.0 KiB
Java
package eu.mikroskeem.uurimustoo.algoritmidetest.compressors;
|
|
|
|
import eu.mikroskeem.shuriken.common.SneakyThrow;
|
|
|
|
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 {
|
|
try(OutputStream out = pipe(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;
|
|
}
|