package com.atolsystems.memop;
import com.atolsystems.atolutilities.AStringUtilities;
import com.atolsystems.atolutilities.ATimeUtilities;
import com.atolsystems.atolutilities.ProgrammingChunk;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.Deflater;
/**
*
* @author sebastien.riou
*/
public class CompressionTest {
//final static boolean DEBUG = true;
PrintStream ps;
/*public static void main(String[] args) {
CompressionTest nl = new CompressionTest(System.out);
nl.compressionTest((List<ProgrammingChunk>) null);
}*/
CompressionTest(PrintStream ps) {
this.ps = ps;
}
//final static String ZEROES = "00000000000000000000000000000000";
void compressionTest(List<ProgrammingChunk> image) {
ps.println("Compression test report");
ps.println("Start time:" + ATimeUtilities.getTimeForUi());
ps.println();
byte[] input = ProgrammingChunk.list2Bytes(image);
compressImage(input);
ps.println("End of report:" + ATimeUtilities.getTimeForUi());
}
void compressionTest(byte[] plainImage, List<ProgrammingChunk> encryptedImage) {
ps.println("Compression test report");
ps.println("Start time:" + ATimeUtilities.getTimeForUi());
ps.println();
byte[] output = ProgrammingChunk.list2Bytes(encryptedImage);
compressImage(output);
byte[] input = plainImage;
byte[] both=new byte[input.length+output.length];
System.arraycopy(input, 0, both, 0, input.length);
System.arraycopy(output, 0, both, input.length, output.length);
ps.println("Compress \"image+encrypted image\"");
compressImage(both);
ps.println("End of report:" + ATimeUtilities.getTimeForUi());
}
byte[] compressImage(byte[] input) {
// Compressor with highest level of compression
Deflater compressor = new Deflater();
compressor.setLevel(Deflater.BEST_COMPRESSION);
// Give the compressor the data to compress
compressor.setInput(input);
compressor.finish();
// Create an expandable byte array to hold the compressed data.
// It is not necessary that the compressed data will be smaller than
// the uncompressed data.
ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
// Compress the data
byte[] buf = new byte[1024];
while (!compressor.finished()) {
int count = compressor.deflate(buf);
bos.write(buf, 0, count);
}
try {
bos.close();
} catch (IOException e) {
}
// Get the compressed data
byte[] compressedData = bos.toByteArray();
float cl = input.length;
cl = (cl / (float) (compressedData.length))-1;
cl = cl * 100;
ps.println("Input data: ("+input.length+" bytes)");
ps.println(AStringUtilities.bytesToHex(input));
ps.println();
ps.println("Compressed data:("+compressedData.length+" bytes)");
ps.println(AStringUtilities.bytesToHex(compressedData));
ps.println();
ps.println("Compression level: " + cl + "%");
ps.println();
return compressedData;
}
}