Advertisement
HwapX

RLE v2

Jun 14th, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.InputStream;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.nio.charset.Charset;
  5.  
  6. public class RLE{
  7.    
  8.     private static Byte[] stringToByteArray(String string) {
  9.         byte[] bytes  = string.getBytes(Charset.forName("ASCII"));
  10.         Byte[] result = new Byte[bytes.length];
  11.        
  12.         for(int i = 0; i < bytes.length; i++)
  13.             result[i] = bytes[i];
  14.            
  15.         return result;
  16.     }
  17.  
  18.     public static void main(String[] args){
  19.         if(args.length == 0)
  20.             throw new IllegalArgumentException("ร‰ necessรกrio informar a string a ser comprimida.");
  21.  
  22.         Byte[] data = stringToByteArray(args[0]);
  23.  
  24.         System.out.println("Entrada......: " + Arrays.toString(data));
  25.         data = compressRLE(data, 2);
  26.         System.out.println("Comprimido...: " + Arrays.toString(data));
  27.         System.out.println("Descomprimido: " + Arrays.toString(decompressRLE(data, 2)));
  28.     }
  29.    
  30.     private static Byte[] compressRLE(Byte[] data, int n) {
  31.         ArrayList<Byte> result = new ArrayList<Byte>();
  32.         int count = 0;
  33.         Byte last = 0;
  34.         for(Byte b : data) {
  35.             if(b != last || count == 0xFF + n) {
  36.                 writeRLE(last, count, n, result);
  37.                 last = b;
  38.                 count = 1;
  39.             } else {
  40.                 count++;
  41.             }
  42.         }
  43.        
  44.         writeRLE(last, count, n, result);
  45.        
  46.         return result.toArray(new Byte[result.size()]);
  47.     }
  48.    
  49.     private static Byte[] decompressRLE(Byte[] data, int n) {
  50.         ArrayList<Byte> result = new ArrayList<Byte>();
  51.         int count = 0;
  52.         Byte last = 0;
  53.         for(Byte b : data) {
  54.             if(count == n) {
  55.                 count = b + n;
  56.                 for(int i = 0; i < count; i++)
  57.                     result.add(last);
  58.                 count = 0;
  59.             } else if (b != last) {
  60.                 for(int i = 0; i < count; i++)
  61.                     result.add(last);
  62.                 last = b;
  63.                 count = 1;
  64.             } else {
  65.                 count++;
  66.             }
  67.         }
  68.        
  69.         return result.toArray(new Byte[result.size()]);
  70.     }
  71.    
  72.     private static void writeRLE(Byte b, int count, int n, ArrayList<Byte> result) {
  73.         for(int i = 0; i < Math.min(count, n); i++)
  74.             result.add(b);
  75.            
  76.         if(count >= n) {
  77.             byte c = (byte)(count-n);
  78.             result.add(c);
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement