HeadersParser { static String readLine(final InputStream inputStream, final String charset) { byte[] rawData = readRawLine(inputStream); if (rawData == null) { return null; } int len = rawData.length; int offset = 0; if (len > 0) { if (rawData[len - 1] == '\n') { offset++; if (len > 1) { if (rawData[len - 2] == '\r') { offset++; } } } } return getString(rawData, 0, len - offset, charset); } private  HeadersParser(); static Map<String, List<String>> parseHeaders(final InputStream headersSection, final String charset); static final String EMPTY_STRING; }
@Test public void testReadLine() throws Exception { InputStream lines = new ByteArrayInputStream("\r\r\nfoo bar\r\n".getBytes(CHARACTER_SET)); assertEquals("\r", HeadersParser.readLine(lines, CHARACTER_SET)); assertEquals("foo bar", HeadersParser.readLine(lines, CHARACTER_SET)); assertEquals(null, HeadersParser.readLine(lines, CHARACTER_SET)); lines = new ByteArrayInputStream("\n\r\nfoo bar\r\n".getBytes(CHARACTER_SET)); assertEquals("", HeadersParser.readLine(lines, CHARACTER_SET)); assertEquals("", HeadersParser.readLine(lines, CHARACTER_SET)); assertEquals("foo bar", HeadersParser.readLine(lines, CHARACTER_SET)); assertEquals(null, HeadersParser.readLine(lines, CHARACTER_SET)); }