DelimitedStreamReader { public String readLine() throws IOException { return readLine(true); } DelimitedStreamReader(InputStream stream, Charset charset, int bufferSize); String readLine(); String read(int numBytes);  }
@Test public void testReadlineWithSmallBuffer() throws Exception { String myMessage = "{this is my message}\r\n"; byte[] bytes = myMessage.getBytes(Charsets.UTF_8); InputStream stream = new ByteArrayInputStream(bytes); DelimitedStreamReader r = new DelimitedStreamReader(stream, Charsets.UTF_8, myMessage.length() / 3); String msg = r.readLine(); assertEquals(msg, myMessage.trim()); }
@Test public void testReadlineWithBigBuffer() throws Exception { String myMessage = "{this is my message}\r\n"; byte[] bytes = myMessage.getBytes(Charsets.UTF_8); InputStream stream = new ByteArrayInputStream(bytes); DelimitedStreamReader r = new DelimitedStreamReader(stream, Charsets.UTF_8, myMessage.length() * 3); String msg = r.readLine(); assertEquals(msg, myMessage.trim()); }
@Test public void testReadlineMultipleSmallBuffer() throws Exception { String myMessage = "{msg1}\r\n"; String myMessage2 = "{this is my message}\n"; String myMessage3 = "{this is my message2}\r\n"; byte[] bytes = (myMessage + myMessage2 + myMessage3).getBytes(Charsets.UTF_8); InputStream stream = new ByteArrayInputStream(bytes); DelimitedStreamReader r = new DelimitedStreamReader(stream, Charsets.UTF_8, myMessage.length()); String msg1 = r.readLine(); String msg2 = r.readLine(); String msg3 = r.readLine(); assertEquals(msg1, myMessage.trim()); assertEquals(msg2, myMessage2.trim()); assertEquals(msg3, myMessage3.trim()); }
@Test public void testReadlineMultipleHugeBuffer() throws Exception { String myMessage = "{msg1}\r\n"; String myMessage2 = "{this is my message}\r\n"; byte[] bytes = (myMessage + myMessage2).getBytes(Charsets.UTF_8); InputStream stream = new ByteArrayInputStream(bytes); DelimitedStreamReader r = new DelimitedStreamReader(stream, Charsets.UTF_8, myMessage.length() * 10 ); String msg1 = r.readLine(); String msg2 = r.readLine(); assertEquals(msg1, myMessage.trim()); assertEquals(msg2, myMessage2.trim()); }
@Test public void testIncompleteReadline() { String myMessage = "{this is my message}"; byte[] bytes = myMessage.getBytes(Charsets.UTF_8); InputStream stream = new ByteArrayInputStream(bytes); DelimitedStreamReader r = new DelimitedStreamReader(stream, Charsets.UTF_8, myMessage.length()); try { r.readLine(); fail(); } catch (IOException e) { } }