package com.intel.daal.examples.compression;
import java.io.RandomAccessFile;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.zip.CRC32;
import java.util.zip.Checksum;
import com.intel.daal.data_management.compression.CompressionLevel;
import com.intel.daal.data_management.compression.zlib.ZlibCompressor;
import com.intel.daal.data_management.compression.zlib.ZlibDecompressor;
import com.intel.daal.services.DaalContext;
class CompressorExample {
static Queue<byte[]> sendReceiveQueue = new LinkedList<byte[]>();
private static final long maxDataBlockSize = 16384;
static long compressedSize = 0;
static long receivedSize = 0;
static long availableDataSize = 0;
private static final String dataset = "../data/batch/logitboost_train.csv";
private static byte[] sentDataStream;
private static byte[] uncompressedDataBlock;
private static byte[] compressedDataBlock;
private static byte[] receivedDataStream;
private static DaalContext context = new DaalContext();
public static void main(String[] args) throws java.io.FileNotFoundException, java.io.IOException {
prepareMemory();
ZlibCompressor compressor = new ZlibCompressor(context);
compressor.parameter.setCompressionLevel(CompressionLevel.DefaultLevel);
compressor.parameter.setGzHeader(true);
while ((uncompressedDataBlock = getDataBlock()) != null) {
compressor.setInputDataBlock(uncompressedDataBlock);
do {
compressor.run(compressedDataBlock, maxDataBlockSize, 0);
compressedSize = compressor.getUsedOutputDataBlockSize();
sendDataBlock(compressedDataBlock, compressedSize);
}
while (compressor.isOutputDataBlockFull());
}
ZlibDecompressor decompressor = new ZlibDecompressor(context);
decompressor.parameter.setGzHeader(true);
while ((compressedDataBlock = receiveDataBlock()) != null) {
decompressor.setInputDataBlock(compressedDataBlock);
decompressor.run(receivedDataStream, maxDataBlockSize, receivedSize);
receivedSize += decompressor.getUsedOutputDataBlockSize();
}
printCRC32(sentDataStream, receivedDataStream);
context.dispose();
}
private static void prepareMemory() throws java.io.FileNotFoundException, java.io.IOException {
sentDataStream = readData();
compressedDataBlock = new byte[(int) maxDataBlockSize];
receivedDataStream = new byte[sentDataStream.length];
availableDataSize = sentDataStream.length;
}
private static byte[] readData() throws java.io.FileNotFoundException, java.io.IOException {
RandomAccessFile file = new RandomAccessFile(dataset, "r");
int dataLength = (int) file.length();
byte[] sentData = new byte[dataLength];
file.read(sentData);
file.close();
return sentData;
}
private static byte[] getDataBlock() {
long startPosition = sentDataStream.length - availableDataSize;
byte[] currentBlock;
if (availableDataSize >= maxDataBlockSize) {
currentBlock = Arrays.copyOfRange(sentDataStream, (int) startPosition,
(int) (startPosition + maxDataBlockSize));
availableDataSize -= maxDataBlockSize;
} else if ((availableDataSize < maxDataBlockSize) && (availableDataSize > 0)) {
currentBlock = Arrays.copyOfRange(sentDataStream, (int) startPosition, sentDataStream.length);
availableDataSize = 0;
} else {
return null;
}
return currentBlock;
}
private static void sendDataBlock(byte[] block, long size) {
byte[] currentBlock = Arrays.copyOf(block, (int) size);
sendReceiveQueue.add(currentBlock);
}
private static byte[] receiveDataBlock() {
byte[] currentBlock;
if ((currentBlock = sendReceiveQueue.poll()) == null) {
return null;
}
return currentBlock;
}
private static void printCRC32(byte[] sentData, byte[] receivedData) {
Checksum crcSentDataStream = new CRC32();
crcSentDataStream.update(sentData, 0, sentData.length);
Checksum crcReceivedDataStream = new CRC32();
crcReceivedDataStream.update(receivedData, 0, (int) receivedSize);
System.out.println("Compression example program results:");
System.out.println("Input data checksum: 0x" + Integer.toHexString((int) crcSentDataStream.getValue()));
System.out.println("Received data checksum: 0x" + Integer.toHexString((int) crcReceivedDataStream.getValue()));
if (sentData.length != receivedSize) {
System.out.println("ERROR: Received data size (" + receivedSize + ") mismatches with the sent data size ("
+ sentData.length + ")");
} else if (crcSentDataStream.getValue() != crcReceivedDataStream.getValue()) {
System.out.println("ERROR: Received data CRC mismatches with the sent data CRC");
} else {
System.out.println("OK: Received data CRC matches with the sent data CRC");
}
}
}