/** * Returns the lines of the file from bottom to top. * * @return the next line or null if the start of the file is reached * @throws IOException if an I/O error occurs */ public String readLine() throws IOException { String line = currentFilePart.readLine(); while (line == null) { currentFilePart = currentFilePart.rollOver(); if (currentFilePart != null) { line = currentFilePart.readLine(); } else { // no more fileparts: we're done, leave line set to null break; } } // aligned behaviour with BufferedReader that doesn't return a last, empty line if("".equals(line) && !trailingNewlineOfFileSkipped) { trailingNewlineOfFileSkipped = true; line = readLine(); } return line; }
private void assertFileWithShrinkingTestLines(final ReversedLinesFileReader reversedLinesFileReader) throws IOException { String line = null; int lineCount = 0; while ((line = reversedLinesFileReader.readLine()) != null) { lineCount++; assertEqualsAndNoLineBreaks("Line " + lineCount + " is not matching", TEST_LINE.substring(0, lineCount), line); } }
@Test public void testShiftJISFile() throws URISyntaxException, IOException { final File testFileShiftJIS = new File(this.getClass().getResource("/test-file-shiftjis.bin").toURI()); reversedLinesFileReader = new ReversedLinesFileReader(testFileShiftJIS, testParamBlockSize, "Shift_JIS"); assertEqualsAndNoLineBreaks(TEST_LINE_SHIFT_JIS2, reversedLinesFileReader.readLine()); assertEqualsAndNoLineBreaks(TEST_LINE_SHIFT_JIS1, reversedLinesFileReader.readLine()); }
@Test public void testGBK() throws URISyntaxException, IOException { final File testFileGBK = new File(this.getClass().getResource("/test-file-gbk.bin").toURI()); reversedLinesFileReader = new ReversedLinesFileReader(testFileGBK, testParamBlockSize, "GBK"); assertEqualsAndNoLineBreaks(TEST_LINE_GBK_2, reversedLinesFileReader.readLine()); assertEqualsAndNoLineBreaks(TEST_LINE_GBK_1, reversedLinesFileReader.readLine()); }
@Test public void testWindows31jFile() throws URISyntaxException, IOException { final File testFileWindows31J = new File(this.getClass().getResource("/test-file-windows-31j.bin").toURI()); reversedLinesFileReader = new ReversedLinesFileReader(testFileWindows31J, testParamBlockSize, "windows-31j"); assertEqualsAndNoLineBreaks(TEST_LINE_WINDOWS_31J_2, reversedLinesFileReader.readLine()); assertEqualsAndNoLineBreaks(TEST_LINE_WINDOWS_31J_1, reversedLinesFileReader.readLine()); }
@Test public void testxWindows950File() throws URISyntaxException, IOException { final File testFilexWindows950 = new File(this.getClass().getResource("/test-file-x-windows-950.bin").toURI()); reversedLinesFileReader = new ReversedLinesFileReader(testFilexWindows950, testParamBlockSize, "x-windows-950"); assertEqualsAndNoLineBreaks(TEST_LINE_X_WINDOWS_950_2, reversedLinesFileReader.readLine()); assertEqualsAndNoLineBreaks(TEST_LINE_X_WINDOWS_950_1, reversedLinesFileReader.readLine()); }
@Test // this test is run 3x for same block size as we want to test with 10 public void testFileSizeIsExactMultipleOfBlockSize() throws URISyntaxException, IOException { final int blockSize = 10; final File testFile20Bytes = new File(this.getClass().getResource("/test-file-20byteslength.bin").toURI()); reversedLinesFileReader = new ReversedLinesFileReader(testFile20Bytes, blockSize, ISO_8859_1); final String testLine = "123456789"; assertEqualsAndNoLineBreaks(testLine, reversedLinesFileReader.readLine()); assertEqualsAndNoLineBreaks(testLine, reversedLinesFileReader.readLine()); }
@Test public void testFileSizeIsExactMultipleOfBlockSize() throws URISyntaxException, IOException { final int blockSize = 10; final File testFile20Bytes = new File(this.getClass().getResource("/test-file-20byteslength.bin").toURI()); reversedLinesFileReader = new ReversedLinesFileReader(testFile20Bytes, blockSize, "ISO-8859-1"); final String testLine = "123456789"; assertEqualsAndNoLineBreaks(testLine, reversedLinesFileReader.readLine()); assertEqualsAndNoLineBreaks(testLine, reversedLinesFileReader.readLine()); }
@Test public void testxWindows949File() throws URISyntaxException, IOException { final File testFilexWindows949 = new File(this.getClass().getResource("/test-file-x-windows-949.bin").toURI()); reversedLinesFileReader = new ReversedLinesFileReader(testFilexWindows949, testParamBlockSize, "x-windows-949"); assertEqualsAndNoLineBreaks(TEST_LINE_X_WINDOWS_949_2, reversedLinesFileReader.readLine()); assertEqualsAndNoLineBreaks(TEST_LINE_X_WINDOWS_949_1, reversedLinesFileReader.readLine()); }
@Test public void testIsoFileManyWindowsBreaksSmallBlockSize2VerifyBlockSpanningNewLines() throws URISyntaxException, IOException { final File testFileIso = new File(this.getClass().getResource("/test-file-iso8859-1-shortlines-win-linebr.bin").toURI()); reversedLinesFileReader = new ReversedLinesFileReader(testFileIso, testParamBlockSize, ISO_8859_1); for (int i = 3; i > 0; i--) { for (int j = 1; j <= 3; j++) { assertEqualsAndNoLineBreaks("", reversedLinesFileReader.readLine()); } assertEqualsAndNoLineBreaks("" + i, reversedLinesFileReader.readLine()); } }
@Test public void testDataIntegrityWithBufferedReader() throws URISyntaxException, IOException { final File testFileIso = new File(this.getClass().getResource("/" + fileName).toURI()); reversedLinesFileReader = new ReversedLinesFileReader(testFileIso, buffSize, encoding); final Stack<String> lineStack = new Stack<>(); bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(testFileIso), encoding)); String line = null; // read all lines in normal order while ((line = bufferedReader.readLine()) != null) { lineStack.push(line); } // read in reverse order and compare with lines from stack while ((line = reversedLinesFileReader.readLine()) != null) { final String lineFromBufferedReader = lineStack.pop(); assertEquals(lineFromBufferedReader, line); } }
@Test public void testEmptyFile() throws URISyntaxException, IOException { final File testFileEmpty = new File(this.getClass().getResource("/test-file-empty.bin").toURI()); reversedLinesFileReader = new ReversedLinesFileReader(testFileEmpty, testParamBlockSize, UTF_8); assertNull(reversedLinesFileReader.readLine()); }
@Override public String readLine() throws IOException { return journal.readLine(); }
/** * The main work of this method is done in moving the logReader to the point just before the last shutdown */ private int advanceReaderToShutdownLine(final ReversedLinesFileReader logReader, final String startIndicator, final int lineScanLimit) throws IOException { int lineCount = 0; String line; while(lineCount++ < lineScanLimit && (line = logReader.readLine()) != null) { if (line.contains(startIndicator)) { break; } } return lineCount; }
/** * Returns the lines of the file from bottom to top. * * @return the next line or null if the start of the file is reached * @throws IOException if an I/O error occurs */ public String readLine() throws IOException { String line = currentFilePart.readLine(); while (line == null) { currentFilePart = currentFilePart.rollOver(); if (currentFilePart != null) { line = currentFilePart.readLine(); } else { // no more fileparts: we're done, leave line set to null break; } } // aligned behaviour wiht BufferedReader that doesn't return a last, emtpy line if("".equals(line) && !trailingNewlineOfFileSkipped) { trailingNewlineOfFileSkipped = true; line = readLine(); } return line; }
/** * Returns the lines of the file from bottom to top. * * @return the next line or null if the start of the file is reached * @throws IOException if an I/O error occurs */ public String readLine() throws IOException { String line = currentFilePart.readLine(); while (line == null) { currentFilePart = currentFilePart.rollOver(); if (currentFilePart != null) { line = currentFilePart.readLine(); } else { // no more fileparts: we're done, leave line set to null break; } } // aligned behaviour wiht BufferedReader that doesn't return a last, emtpy line if("".equals(line) && !trailingNewlineOfFileSkipped) { trailingNewlineOfFileSkipped = true; line = readLine(); } return line; }
private Optional<Date> findTimeFromPreviousInstance(final ReversedLinesFileReader logReader, final Pattern timestampPattern, final int lineScanLimit, final String groupName, final DateFormat dateFormat) throws IOException, ParseException { int lineCount = 0; String line; while(lineCount++ < lineScanLimit && (line = logReader.readLine()) != null) { Matcher matcher = timestampPattern.matcher(line); if (matcher.find()) { return Optional.of(dateFormat.parse(matcher.group(groupName))); } } return Optional.empty(); } }
public static HashMap<Sequence, Double> readLastEMStepSequences(final File logFile) throws IOException { final HashMap<Sequence, Double> sequences = new HashMap<>(); final ReversedLinesFileReader reader = new ReversedLinesFileReader(logFile); String line = reader.readLine(); while (line != null) { if (line.contains("Parameter Optimal Sequences:")) { final Matcher m = Pattern .compile( "\\[((?:[0-9]|,| )+?)\\]=\\(((?:(?:[-+]?[0-9]*\\.?[0-9]+(?:[eE][-+]?[0-9]+)?)|,)+?)\\)") .matcher(line); while (m.find()) { final Sequence sequence = new Sequence(); final String[] items = m.group(1).split(", "); for (final String item : items) sequence.add(Integer.parseInt(item)); final double prob = 1 - Double.parseDouble(m.group(2).split(",")[0]); sequences.put(sequence, prob); } break; } line = reader.readLine(); } reader.close(); return sequences; }
/** * Create an iterator that reads a file line-by-line in reverse * @param rdf the RDF object * @param file the file * @param identifier the identifier * @param time the time */ public StreamReader(final RDF rdf, final File file, final IRI identifier, final Instant time) { this.rdf = rdf; this.time = time; this.identifier = identifier; try { this.reader = new ReversedLinesFileReader(file, UTF_8); this.line = reader.readLine(); } catch (final IOException ex) { throw new UncheckedIOException(ex); } bufferIter = readPatch(); }
@Override public String[] getLastLinesFromFile( String fileName, int numLinesToRead, Charset chartset ) { LinkedList<String> lastLinesList = new LinkedList<String>(); ReversedLinesFileReader reversedReader = null; try { reversedReader = new ReversedLinesFileReader(new File(fileName), 4096, chartset); while (lastLinesList.size() < numLinesToRead) { String line = reversedReader.readLine(); // check if the file has less lines than the wanted if (line != null) { lastLinesList.addFirst(line); } else { break; } } return lastLinesList.toArray(new String[lastLinesList.size()]); } catch (IOException ioe) { throw new FileSystemOperationException("Error reading file '" + fileName + "'", ioe); } finally { if (reversedReader != null) { IoUtils.closeStream(reversedReader); } } }