Java: Javolution: Как правильно использовать UTF8StreamReader?Произошла ошибка. Причина: java.lang.ArrayIndexOutOfBoundsException: 2048 - PullRequest
0 голосов
/ 20 июня 2011

Вот код:

public static void mergeAllFilesJavolution()throws FileNotFoundException, IOException {
    String fileDir = "C:\\TestData\\w12";
    File dirSrc = new File(fileDir);
    File[] list = dirSrc.listFiles();
    long start = System.currentTimeMillis();
    for(int j=0; j<list.length; j++){
        int chr;
        String srcFile = list[j].getPath();
        String outFile = fileDir + "\\..\\merged.txt";
        UTF8StreamReader inFile=new UTF8StreamReader().setInput(new FileInputStream(srcFile));
        UTF8StreamWriter outPut=new UTF8StreamWriter().setOutput(new FileOutputStream(outFile, true)); 
        while((chr=inFile.read()) != -1) {
            outPut.write(chr);
        }
        outPut.close();
        inFile.close();
    }
    System.out.println(System.currentTimeMillis()-start);
}

Размер файла utf-8 составляет 200 МБ в качестве тестовых данных, но высокая вероятность увеличения до 800 МБ .

Вот исходный код UTF8StreamReader.read ().

/**
 * Holds the bytes buffer.
 */
private final byte[] _bytes;

/**
 * Creates a UTF-8 reader having a byte buffer of moderate capacity (2048).
 */
public UTF8StreamReader() {
    _bytes = new byte[2048];
}

/**
 * Reads a single character.  This method will block until a character is
 * available, an I/O error occurs or the end of the stream is reached.
 *
 * @return the 31-bits Unicode of the character read, or -1 if the end of
 *         the stream has been reached.
 * @throws IOException if an I/O error occurs.
 */
public int read() throws IOException {
    byte b = _bytes[_start];
    return ((b >= 0) && (_start++ < _end)) ? b : read2();
}

Ошибка возникает при _байтах [_start] , поскольку _bytes = новый байт [2048].

Вот еще один конструктор UTF8StreamReader:

/**
 * Creates a UTF-8 reader having a byte buffer of specified capacity.
 * 
 * @param capacity the capacity of the byte buffer.
 */
public UTF8StreamReader(int capacity) {
    _bytes = new byte[capacity];
}

Проблема: Как можно указать правильную емкость _bytes при создании UTF8StreamReader?

Я попробовал File.length () , но он возвращает тип long (я думаю, это правильно, потому что я ожидаю огромный размер файла, но конструктор получает только тип int).

Любые указания в правильном направлении приветствуются.

1 Ответ

0 голосов
/ 30 июня 2011

Похоже, никто еще не испытывал того же в вышеуказанной ситуации.

В любом случае, я попробовал другое решение, не используя вышеуказанный класс (UTF8StreamReader), а ByteBuffer (UTF8ByteBufferReader).Это невероятно быстрее, чем StreamReader.

Ускоренное объединение файлов с помощью ByteBuffer

...