Beispiel #1
1
 /**
  * Construct a frequency table from an InputStream. This will create a temporary file to copy the
  * InputStream in.
  *
  * @param source the Input Stream
  * @return an InputStream which is a copy of the source Stream, provided to re-Read the same Bytes
  *     for the actual compression process.
  */
 public InputStream constructTable(InputStream source, boolean copy) {
   freq = new int[TABLESIZE];
   try {
     File file = null;
     FileOutputStream fos = null;
     if (copy == true) {
       file = File.createTempFile("huf", "tmp");
       file.deleteOnExit();
       fos = new FileOutputStream(file);
     }
     while (source.available() != 0) {
       int c = source.read();
       freq[c]++;
       if (copy) fos.write(c);
     }
     source.close();
     if (copy) {
       fos.close();
       return new FileInputStream(file);
     }
     return null;
   } catch (Exception ex) {
     ExHandler.handle(ex);
     return null;
   }
 }
Beispiel #2
0
 /**
  * Reloads a frequency table as saved by saveTable
  *
  * @return the table
  */
 public static int[] loadTable(InputStream in) {
   try {
     int l = in.read();
     l |= (in.read() << 8);
     byte[] tbl = new byte[l];
     for (int i = 0; i < tbl.length; i++) {
       tbl[i] = (byte) in.read();
     }
     return expandTable(tbl);
   } catch (Exception ex) {
     ExHandler.handle(ex);
     return null;
   }
 }
Beispiel #3
0
 /**
  * Creates a compacted form of the actual frequency table and saves it into an OutputStream.
  *
  * @return true on success
  */
 public boolean saveTable(OutputStream out) {
   byte[] tbl = compactTable(freq);
   try {
     short l = (short) tbl.length;
     out.write(l & 0xff);
     out.write(l >> 8);
     for (int i = 0; i < tbl.length; i++) {
       out.write(tbl[i]);
     }
     return true;
   } catch (Exception ex) {
     ExHandler.handle(ex);
     return false;
   }
 }
Beispiel #4
0
 /** constructs a frequency table from a file */
 public static int[] constructTable(RandomAccessFile file) {
   int[] ret = new int[TABLESIZE];
   try {
     file.seek(0L);
     long l = file.length();
     for (long i = 0; i < l; i++) {
       int c = file.read();
       ret[c]++;
     }
     return ret;
   } catch (Exception ex) {
     ExHandler.handle(ex);
     return null;
   }
 }