

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class BitmapEncoderExamples {
    
    public static void main(String[] args) throws IOException {
    	// Encode 3 bytes into a bitmap file
    	BitmapEncoder.encodeToBitmap(new byte[] { 0x11, 0x22, 0x33 }, new File("/tmp/3-bytes.bmp"));
    	byte[] threebytes = BitmapEncoder.decodeFromBitmap(new File("/tmp/3-bytes.bmp"));
    	
    	System.out.println("EXAMPLE 1");
    	System.out.println("----");
    	System.out.println("threebytes = "+new BigInteger(1, threebytes).toString(16));
    	System.out.println("----");
    	System.out.println("");
    	
    	
    	// Encode file "/etc/vmlinuz" (about 4 MB) to "/tmp/vmlinuz.bmp" and restore it to "/tmp/vmlinuz-restored"
    	BitmapEncoder.encodeToBitmap(new File("/vmlinuz"), new File("/tmp/vmlinuz.bmp"));
    	BitmapEncoder.decodeFromBitmap(new File("/tmp/vmlinuz.bmp"), new File("/tmp/vmlinuz-restored"));
    	
    	System.out.println("EXAMPLE 2");
    	System.out.println("----");
    	System.out.println("/vmlinuz              "+new File("/vmlinuz").length()+" bytes");
    	System.out.println("/tmp/vmlinuz.bmp      "+new File("/tmp/vmlinuz.bmp").length()+" bytes");
    	System.out.println("/tmp/vmlinuz-restored "+new File("/tmp/vmlinuz-restored").length()+" bytes");
    	System.out.println("----");
    	System.out.println("");
    	
    	
    	// Encode file "/etc/motd" into file "/tmp/motd.bmp" and restore it to memory
    	BitmapEncoder.encodeToBitmap(new File("/etc/motd"), new File("/tmp/motd.bmp"));
    	String motdFileContent = new String(BitmapEncoder.decodeFromBitmap(new File("/tmp/motd.bmp")));
    	
    	System.out.println("EXAMPLE 3");
    	System.out.println("----");
    	System.out.println("/etc/motd              "+new File("/etc/motd").length()+" bytes");
    	System.out.println("/tmp/motd.bmp          "+new File("/tmp/motd.bmp").length()+" bytes");
    	System.out.println("");
    	System.out.println("Restored Contents:");
    	System.out.println(motdFileContent);
    	System.out.println("----");
    	System.out.println("");
    	
    	
    	// Encode file "/etc/motd" into file "/tmp/motd.bmp.gz", and gzip before writing
    	// Then restore and print it
    	BitmapEncoder.encodeToBitmap(
    			new FileInputStream(new File("/etc/hosts")),	new File("/etc/hosts").length(),
    			new GZIPOutputStream(new FileOutputStream("/tmp/hosts.bmp.gz")));
    	
    	String hostsFileContent = 
    			new String(BitmapEncoder.decodeFromBitmap(
    					new GZIPInputStream(new FileInputStream(new File("/tmp/hosts.bmp.gz")))));
    	
    	System.out.println("EXAMPLE 4");
    	System.out.println("----");    	
    	System.out.println("/etc/hosts              "+new File("/etc/hosts").length()+" bytes");
    	System.out.println("/tmp/hosts.bmp.gz       "+new File("/tmp/hosts.bmp.gz").length()+" bytes");
    	System.out.println("");
    	System.out.println("Restored Contents:");
    	System.out.println(hostsFileContent);
    	System.out.println("----");
    	System.out.println("");
    	
        
    }
}
