2017-04-20 31 views
0

我目前正在使用霍夫曼樹來壓縮/解壓縮文本文件。目前我的問題是,當寫字節和閱讀他們,我失去了我的數字中的任何前導0。Integer.toBinaryString()失去領先0的

在我的OutputStream類中,我的writeBit()方法中,每次輸入一位,當我的位數達到8時,我將該字節寫入文件。目前使用字符串來構建這個二進制數,儘管實際寫入該位時會發生問題。

HuffmanOutputStream.java:

/** 
* Created by Sully on 3/20/2017. 
*/ 

import java.io.IOException; 


public class HuffmanOutputStream extends BitOutputStream { 

private int count = 0; 
private String bytes = ""; 

public HuffmanOutputStream(String filename, String tree, int totalChars) { 
    super(filename); 
    try { 
     d.writeUTF(tree); 
     d.writeInt(totalChars); 
    } catch (IOException e) { 
    } 
} 

public void writeBit(int bit) { 
    //PRE bit == 0 || bit == 1 
    if (count < 8) { 
     bytes += bit; 
     count++; 
    } 
    try { 

     if (count == 8) { 
      d.writeByte(Integer.parseInt(bytes, 2)); 
      count = 0; 
      bytes = ""; 
     } 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 


public void close() { 

} 
} 

出問題時的一個例子,我的文本文件,我構建的第一個字節是01100001,雖然當我使用的Integer.parseInt(字節,2)給出的整數是97,當它作爲二進制數讀取時,只返回1100001.由於霍夫曼樹依賴於包含這些0,我怎樣才能保持這個0?還要確保在0保持正確的情況下正確讀取它?

HuffmanInputStream.java:

/** 
* Created by Sully on 3/20/2017. 
*/ 

import java.io.IOException; 

public class HuffmanInputStream extends BitInputStream { 
    private String tree; 
    private int totalChars; 

    private int currentByte; 
    private int bitCount; 
    private static final int BYTE_SIZE = 8; 
    private int[] bufferedBits = new int[BYTE_SIZE]; 


public HuffmanInputStream(String filename) { 
    super(filename); 

    try { 
     tree = d.readUTF(); 
     totalChars = d.readInt(); 
     currentByte = 0; 
     bitCount = 8; 

    } catch (IOException e) { 
    } 
} 


public int readBit() { 

    if (currentByte == -1) { 
     return -1; 
    } 


    if (bitCount == 8) { 
     try { 
      currentByte = d.read(); 
      if(currentByte == -1){ 
       return -1; 
      } 
      String binary = Integer.toBinaryString(currentByte); 
      for (int x = 0; x < binary.length(); x++) { 
       bufferedBits[x] = Character.valueOf(binary.charAt(x)); 
      } 
      bitCount = 0; 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } 

    int val = bufferedBits[bitCount]; 

    bitCount++; 

    return val % 2; 


} 

public String getTree() { 
    return tree; 
} 

public int totalChars() { 
    return totalChars; 
} 

public void close() { 
    try { 
     d.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 
} 

我知道這是一個有點冗長的一個問題,但任何幫助是極大的讚賞!

+0

這聽起來像你的問題只是用'Integer.toBinaryString'。 –

+0

@LouisWasserman你介意解釋這個問題可能是什麼?我只是想明白爲什麼我會失去這些0。我需要做某種填充/位移嗎? –

+1

這與數字或填充或位移有關,以及與'Integer.toBinaryString'選擇相關的所有操作都可以用作從'int'轉換爲0和1序列的方式。 'Integer.toBinaryString'根本不會生成任何前導零。 –

回答

2

我假設你希望有足夠的領先0 s來使String的長度從Integer#toBinaryString 8返回;下面的代碼將爲你實現這個:

String binary = String.format("%8s", Integer.toBinaryString(currentByte)).replace(' ', '0'); 
+0

非常感謝!在您回答時,我發現了另一種類似於此權利的解決方案再次感謝。 –

+0

不客氣! –