Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.96 KB | None | 0 0
  1.   public static void code(String infile, String outfile) throws IOException  {
  2.     FileInputStream  bis  = new FileInputStream(infile);
  3.     FileOutputStream bos = new FileOutputStream(outfile);
  4.     PrintWriter      tos = new PrintWriter(new FileWriter(outfile+".txt"));
  5.     LzvCodeTable t = new LzvCodeTable();
  6.     int crt=bis.read();
  7.     byte [] prefix=new byte[0];
  8.     while(crt != -1) {
  9.       byte crtByte=(byte)crt;
  10.       byte[] newPrefix = new byte[prefix.length+1];
  11.  
  12.       for (int i = 0; i < prefix.length; i++) {
  13.         newPrefix[i]=prefix[i];
  14.       }
  15.       newPrefix[newPrefix.length-1] = crtByte;
  16.  
  17.       if( !t.contains(newPrefix) || bis.available() == 0 ){
  18.         int u = t.putAndCode(prefix,crtByte);
  19.         LzvItem item = new LzvItem(u,crtByte);
  20.         printItem(bos, tos, item);
  21.         prefix=new byte[0];
  22.       }else{
  23.         prefix = newPrefix;
  24.       }
  25.       crt = bis.read();
  26.  
  27.       // TODO - A COMPLETER
  28.       // ...
  29.       // ...  LzvItem item = new LzvItem(...
  30.       // ...  printItem(bos, tos, item);
  31.       // ...
  32.       // Remember there are two cases where EOF can happen:
  33.       // - just after a fresh item;
  34.       // - with a current word that forms a known prefix; in this case
  35.       //   we will add again this word in the code table.
  36.     }
  37.     bis.close();
  38.     bos.close();
  39.     tos.close();
  40.     System.out.println("Code table: size = "+t.size());
  41.     if (t.size()<10) {
  42.       System.out.println("Table Content:");
  43.       System.out.println(t);
  44.     }
  45.   }
  46.  
  47.   private static void printItemAsBinary(FileOutputStream bos, LzvItem item) throws IOException {
  48.     byte b1 = (byte)(item.entryNb>>8);
  49.     bos.write(b1);
  50.     byte b2 = (byte)item.entryNb;
  51.     bos.write(b2);
  52.     bos.write(item.appendedChar);
  53.   }
  54.  
  55.   private static LzvItem readItemFromBinary(FileInputStream bis) throws IOException {
  56.     int u = bis.read();
  57.     if(u==-1)return null;
  58.     u = u<<8;
  59.     u += bis.read();
  60.     return new LzvItem(u,(byte)bis.read());
  61.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement