2012-02-22 18 views
1

ファイル全体を読み込んでビットを書き込むファイルリーダーがあります。私はこれを呼び出すクラスでファイルのビットを読み込んで保存する

import java.io.*; 

public class FileReader extends ByteArrayInputStream{ 

    private int bitsRead; 
    private int bitPosition; 
    private int currentByte; 
    private int myMark; 
    private final static int NUM_BITS_IN_BYTE = 8; 
    private final static int END_POSITION = -1; 
    private boolean readingStarted; 
    /** 
    * Create a BitInputStream for a File on disk. 
    */ 
    public FileReader(byte[] buf) throws IOException { 
    super(buf); 

    myMark   = 0; 
    bitsRead  = 0; 
    bitPosition = NUM_BITS_IN_BYTE-1; 
    currentByte = 0; 
    readingStarted = false; 
    } 


    /** 
    * Read a binary "1" or "0" from the File. 
    */ 
    public int readBit() throws IOException { 
    int theBit = -1; 

    if(bitPosition == END_POSITION || !readingStarted) { 
     currentByte = super.read(); 
     bitPosition = NUM_BITS_IN_BYTE-1; 
     readingStarted = true; 
    } 

    theBit = (0x01 << bitPosition) & currentByte; 
    bitPosition--; 

    if(theBit > 0) { 
     theBit = 1; 
    } 

    return(theBit); 
    } 


    /** 
    * Return the next byte in the File as lowest 8 bits of int. 
    */ 
    public int read() { 
    currentByte = super.read(); 
    bitPosition = END_POSITION; 
    readingStarted = true; 

    return(currentByte); 
    } 


    /** 
    * 
    */ 
    public void mark(int readAheadLimit) { 
    super.mark(readAheadLimit); 
    myMark = bitPosition; 
    } 


    /** 
    * Add needed functionality to super's reset() method. Reset to 
    * the last valid position marked in the input stream. 
    */ 
    public void reset() { 
    super.pos = super.mark-1; 
    currentByte = super.read(); 
    bitPosition = myMark; 
    } 


    /** 
    * Returns the number of bits still available to be read. 
    */ 
    public int availableBits() throws IOException { 
    return( ((super.available() * 8) + (bitPosition + 1)) ); 
    } 

} 

が、私は:

FileInputStream inputStream = new FileInputStream(file); 

     byte[] fileBits = new byte[inputStream.available()]; 

     inputStream.read(fileBits, 0, inputStream.available()); 
     inputStream.close(); 

     FileReader bitIn = new FileReader(fileBits);  

、正しくこの作品 は私が読んで役立つ、このクラスを持っています。 しかし、私は100MB以上の大きなファイルには問題があります。なぜなら、バイト[]には終わりがあるからです。

私はより大きなファイルを読んでいます。おそらく、私はこのコードをどのように改善できるかを示唆するかもしれません。

ありがとうございました。

+1

)(利用でき、使用しないでください。 read()呼び出しの結果を無視してはいけません。 available()はファイルのサイズを返しません。そして、read()はすべてを一度に読むとは限りません。 –

+0

'java.io.Reader'を実装していない限り' FileReader'と呼ばないでください。また、 'java.io.FileReader'と衝突するので、それをしないでください。 –

+0

チップありがとうございました – Streetboy

答えて

0

ファイルサイズを大きくすることが重要な場合は、ファイル全体をメモリに読み込まないほうがよいでしょう。欠点は、より多くの場所でIOExceptionを処理することが少し面倒かもしれないということです。また、アプリケーションにInputStream APIを実装するものが必要であるとは思われません。readBit()メソッドが必要です。そのため、InputStreamを安全にカプセル化して拡張するのではなくカプセル化することができます。

class FileReader { 

    private final InputStream src; 

    private final byte[] bits = new byte[8192]; 

    private int len; 

    private int pos; 

    FileReader(InputStream src) { 
    this.src = src; 
    } 

    int readBit() throws IOException { 
    int idx = pos/8; 
    if (idx >= len) { 
     int n = src.read(bits); 
     if (n < 0) 
     return -1; 
     len = n; 
     pos = 0; 
     idx = 0; 
    } 
    return ((bits[idx] & (1 << (pos++ % 8))) == 0) ? 0 : 1; 
    } 

} 

使用法は類似しています。

FileInputStream src = new FileInputStream(file); 
try { 
    FileReader bitIn = new FileReader(src); 
    ... 
} finally { 
    src.close(); 
} 

実際にファイル全体を読み込み、実際のファイルで作業している場合は、最初にファイルの長さを問い合わせることができます。

File file = new File(path); 
if (file.length() > Integer.MAX_VALUE) 
    throw new IllegalArgumentException("File is too large: " + file.length()); 
int len = (int) file.length(); 
FileInputStream inputStream = new FileInputStream(file); 
try { 
    byte[] fileBits = new byte[len]; 
    for (int pos = 0; pos < len;) { 
    int n = inputStream.read(fileBits, pos, len - pos); 
    if (n < 0) 
     throw new EOFException(); 
    pos += n; 
    } 
    /* Use bits. */ 
    ... 
} finally { 
    inputStream.close(); 
} 
+0

上記のコードでは、int n = src.read(bits); 例外が発生します – Streetboy

0
org.apache.commons.io.IOUtils.copy(InputStream in, OutputStream out) 
関連する問題