### Question:
LineReader { public String readLine(Reader reader) throws IOException { char curChar; int val; StringBuilder sb = new StringBuilder(40); while ((val = reader.read()) != -1) { curChar = (char) val; if (curChar == '\n' && lineSeparator.length == 1 && curChar == lineSeparator[0]) { return sb.toString(); } else if (curChar == '\r' && lineSeparator.length == 1 && curChar == lineSeparator[0]) { return sb.toString(); } else if (curChar == '\n' && lineSeparator.length == 2 && curChar == lineSeparator[1]) { if (sb.length() > 0 && sb.charAt(sb.length() - 1) == lineSeparator[0]) { sb.deleteCharAt(sb.length() - 1); return sb.toString(); } } sb.append(curChar); } if (sb.length() == 0) return null; return sb.toString(); } LineReader();  LineReader(String lineSeparator); @SuppressLint("NewApi") static String getLineSeparator(); String readLine(Reader reader);  }### Answer:
@Test public void testLineEndings_linux() throws IOException { final List<String> output = new ArrayList<>(); final LineReader reader = new LineReader("\n"); Reader stream = new BufferedReader(new InputStreamReader(makeStream("line1\r\nline2\r\nli\rne\n\n"))); String line; while ((line = reader.readLine(stream)) != null) { output.add(line); } assertThat(output.size(), is(4)); assertThat(output.get(0), is("line1\r")); assertThat(output.get(1), is("line2\r")); assertThat(output.get(2), is("li\rne")); assertThat(output.get(3), is("")); }
@Test public void testLineEndings_windows() throws IOException { final List<String> output = new ArrayList<>(); final LineReader reader = new LineReader("\r\n"); Reader stream = new BufferedReader(new InputStreamReader(makeStream("line1\r\nline2\r\n\r\n"))); String line; while ((line = reader.readLine(stream)) != null) { output.add(line); } assertThat(output.size(), is(3)); assertThat(output.get(0), is("line1")); assertThat(output.get(1), is("line2")); assertThat(output.get(2), is("")); }
@Test public void testLineEndings_legacy() throws IOException { final List<String> output = new ArrayList<>(); final LineReader reader = new LineReader("\r"); Reader stream = new BufferedReader(new InputStreamReader(makeStream("line1\n\rline2\n\rli\nne\r\r"))); String line; while ((line = reader.readLine(stream)) != null) { output.add(line); } assertThat(output.size(), is(4)); assertThat(output.get(0), is("line1\n")); assertThat(output.get(1), is("line2\n")); assertThat(output.get(2), is("li\nne")); assertThat(output.get(3), is("")); }