Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void code(String infile, String outfile) throws IOException {
- FileInputStream bis = new FileInputStream(infile);
- FileOutputStream bos = new FileOutputStream(outfile);
- PrintWriter tos = new PrintWriter(new FileWriter(outfile+".txt"));
- LzvCodeTable t = new LzvCodeTable();
- int crt=bis.read();
- byte [] prefix=new byte[0];
- while(crt != -1) {
- byte crtByte=(byte)crt;
- byte[] newPrefix = new byte[prefix.length+1];
- for (int i = 0; i < prefix.length; i++) {
- newPrefix[i]=prefix[i];
- }
- newPrefix[newPrefix.length-1] = crtByte;
- if( !t.contains(newPrefix) || bis.available() == 0 ){
- int u = t.putAndCode(prefix,crtByte);
- LzvItem item = new LzvItem(u,crtByte);
- printItem(bos, tos, item);
- prefix=new byte[0];
- }else{
- prefix = newPrefix;
- }
- crt = bis.read();
- // TODO - A COMPLETER
- // ...
- // ... LzvItem item = new LzvItem(...
- // ... printItem(bos, tos, item);
- // ...
- // Remember there are two cases where EOF can happen:
- // - just after a fresh item;
- // - with a current word that forms a known prefix; in this case
- // we will add again this word in the code table.
- }
- bis.close();
- bos.close();
- tos.close();
- System.out.println("Code table: size = "+t.size());
- if (t.size()<10) {
- System.out.println("Table Content:");
- System.out.println(t);
- }
- }
- private static void printItemAsBinary(FileOutputStream bos, LzvItem item) throws IOException {
- byte b1 = (byte)(item.entryNb>>8);
- bos.write(b1);
- byte b2 = (byte)item.entryNb;
- bos.write(b2);
- bos.write(item.appendedChar);
- }
- private static LzvItem readItemFromBinary(FileInputStream bis) throws IOException {
- int u = bis.read();
- if(u==-1)return null;
- u = u<<8;
- u += bis.read();
- return new LzvItem(u,(byte)bis.read());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement