EDITokenizer extends AbstractTokenizer { public char[] lookahead(int n) throws IOException { logger.debug("EDITokenizer.lookahead({})", n); char[] rval = new char[n]; getChar(); rval[0] = cChar; ungetChar(); if (charBuffer.remaining() < n - 1) { logger.debug("Buffering more data to satisfy lookahead({}})", n); readUntilBufferProvidesAtLeast(n - 1); } int j = 1; for (int i = charBuffer.position(); i < ((Buffer) charBuffer).limit() && j < n; i++) rval[j++] = charBuffer.get(i); for (; j < n; ) { rval[j++] = '?'; } return rval; } EDITokenizer(Reader source);  EDITokenizer(Reader source, char[] preRead); @Override String toString(); void getChar(); char[] getBuffered(); char[] lookahead(int n); static final int BUFFER_SIZE; }
@Test public void testLookahead() throws IOException, EDISyntaxException { final String str = "abcdefghijklmnopqrstuvwxyz"; EDITokenizer tokenizer = new EDITokenizer(new StringReader(str)); char[] returnValue; returnValue = tokenizer.getBuffered(); assertEquals(0, returnValue.length); char[] lookahead; lookahead = tokenizer.lookahead(1); assertEquals(1, lookahead.length); assertEquals('a', lookahead[0]); lookahead = tokenizer.lookahead(2); assertEquals(2, lookahead.length); assertEquals('a', lookahead[0]); assertEquals('b', lookahead[1]); lookahead = tokenizer.lookahead(4); assertEquals(4, lookahead.length); assertEquals('a', lookahead[0]); assertEquals('b', lookahead[1]); assertEquals('c', lookahead[2]); assertEquals('d', lookahead[3]); lookahead = tokenizer.lookahead(26); assertEquals(26, lookahead.length); assertEquals('a', lookahead[0]); assertEquals('b', lookahead[1]); assertEquals('c', lookahead[2]); assertEquals('d', lookahead[3]); assertEquals('z', lookahead[25]); char[] consumed = tokenizer.getChars(1); assertEquals(1, consumed.length); assertEquals('a', consumed[0]); lookahead = tokenizer.lookahead(27); assertEquals(27, lookahead.length); assertEquals('b', lookahead[0]); assertEquals('c', lookahead[1]); assertEquals('d', lookahead[2]); assertEquals('z', lookahead[24]); assertEquals('?', lookahead[25]); assertEquals('?', lookahead[26]); lookahead = tokenizer.lookahead(100); assertEquals(100, lookahead.length); assertEquals(str.substring(1), new String(lookahead, 0, str.length() - 1)); assertEquals('b', lookahead[0]); assertEquals('c', lookahead[1]); assertEquals('?', lookahead[str.length() - 1]); assertEquals('?', lookahead[str.length()]); assertEquals('?', lookahead[99]); returnValue = tokenizer.getBuffered(); assertEquals(str.length() - 1, returnValue.length); assertEquals('b', returnValue[0]); assertEquals('z', returnValue[str.length() - 2]); lookahead = tokenizer.lookahead(100); assertEquals(100, lookahead.length); assertEquals(str.substring(1), new String(lookahead, 0, str.length() - 1)); assertEquals('b', lookahead[0]); assertEquals('c', lookahead[1]); assertEquals('?', lookahead[str.length() - 1]); assertEquals('?', lookahead[str.length()]); assertEquals('?', lookahead[99]); }