DelimitedStreamReader { public String read(int numBytes) throws IOException { Preconditions.checkArgument(numBytes >= 0); Preconditions.checkArgument(numBytes <= MAX_ALLOWABLE_BUFFER_SIZE); int numBytesRemaining = numBytes; if (!isReadBufferEmpty()) { int length = Math.min(end - offset, numBytesRemaining); copyToStrBuffer(buffer, offset, length); offset += length; numBytesRemaining -= length; } if (numBytesRemaining > 0) { readAmountToStrBuffer(numBytesRemaining); } if (strBufferIndex > 0 && strBuffer[strBufferIndex - 1] != LF) { return readLine(false); } int strBufferLength = strBufferIndex; strBufferIndex = 0; return new String(strBuffer, 0, strBufferLength, charset); } DelimitedStreamReader(InputStream stream, Charset charset, int bufferSize); String readLine(); String read(int numBytes);  }
@Test public void testEmptyRead() throws Exception { InputStream stream = mock(InputStream.class); when(stream.read(any(byte[].class), anyInt(), anyInt())) .thenReturn(-1); DelimitedStreamReader r = new DelimitedStreamReader(stream, Charsets.UTF_8, 10); try { r.read(10); fail(); } catch (IOException e) { } }
@Test public void testReadRemainder() throws Exception { String myMessage = "{this is my message}\n"; byte[] bytes = myMessage.getBytes(Charsets.UTF_8); InputStream stream = new ByteArrayInputStream(bytes); DelimitedStreamReader r = new DelimitedStreamReader(stream, Charsets.UTF_8, myMessage.length() / 3); assertTrue(stream.read(new byte[myMessage.length()/2], 0, myMessage.length()/2) > 0); String remainder = r.read(myMessage.length() - myMessage.length()/2); assertEquals(remainder, myMessage.substring(myMessage.length()/2, myMessage.length())); }
@Test public void testIncompleteRead() { 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.read(myMessage.length() * 2); fail(); } catch (IOException e) { } }
@Test public void testLenientRead() throws Exception { String myMessage = "{this is my message}\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.read(myMessage.length()/2); assertEquals(msg, myMessage); }
@Test public void testMultibyteCharacters() throws Exception { String myMessage = "{this is my message: héÿ}\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.read(myMessage.length()); assertEquals(msg, myMessage); }
@Test public void testMultipleStreamReads() throws Exception { String myMessage = "{this is my message: héÿ}\n"; byte[] bytes = myMessage.substring(0, myMessage.length()/2).getBytes(Charsets.UTF_8); byte[] bytes2 = myMessage.substring(myMessage.length()/2, myMessage.length()).getBytes(Charsets.UTF_8); InputStream miniStream = new ByteArrayInputStream(bytes); InputStream miniStream2 = new ByteArrayInputStream(bytes2); InputStream stream = new SplitInputStream(Lists.newArrayList(miniStream, miniStream2)); DelimitedStreamReader r = new DelimitedStreamReader(stream, Charsets.UTF_8, myMessage.length() / 3); String msg = r.read(myMessage.length()); assertEquals(msg, myMessage); }
@Test public void testReadWithSmallBuffer() 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.read(myMessage.length()); assertEquals(msg, myMessage); }
@Test public void testReadWithBigBuffer() 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.read(myMessage.length()); assertEquals(msg, myMessage); }
@Test public void testReadMultipleSmallBuffer() 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() - 1); String msg1 = r.read(myMessage.length()); String msg2 = r.read(myMessage2.length()); assertEquals(msg1, myMessage); assertEquals(msg2, myMessage2); }
@Test public void testReadMultipleHugeBuffer() 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.read(myMessage.length()); String msg2 = r.read(myMessage2.length()); assertEquals(msg1, myMessage); assertEquals(msg2, myMessage2); }