Advertisement
HwapX

RLE Java

Jun 8th, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.39 KB | None | 0 0
  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.         ArrayList<Byte> data = new ArrayList<Byte>(
  20.                                      Arrays.asList(StringToByteArray("www"))
  21.                                    );
  22.  
  23.         System.out.println("Entrada......: " + data);
  24.         data = compressRLE(data, 2);
  25.         System.out.println("Comprimido...: " + data);
  26.         System.out.println("Descomprimido: " + DecompressRLE(data, 2));
  27.     }
  28.    
  29.     private static ArrayList<Byte> compressRLE(ArrayList<Byte> data, int n) {
  30.         ArrayList<Byte> result = new ArrayList<Byte>();
  31.         int count = 0;
  32.         Byte last = 0;
  33.         for(Byte b : data) {
  34.             if(b != last || count == 0xFF + n) {
  35.                 writeRLE(last, count, n, result);
  36.                 last = b;
  37.                 count = 1;
  38.             } else {
  39.                 count++;
  40.             }
  41.         }
  42.        
  43.         writeRLE(last, count, n, result);
  44.        
  45.         return result;
  46.     }
  47.    
  48.     private static ArrayList<Byte> DecompressRLE(ArrayList<Byte> data, int n) {
  49.         ArrayList<Byte> result = new ArrayList<Byte>();
  50.         int count = 0;
  51.         Byte last = 0;
  52.         for(Byte b : data) {
  53.             if(count == n) {
  54.                 count = b + n;
  55.                 for(int i = 0; i < count; i++)
  56.                     result.add(last);
  57.                 count = 0;
  58.             } else if (b != last) {
  59.                 for(int i = 0; i < count; i++)
  60.                     result.add(last);
  61.                 last = b;
  62.                 count = 1;
  63.             } else {
  64.                 count++;
  65.             }
  66.         }
  67.        
  68.         return result;
  69.     }
  70.    
  71.     private static void writeRLE(Byte b, int count, int n, ArrayList<Byte> result) {
  72.         for(int i = 0; i < Math.min(count, n); i++)
  73.             result.add(b);
  74.            
  75.         if(count >= n) {
  76.             byte c = (byte)(count-n);
  77.             result.add(c);
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement