@Override public void setupDatasetProvider(File file, boolean testMode, ImportAttributes properties) throws Exception { if (properties.isIgnoreLineBreakInEnclosedValues()) { setEncloser(properties.getEnclosure()); } this.testMode = testMode; if (testMode) { randomReader = new RandomAccessReader(new RandomAccessFile(file, "r"), properties.getCharsetName()); if (useEnclosure) { randomReader.setEnclosure(enclosure); } else { randomReader.disableEnclosure(); } } else { bufferedReader = new BufferedReader( new InputStreamReader(new FileInputStream(file), properties.getCharsetName())); } currentFile = file; currentRowNum = 0; currentLine = null; }
@Override public void closeDatasetProvider() { if (randomReader != null) { try { randomReader.close(); } catch (IOException e) { e.printStackTrace(); } } if (bufferedReader != null) { try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } }
private void readDatasetInTestModeAt(long rowNumber) throws Exception { if (rowNumber < 0) { randomReader.seek(0); currentLine = null; randomReader.seek(0); currentRowNum = 0; } else if (rowNumber == 0) { randomReader.seek(0); currentLine = randomReader.readLineWithCharSet(); randomReader.seek(0); currentRowNum = 0; } else { if (rowNumber == currentRowNum - 1) { // step only one line back if (randomReader.seekToPrevLineStart()) { currentRowNum--; long pos = randomReader.getFilePointer(); currentLine = randomReader.readLineWithCharSet(); randomReader.seek(pos); } } else { // workaround because stepping backwards is to slow if (rowNumber < currentRowNum) { currentRowNum = 0; randomReader.seek(0); } if (rowNumber > currentRowNum) { boolean jumped = false; long pos = randomReader.getFilePointer(); while (rowNumber > currentRowNum) { if (randomReader.seekToNextLineStart()) { pos = randomReader.getFilePointer(); currentRowNum++; jumped = true; } else { break; } } if (jumped) { currentLine = randomReader.readLineWithCharSet(); randomReader.seek(pos); } } } } }
protected RandomAccessReader createReader(String path) { return RandomAccessReader.open(new File(path), this); }