Guest User

java encoding sniffer

a guest
Apr 4th, 2012
7
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.76 KB | None | 0 0
  1. package my;
  2.  
  3. import java.io.File;
  4.  
  5. public class Encoding {
  6.    
  7.      /**************************************************************
  8.      * http://en.wikipedia.org/wiki/Cp1252#Codepage_layout         *
  9.      * http://en.wikipedia.org/wiki/ISO/IEC_8859-1#Codepage_layout *
  10.      * http://en.wikipedia.org/wiki/Utf-8#Codepage_layout          *
  11.      **************************************************************/
  12.    
  13.      /***********************************************************************************
  14.      *  UTF-8 patterns                                                                  *
  15.      *  http://triptico.com/docs/unicode.html                                           *
  16.      *  U+00000000 - U+0000007F: 0xxxxxxx                                               *
  17.      *  U+00000080 - U+000007FF: 110xxxxx 10xxxxxx                                      *
  18.      *  U+00000800 - U+0000FFFF: 1110xxxx 10xxxxxx 10xxxxxx                             *
  19.      *  U+00010000 - U+001FFFFF: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx                    *
  20.      *  U+00200000 - U+03FFFFFF: 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx           *
  21.      *  U+04000000 - U+7FFFFFFF: 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx  *
  22.      ***********************************************************************************/
  23.    
  24.     /**
  25.      * Nota bene: byte e' signed, quindi per caratteri > 128 considerare che il valore sara' negativo
  26.      */
  27.    
  28.     public static void main(String[] args) throws Exception {
  29.         String[] files = new File("input").list();
  30.         for (int i=0;i<files.length;i++) {
  31.             String fileName = "input/" + files[i];
  32.            
  33.             System.out.println("fileName: " + fileName);
  34.             System.out.println("encoding: "+guessEncoding(IO.readFile(fileName)));
  35.         }
  36.     }
  37.     // java coi byte signed nun se po vede
  38.     public static String guessEncoding(byte[] blob) {
  39.         if (blob.length > 2) {
  40.             if (blob[0] == (byte)0xEF && blob[1] == (byte)0xBB && blob[2] == (byte)0xBF) {
  41.                 // bom utf-8 presente
  42.                 return "UTF-8";
  43.             }
  44.         }
  45.         boolean excludeCp1252 = false;
  46.         boolean excludeLatin1 = false;
  47.         boolean validUtf8Sequence = false;
  48.         boolean isUtf8Sequence = false;
  49.         boolean containsUtf8Sequences = false;
  50.         boolean latin1GrayArea = false;
  51.        
  52.         for (int i = 0; i < blob.length; i++) {
  53.             byte b = blob[i];
  54.            
  55.             if (b == (byte)0x7F || b >= (byte)0x80 && b <= (byte)0xA0) {
  56.                 // non e' latin1
  57.                 excludeLatin1 = true;
  58.             }
  59.             if (b == (byte)0x81 || b == (byte)0x8D || b == (byte)0x8F || b == (byte)0x90 || b == (byte)0x9D) {
  60.                 // non e' cp1252
  61.                 excludeCp1252 = true;
  62.             }
  63.            
  64.             if (b >= B10000000 && b <= B10111111) {
  65.                 latin1GrayArea = true;
  66.             }
  67.             isUtf8Sequence = false;
  68.             validUtf8Sequence = false;
  69.             if (b >= B11000000 && b <= B11011111) {
  70.                 // utf8: U+00000080 - U+000007FF
  71.                 isUtf8Sequence = true;
  72.                 validUtf8Sequence = i < blob.length - 1
  73.                                     && blob[i+1] >= B10000000 && blob[i+1] <= B10111111;
  74.                 if (validUtf8Sequence) i+=1;
  75.             } else if (b >= B11100000 && b <= B11101111) {
  76.                 // utf8: U+00000800 - U+0000FFFF
  77.                 isUtf8Sequence = true;
  78.                 validUtf8Sequence = i < blob.length - 2
  79.                                     && blob[i+1] >= B10000000 && blob[i+1] <= B10111111
  80.                                     && blob[i+2] >= B10000000 && blob[i+2] <= B10111111;
  81.                 if (validUtf8Sequence) i+=2;
  82.             } else if (b >= B11110000 && b <= B11110111) {
  83.                 // utf-8: U+00010000 - U+001FFFFF
  84.                 isUtf8Sequence = true;
  85.                 validUtf8Sequence = i < blob.length - 3
  86.                                     && blob[i+1] >= B10000000 && blob[i+1] <= B10111111
  87.                                     && blob[i+2] >= B10000000 && blob[i+2] <= B10111111
  88.                                     && blob[i+3] >= B10000000 && blob[i+3] <= B10111111;
  89.                 if (validUtf8Sequence) i+=3;
  90.             } else if (b >= B11111000 && b <= B11111011) {
  91.                 // utf-8: U+00200000 - U+03FFFFFF
  92.                 isUtf8Sequence = true;
  93.                 validUtf8Sequence = i < blob.length - 4
  94.                                     && blob[i+1] >= B10000000 && blob[i+1] <= B10111111
  95.                                     && blob[i+2] >= B10000000 && blob[i+2] <= B10111111
  96.                                     && blob[i+3] >= B10000000 && blob[i+3] <= B10111111
  97.                                     && blob[i+4] >= B10000000 && blob[i+4] <= B10111111;
  98.                 if (validUtf8Sequence) i+=4;
  99.             } else if (b == B11111100 || b == B11111101) {
  100.                 // utf-8: U+04000000 - U+7FFFFFFF
  101.                 isUtf8Sequence = true;
  102.                 validUtf8Sequence = i < blob.length - 5
  103.                                     && blob[i+1] >= B10000000 && blob[i+1] <= B10111111
  104.                                     && blob[i+2] >= B10000000 && blob[i+2] <= B10111111
  105.                                     && blob[i+3] >= B10000000 && blob[i+3] <= B10111111
  106.                                     && blob[i+4] >= B10000000 && blob[i+4] <= B10111111
  107.                                     && blob[i+5] >= B10000000 && blob[i+5] <= B10111111;
  108.                 if (validUtf8Sequence) i+=5;
  109.             }
  110.             if (excludeCp1252 && excludeLatin1) {
  111.                 if (isUtf8Sequence && !validUtf8Sequence) {
  112.                     return null;
  113.                 }
  114.             }
  115.             containsUtf8Sequences = containsUtf8Sequences || (isUtf8Sequence && validUtf8Sequence);
  116.         }
  117.        
  118.         if (excludeCp1252 && !containsUtf8Sequences) {
  119.             return "latin1";
  120.         }
  121.         if (excludeLatin1 && !containsUtf8Sequences) {
  122.             return "cp1252";
  123.         }
  124.         if (!excludeLatin1 && !excludeCp1252 && latin1GrayArea && !containsUtf8Sequences) {
  125.             return "latin1";
  126.         }
  127.         return "UTF-8";
  128.     }
  129.    
  130.     private static byte B11000000 = fromBin("11000000");
  131.     private static byte B11011111 = fromBin("11011111");
  132.    
  133.     private static byte B11100000 = fromBin("11100000");
  134.     private static byte B11101111 = fromBin("11101111");
  135.    
  136.     private static byte B11110000 = fromBin("11110000");
  137.     private static byte B11110111 = fromBin("11110111");
  138.    
  139.     private static byte B11111000 = fromBin("11111000");
  140.     private static byte B11111011 = fromBin("11111011");
  141.    
  142.     private static byte B11111100 = fromBin("11111100");
  143.     private static byte B11111101 = fromBin("11111101");
  144.    
  145.     private static byte B10000000 = fromBin("10000000");
  146.     private static byte B10111111 = fromBin("10111111");
  147.  
  148.     public static byte fromBin(String bin) {
  149.         return (byte) Integer.parseInt(bin, 2);
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment