@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(expected = UnsupportedEncodingException.class) public void testUnsupportedEncodingBig5() throws URISyntaxException, IOException { final File testFileEncodingBig5 = new File(this.getClass().getResource("/test-file-empty.bin").toURI()); new ReversedLinesFileReader(testFileEncodingBig5, testParamBlockSize, "Big5").close(); }
/** * 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; }
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; }
@Test public void testUTF8FileCRBreaks() throws URISyntaxException, IOException { final File testFileIso = new File(this.getClass().getResource("/test-file-utf8-cr-only.bin").toURI()); reversedLinesFileReader = new ReversedLinesFileReader(testFileIso, testParamBlockSize, UTF_8); assertFileWithShrinkingTestLines(reversedLinesFileReader); }
@After public void closeReader() { try { bufferedReader.close(); } catch (final Exception e) { // ignore } try { reversedLinesFileReader.close(); } catch (final Exception e) { // ignore } }
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); } }
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]*\\.?[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)); if (m.group(2) != null) { // has occurrence for (int i = 0; i < Integer.parseInt(m.group(2)) - 1; i++) sequence.incrementOccurence(); } final double prob = Double.parseDouble(m.group(3)); sequences.put(sequence, prob); } break; } line = reader.readLine(); } reader.close(); return sequences; }
@Test public void testUTF8FileWindowsBreaksSmallBlockSize2VerifyBlockSpanningNewLines() throws URISyntaxException, IOException { final File testFileUtf8 = new File(this.getClass().getResource("/test-file-utf8-win-linebr.bin").toURI()); reversedLinesFileReader = new ReversedLinesFileReader(testFileUtf8, testParamBlockSize, UTF_8); assertFileWithShrinkingTestLines(reversedLinesFileReader); }
@After public void closeReader() { try { reversedLinesFileReader.close(); } catch (final Exception e) { // ignore } }
@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()); }
@Override public String readLine() throws IOException { return journal.readLine(); }
@Test(expected = UnsupportedEncodingException.class) public void testUnsupportedEncodingUTF16() throws URISyntaxException, IOException { final File testFileEmpty = new File(this.getClass().getResource("/test-file-empty.bin").toURI()); new ReversedLinesFileReader(testFileEmpty, testParamBlockSize, "UTF-16").close(); }
@Test public void testUTF8FileWindowsBreaks() throws URISyntaxException, IOException { final File testFileIso = new File(this.getClass().getResource("/test-file-utf8-win-linebr.bin").toURI()); reversedLinesFileReader = new ReversedLinesFileReader(testFileIso, testParamBlockSize, UTF_8); assertFileWithShrinkingTestLines(reversedLinesFileReader); }
@After public void closeReader() { try { reversedLinesFileReader.close(); } catch(final Exception e) { // ignore } }
@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()); }
/** * 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; }
@Test(expected=UnsupportedEncodingException.class) public void testUnsupportedEncodingUTF16() throws URISyntaxException, IOException { final File testFileEmpty = new File(this.getClass().getResource("/test-file-empty.bin").toURI()); new ReversedLinesFileReader(testFileEmpty, 4096, "UTF-16").close(); }
@Test public void testUTF16LEFile() throws URISyntaxException, IOException { final File testFileUTF16LE = new File(this.getClass().getResource("/test-file-utf16le.bin").toURI()); reversedLinesFileReader = new ReversedLinesFileReader(testFileUTF16LE, testParamBlockSize, "UTF-16LE"); assertFileWithShrinkingTestLines(reversedLinesFileReader); }
@Override public void close() throws IOException { journal.close(); } }