From 0b9c56c76952cc93dea74b386938154158f407dd Mon Sep 17 00:00:00 2001 From: Dinesh Bolkensteyn Date: Thu, 20 Oct 2011 20:50:42 +0200 Subject: SONAR-2923 and SONAR-2632: Remove sonar-channel buffer restriction and character pushback feature --- .../org/sonar/channel/ChannelCodeReaderFilter.java | 1 + .../main/java/org/sonar/channel/CodeBuffer.java | 111 +++++++-------------- .../org/sonar/channel/CodeReaderConfiguration.java | 20 ---- .../java/org/sonar/channel/CodeBufferTest.java | 22 ---- .../java/org/sonar/channel/CodeReaderTest.java | 18 ---- .../java/org/sonar/channel/RegexChannelTest.java | 21 ++++ 6 files changed, 56 insertions(+), 137 deletions(-) diff --git a/sonar-channel/src/main/java/org/sonar/channel/ChannelCodeReaderFilter.java b/sonar-channel/src/main/java/org/sonar/channel/ChannelCodeReaderFilter.java index 5422def73a3..032f2d67ef6 100644 --- a/sonar-channel/src/main/java/org/sonar/channel/ChannelCodeReaderFilter.java +++ b/sonar-channel/src/main/java/org/sonar/channel/ChannelCodeReaderFilter.java @@ -73,6 +73,7 @@ public final class ChannelCodeReaderFilter extends CodeReaderFilter codeReaderFilter : configuration.getCodeReaderFilters()) { reader = new Filter(reader, codeReaderFilter, configuration); } - this.code = reader; - fillBuffer(); + + /* Buffer the whole reader */ + fillBuffer(reader); + + /* Clean-up */ + IOUtils.closeQuietly(reader); } - - protected CodeBuffer(String code, CodeReaderConfiguration configuration) { - this(new StringReader(code), configuration); + + private void fillBuffer(Reader reader) { + try { + StringBuilder sb = new StringBuilder(); + char[] buffer = new char[4096]; + int read; + while ((read = reader.read(buffer)) != -1) { + sb.append(buffer, 0, read); + } + this.buffer = new char[sb.length()]; + sb.getChars(0, sb.length(), this.buffer, 0); + } catch (IOException e) { + throw new RuntimeException(e); + } } /** @@ -76,16 +93,13 @@ public class CodeBuffer implements CharSequence { * @return the next character or -1 if the end of the stream is reached */ public final int pop() { - if (bufferPosition == bufferSize) { - fillBuffer(); - } - if (bufferSize == 0) { + if (bufferPosition >= buffer.length) { return -1; } int character = buffer[bufferPosition++]; updateCursorPosition(character); if (recordingMode) { - recordedCharacters.append((char) character); + recordedCharacters.append((char)character); } lastChar = character; return character; @@ -103,24 +117,6 @@ public class CodeBuffer implements CharSequence { } } - private int fillBuffer() { - try { - int offset = bufferSize - bufferPosition; - if (offset != 0) { - System.arraycopy(buffer, bufferPosition, buffer, 0, bufferSize - bufferPosition); - } - bufferPosition = 0; - int numberOfChars = code.read(buffer, offset, bufferCapacity - offset); - if (numberOfChars == -1) { - numberOfChars = 0; - } - bufferSize = numberOfChars + offset; - return offset; - } catch (IOException e) { - throw new ChannelException(e.getMessage(), e); - } - } - /** * Looks at the last consumed character * @@ -139,38 +135,6 @@ public class CodeBuffer implements CharSequence { return intAt(0); } - /** - * Pushes a character sequence onto the top of this CodeBuffer. This characters will be then the first to be read. - * - * @param chars - * the character sequences to push into the CodeBuffer - */ - public void push(CharSequence chars) { - int length = chars.length(); - if (bufferPosition >= length) { - for (int index = 0; index < length; index++) { - buffer[bufferPosition + index - length] = chars.charAt(index); - } - bufferPosition -= length; - } else { - char[] extendedBuffer = new char[buffer.length - bufferPosition + length]; - for (int index = 0; index < length; index++) { - extendedBuffer[index] = chars.charAt(index); - } - System.arraycopy(buffer, bufferPosition, extendedBuffer, length, bufferSize - bufferPosition); - buffer = extendedBuffer; - bufferSize = bufferSize + length - bufferPosition; - bufferPosition = 0; - } - } - - /** - * Close the stream - */ - public final void close() { - IOUtils.closeQuietly(code); - } - /** * @return the current line of the cursor */ @@ -219,33 +183,26 @@ public class CodeBuffer implements CharSequence { * Returns the character at the specified index after the cursor without consuming it * * @param index - * the index of the character to be returned + * the relative index of the character to be returned * @return the desired character * @see java.lang.CharSequence#charAt(int) */ public final char charAt(int index) { - return (char) intAt(index); + return (char)intAt(index); } protected final int intAt(int index) { - if (bufferPosition + index > bufferSize - 1) { - fillBuffer(); - } - if (bufferPosition + index > bufferSize - 1) { + if (bufferPosition + index >= buffer.length) { return -1; } return buffer[bufferPosition + index]; } /** - * Warning : this method returns Integer.MAX_VALUE when the buffer is fully used - * as the length of the stream can't be known before having consumed all characters. - * - * Integer.MAX_VALUE is returned to prevent regular expression matchers to stop consuming the stream of characters (see - * http://jira.codehaus.org/browse/SONAR-2010) + * Returns the relative length of the string (i.e. excluding the popped chars) */ public final int length() { - return (bufferSize == bufferCapacity ? Integer.MAX_VALUE : bufferSize - bufferPosition); + return buffer.length - bufferPosition; } public final CharSequence subSequence(int start, int end) { diff --git a/sonar-channel/src/main/java/org/sonar/channel/CodeReaderConfiguration.java b/sonar-channel/src/main/java/org/sonar/channel/CodeReaderConfiguration.java index bd543207ea9..7347074b01d 100644 --- a/sonar-channel/src/main/java/org/sonar/channel/CodeReaderConfiguration.java +++ b/sonar-channel/src/main/java/org/sonar/channel/CodeReaderConfiguration.java @@ -28,31 +28,12 @@ import java.util.List; */ public class CodeReaderConfiguration { - public final static int DEFAULT_BUFFER_CAPACITY = 8000; - public final static int DEFAULT_TAB_WIDTH = 1; - private int bufferCapacity = DEFAULT_BUFFER_CAPACITY; - private int tabWidth = DEFAULT_TAB_WIDTH; private List> codeReaderFilters = new ArrayList>(); - /** - * @return the bufferCapacity - */ - public int getBufferCapacity() { - return bufferCapacity; - } - - /** - * @param bufferCapacity - * the bufferCapacity to set - */ - public void setBufferCapacity(int bufferCapacity) { - this.bufferCapacity = bufferCapacity; - } - /** * @return the tabWidth */ @@ -96,7 +77,6 @@ public class CodeReaderConfiguration { public CodeReaderConfiguration cloneWithoutCodeReaderFilters() { CodeReaderConfiguration clone = new CodeReaderConfiguration(); - clone.setBufferCapacity(bufferCapacity); clone.setTabWidth(tabWidth); return clone; } diff --git a/sonar-channel/src/test/java/org/sonar/channel/CodeBufferTest.java b/sonar-channel/src/test/java/org/sonar/channel/CodeBufferTest.java index 471b16bd975..387978fe069 100644 --- a/sonar-channel/src/test/java/org/sonar/channel/CodeBufferTest.java +++ b/sonar-channel/src/test/java/org/sonar/channel/CodeBufferTest.java @@ -116,20 +116,6 @@ public class CodeBufferTest { assertEquals('6', reader.charAt(5)); } - @Test - public void testPush() { - CodeReader reader = new CodeReader("12", defaulConfiguration); - assertEquals('1', (char) reader.pop()); - reader.push("a"); - assertEquals('a', (char) reader.peek()); - reader.push("45"); - assertEquals("45a2", new String(reader.peek(4))); - for (int i = 0; i < 4; i++) { - reader.pop(); - } - assertEquals( -1, reader.pop()); - } - @Test public void testCharAtIndexOutOfBoundsException() { CodeBuffer reader = new CodeBuffer("12345", defaulConfiguration); @@ -209,14 +195,6 @@ public class CodeBufferTest { assertThat(codeBuffer.length(), is(4)); } - @Test - public void theLengthShouldBeIntegerMaxValueWhenTheBufferCantContainAllCharacters() { - String myCode = "myCode"; - CodeReaderConfiguration conf = new CodeReaderConfiguration(); - conf.setBufferCapacity(2); - assertThat(new CodeBuffer(myCode, conf).length(), is(Integer.MAX_VALUE)); - } - @Test public void testSeveralCodeReaderFilter() throws Exception { CodeReaderConfiguration configuration = new CodeReaderConfiguration(); diff --git a/sonar-channel/src/test/java/org/sonar/channel/CodeReaderTest.java b/sonar-channel/src/test/java/org/sonar/channel/CodeReaderTest.java index a748cfa6f83..b6f1d0b8289 100644 --- a/sonar-channel/src/test/java/org/sonar/channel/CodeReaderTest.java +++ b/sonar-channel/src/test/java/org/sonar/channel/CodeReaderTest.java @@ -83,24 +83,6 @@ public class CodeReaderTest { assertThat(previousCursor.getLine(), is(1)); } - @Test - public void testPeekToAndReachingTheBufferLimit() { - CodeReaderConfiguration configuration = new CodeReaderConfiguration(); - configuration.setBufferCapacity(10); - CodeReader reader = new CodeReader("word1 word2 word2", configuration); - for (int i = 0; i < 6; i++) { - reader.pop(); - } - StringBuilder result = new StringBuilder(); - reader.peekTo(new EndMatcher() { - - public boolean match(int endFlag) { - return ' ' == (char) endFlag; - } - }, result); - assertEquals("word2", result.toString()); - } - @Test public void testPopToWithRegex() { CodeReader reader = new CodeReader(new StringReader("123ABC")); diff --git a/sonar-channel/src/test/java/org/sonar/channel/RegexChannelTest.java b/sonar-channel/src/test/java/org/sonar/channel/RegexChannelTest.java index 35d9c936b78..322899266ab 100644 --- a/sonar-channel/src/test/java/org/sonar/channel/RegexChannelTest.java +++ b/sonar-channel/src/test/java/org/sonar/channel/RegexChannelTest.java @@ -33,7 +33,28 @@ public class RegexChannelTest { dispatcher.consume(new CodeReader("my word"), output); assertThat(output.toString(), is("my word")); } + + @Test + public void shouldMatchTokenLongerThanBuffer() { + ChannelDispatcher dispatcher = ChannelDispatcher.builder().addChannel(new MyLiteralChannel()).build(); + StringBuilder output = new StringBuilder(); + + dispatcher.consume(new CodeReader("\"bonjour\""), output); + assertThat(output.toString(), is("\"bonjour\"")); + } + + private static class MyLiteralChannel extends RegexChannel { + + public MyLiteralChannel() { + super("\"[^\"]*+\""); + } + @Override + protected void consume(CharSequence token, StringBuilder output) { + output.append("" + token + ""); + } + } + private static class MyWordChannel extends RegexChannel { public MyWordChannel() { -- cgit v1.2.3