[java]Jakarta IO の底力

単純なファイル読み込みを行う場合、FileInputStream, InputFileReader, BufferReader
のコンボを使うよりも、LineIteratorを使う方が高速ですよ、という話。
Jakarta : 266ms
Standard : 343ms
Channel : 3375ms
filesize : 1,331,881 bytes
Java 1.6.0.2

/**
 * jakarta.ioを用いたファイル読込み
 * @param filePath
 * @return ファイル内容
 */
public List<String> jakartaReader(String filePath, String encode) throws IOException{
    List<String> fileData = new ArrayList<String>();
    LineIterator iterator = FileUtils.lineIterator(new File(filePath), encode);
    try{
        while(iterator.hasNext()){
            fileData.add(iterator.nextLine());
        }
    } finally {
        LineIterator.closeQuietly(iterator);
    }
    return fileData;
}
/**
 * 一般的なファイル読込み
 * @param filePath ファイルパス
 * @return ファイル内容
 */
public List<String> standardReader(String filePath, String encode) throws IOException{
    List<String> fileData = new ArrayList<String>();
    FileInputStream fs = null;
    InputStreamReader sr = null;
    BufferedReader br = null;
    try{
        fs = new FileInputStream(filePath);
        sr = new InputStreamReader(fs, encode);
        br = new BufferedReader(sr);
        String line = null;
        while((line = br.readLine()) != null){
            fileData.add(line);
        }
    }finally{
        if(br != null) try { br.close(); } catch (IOException e){}
        if(sr != null) try { sr.close(); } catch (IOException e){}
        if(fs != null) try { fs.close(); } catch (IOException e){}
    }
    return fileData;
}
/**
 * channel経由のファイル読込み
 * @param filePath
 * @return ファイル内容
 */
public List<String> channelReader(String filePath, String encode) throws IOException{
    List<String> fileData = new ArrayList<String>();
    int bufSize = 65535;
    FileInputStream fs = null;
    try{
        fs = new FileInputStream(filePath);
        FileChannel channel = fs.getChannel();
        long size = channel.size();
        if(size < bufSize) bufSize = (int)size;
        ByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
        byte[] bytes = new byte[bufSize];
        boolean chCR = false;
        int lastSize = (int)(size % bufSize);
        read : for(long pos = size; pos > 0; pos -= bufSize){
            if(pos <= lastSize) bytes = new byte[lastSize];
            buffer.get(bytes);
            String temp = new String(bytes, encode);
            int start = 0;
            for(int i=0; i<temp.length(); i++){
                char c = temp.charAt(i);
                if(c == '\r'){ // CR
                    fileData.add(temp.substring(start, i));
                    chCR = true;
                } else if(c == '\n'){ // LF
                    start = i + 1;
                    if(chCR){
                        chCR = false;
                    } else {
                        fileData.add(temp.substring(start, i));
                    }
                } else if(c == '\0'){ // ファイル終了
                    fileData.add(temp.substring(start, i));
                    break read;
                } else {
                    chCR = false;
                }
            }
            size -= bufSize;
        }
    } finally {
        if(fs != null) try{ fs.close(); } catch (IOException e){}
    }
    return fileData;
}