/* ----------------------------------------
 * Origin  : GFI INFORMATIQUE 
 * Project : CDPP Propagation Tool 
 * ----------------------------------------
 * History 
 * Version : 01/05/2015 : SCA : V2.1
 * ----------------------------------------
 */
package fr.tlse.irap.map;

import java.io.ByteArrayOutputStream;
import java.util.Date;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

import jakarta.xml.bind.annotation.adapters.XmlAdapter;

public class FitsAdapter extends XmlAdapter<byte[], byte[]> {

	@Override
	public byte[] marshal(byte[] data) throws Exception {
		//System.out.println("Start Marshalling "+new Date());  
		Deflater deflater = new Deflater();  
		deflater.setInput(data);  

		ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);   

		deflater.finish();  
		byte[] buffer = new byte[1024];   
		while (!deflater.finished()) {  
			int count = deflater.deflate(buffer); // returns the generated code... index  
			outputStream.write(buffer, 0, count);   
		}  
		outputStream.close();  
		byte[] output = outputStream.toByteArray();  

		deflater.end();

		System.out.println("Original: " + data.length / 1024 + " Kb");  
		System.out.println("Compressed: " + output.length / 1024 + " Kb");  
		System.out.println("End Marshalling "+new Date());  
		return output;  		
	}

	@Override
	public byte[] unmarshal(byte[] data) throws Exception {
		//System.out.println("Start UnMarshalling "+new Date());  
		Inflater inflater = new Inflater();   
		inflater.setInput(data);  

		ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);  
		byte[] buffer = new byte[1024];  
		while (!inflater.finished()) {  
			int count = inflater.inflate(buffer);  
			outputStream.write(buffer, 0, count);  
		}  
		outputStream.close();  
		byte[] output = outputStream.toByteArray();  

		inflater.end();

		System.out.println("Original: " + data.length  / 1024 + " Kb");  
		System.out.println("Uncompressed: " + output.length  / 1024 + " Kb");  
		System.out.println("End UnMarshalling "+new Date());  
		return output;  
	}

}
