示例#1
0
 /**
  * compute a frequency table from an InputStream and save a compacted representation of this table
  * in the system preferences. (To be used later by @see #useStandardTable(String) )
  *
  * @param name name to give the table.
  * @param in InputStream
  * @return true on success
  * @throws Exception
  */
 public static boolean CreateStandardTableFromStream(String name, InputStream in)
     throws Exception {
   int[] tbl = new int[TABLESIZE];
   while (in.available() != 0) {
     int c = in.read();
     tbl[c]++;
   }
   byte[] dest = HuffmanTree.compactTable(tbl);
   Preferences pref = Preferences.userNodeForPackage(Huff.class);
   Preferences node = pref.node("StandardTables");
   node.putByteArray(name, dest);
   pref.flush();
   return true;
 }
示例#2
0
 /**
  * compress the source array. use rllchar as character to indicate repetition. if rllchar is 0x0,
  * the least frequent character from source will be used.
  *
  * @return a newly created array with the rll compressed data.
  */
 public static byte[] compress(byte rllchar, byte[] source) {
   if (rllchar == (byte) 0) {
     int[] freq = HuffmanTree.constructTable(source);
     int min = 100000;
     for (int i = 0; i < freq.length; i++) {
       if (freq[i] == 0) {
         rllchar = (byte) i;
         break;
       }
       if (freq[i] < min) {
         min = freq[i];
         rllchar = (byte) i;
       }
     }
   }
   ByteArrayOutputStream baos = new ByteArrayOutputStream(source.length);
   baos.write(rllchar);
   int i;
   for (i = 0; i < source.length - 3; i++) {
     if ((source[i] == source[i + 1]) && (source[i + 1] == source[i + 2])) {
       int rep = 3;
       while (((i + rep) < source.length) && source[i + rep] == source[i]) {
         if (rep == 254) break;
         rep++;
       }
       baos.write(rllchar);
       baos.write(rep);
       baos.write(source[i]);
       i += rep - 1;
     } else if (source[i] == rllchar) {
       baos.write(rllchar);
       baos.write(1);
     } else {
       baos.write(source[i]);
     }
   }
   while (i < source.length) {
     baos.write(source[i++]);
   }
   return baos.toByteArray();
 }
示例#3
0
 /**
  * Import a compaced frequency table from the system preferences (In windows from the registry
  * HKLM/JavaSoft/Prefs/ch/rgw/tools/Compress/StandardTables, in Linux from ~/.prefs)
  *
  * @param name Name of the table in the registry. If no table with this name is found, the default
  *     table (TextDeutsch) is returned.
  * @return the table
  */
 public static int[] useStandardTable(String name) {
   Preferences pr = Preferences.userNodeForPackage(Huff.class);
   Preferences node = pr.node("StandardTables");
   byte[] res = node.getByteArray(name, TextDeutsch);
   return HuffmanTree.expandTable(res);
 }