diff options
author | Evgeny Mandrikov <mandrikov@gmail.com> | 2011-10-21 14:22:52 +0400 |
---|---|---|
committer | Evgeny Mandrikov <mandrikov@gmail.com> | 2011-10-24 10:49:42 +0400 |
commit | df62fc28559228713caf3382183556d4c41e5647 (patch) | |
tree | d2740cc265120df1dbf74467875359fe50587db2 /sonar-channel | |
parent | 1f0558ea12501d8d47b02004ccbdaf59099d07ce (diff) | |
download | sonarqube-df62fc28559228713caf3382183556d4c41e5647.tar.gz sonarqube-df62fc28559228713caf3382183556d4c41e5647.zip |
SONAR-2632 Remove sonar-channel buffer restriction
* Add test for backward compatibility with a COBOL plugin
* Fix violations
* Simplify code
Diffstat (limited to 'sonar-channel')
5 files changed, 43 insertions, 31 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 032f2d67ef6..2dd85d7b424 100644 --- a/sonar-channel/src/main/java/org/sonar/channel/ChannelCodeReaderFilter.java +++ b/sonar-channel/src/main/java/org/sonar/channel/ChannelCodeReaderFilter.java @@ -60,7 +60,7 @@ public final class ChannelCodeReaderFilter<OUTPUT> extends CodeReaderFilter<OUTP } /** - * ${@inheritDoc} + * {@inheritDoc} */ @Override public void setReader(Reader reader) { @@ -69,11 +69,13 @@ public final class ChannelCodeReaderFilter<OUTPUT> extends CodeReaderFilter<OUTP } /** - * ${@inheritDoc} + * {@inheritDoc} */ @Override public int read(char[] filteredBuffer, int offset, int length) throws IOException { - if (internalCodeReader.peek() == -1) return -1; + if (internalCodeReader.peek() == -1) { + return -1; + } int initialOffset = offset; while (offset < filteredBuffer.length) { if (internalCodeReader.peek() == -1) { diff --git a/sonar-channel/src/main/java/org/sonar/channel/ChannelDispatcher.java b/sonar-channel/src/main/java/org/sonar/channel/ChannelDispatcher.java index d21ac683abd..1936c0c1bb0 100644 --- a/sonar-channel/src/main/java/org/sonar/channel/ChannelDispatcher.java +++ b/sonar-channel/src/main/java/org/sonar/channel/ChannelDispatcher.java @@ -29,7 +29,7 @@ import org.slf4j.LoggerFactory; public class ChannelDispatcher<OUTPUT> extends Channel<OUTPUT> { - private static final Logger logger = LoggerFactory.getLogger(ChannelDispatcher.class); + private static final Logger LOG = LoggerFactory.getLogger(ChannelDispatcher.class); private final boolean failIfNoChannelToConsumeOneCharacter; @SuppressWarnings("rawtypes") @@ -79,13 +79,13 @@ public class ChannelDispatcher<OUTPUT> extends Channel<OUTPUT> { } } if ( !characterConsumed) { - if (logger.isDebugEnabled() || failIfNoChannelToConsumeOneCharacter) { + if (LOG.isDebugEnabled() || failIfNoChannelToConsumeOneCharacter) { String message = "None of the channel has been able to handle character '" + (char) code.peek() + "' (decimal value " + code.peek() + ") at line " + code.getLinePosition() + ", column " + code.getColumnPosition(); if (failIfNoChannelToConsumeOneCharacter) { throw new IllegalStateException(message); } - logger.debug(message); + LOG.debug(message); } code.pop(); } diff --git a/sonar-channel/src/main/java/org/sonar/channel/CodeBuffer.java b/sonar-channel/src/main/java/org/sonar/channel/CodeBuffer.java index 2093644b756..705b287f7bb 100644 --- a/sonar-channel/src/main/java/org/sonar/channel/CodeBuffer.java +++ b/sonar-channel/src/main/java/org/sonar/channel/CodeBuffer.java @@ -53,40 +53,28 @@ public class CodeBuffer implements CharSequence { this(new StringReader(code), configuration); } + /** + * Note that this constructor will read everything from reader and will close it. + */ protected CodeBuffer(Reader initialCodeReader, CodeReaderConfiguration configuration) { Reader reader = null; - + try { lastChar = -1; cursor = new Cursor(); tabWidth = configuration.getTabWidth(); - + /* Setup the filters on the reader */ reader = initialCodeReader; for (CodeReaderFilter<?> codeReaderFilter : configuration.getCodeReaderFilters()) { reader = new Filter(reader, codeReaderFilter, configuration); } - - fillBuffer(reader); - } - finally { - if (reader != null) IOUtils.closeQuietly(reader); - } - } - - private void fillBuffer(Reader reader) { - try { - StringBuilder sb = new StringBuilder(); - char[] buffer = new char[4096]; - int read = reader.read(buffer); - while(read != -1 && read != 0) { - sb.append(buffer, 0, read); - read = reader.read(buffer); - } - this.buffer = new char[sb.length()]; - sb.getChars(0, sb.length(), this.buffer, 0); + + buffer = IOUtils.toCharArray(reader); } catch (IOException e) { - throw new RuntimeException(e); + throw new ChannelException(e.getMessage(), e); + } finally { + IOUtils.closeQuietly(reader); } } @@ -243,6 +231,7 @@ public class CodeBuffer implements CharSequence { return column; } + @Override public Cursor clone() { Cursor clone = new Cursor(); clone.column = column; @@ -272,7 +261,8 @@ public class CodeBuffer implements CharSequence { @Override public int read(char[] cbuf, int off, int len) throws IOException { - return codeReaderFilter.read(cbuf, off, len); + int read = codeReaderFilter.read(cbuf, off, len); + return read == 0 ? -1 : read; } @Override diff --git a/sonar-channel/src/main/java/org/sonar/channel/CodeReader.java b/sonar-channel/src/main/java/org/sonar/channel/CodeReader.java index 1033ad20668..0c09c9d4680 100644 --- a/sonar-channel/src/main/java/org/sonar/channel/CodeReader.java +++ b/sonar-channel/src/main/java/org/sonar/channel/CodeReader.java @@ -47,6 +47,7 @@ public class CodeReader extends CodeBuffer { /** * Creates a code reader with specific configuration parameters. + * Note that this constructor will read everything from reader and will close it. * * @param code * the Reader to read code from @@ -123,7 +124,7 @@ public class CodeReader extends CodeBuffer { } /** - * @deprecated use peekTo(EndMatcher matcher, Appendable appendable) + * @deprecated in 2.2, use {@link #peekTo(EndMatcher matcher, Appendable appendable)} instead */ @Deprecated public final String peekTo(EndMatcher matcher) { @@ -133,7 +134,7 @@ public class CodeReader extends CodeBuffer { } /** - * @deprecated use popTo(Matcher matcher, Appendable appendable) + * @deprecated in 2.2, use {@link #popTo(Matcher matcher, Appendable appendable)} instead */ @Deprecated public final void popTo(EndMatcher matcher, Appendable appendable) { 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 387978fe069..b56b0fde1ea 100644 --- a/sonar-channel/src/test/java/org/sonar/channel/CodeBufferTest.java +++ b/sonar-channel/src/test/java/org/sonar/channel/CodeBufferTest.java @@ -257,11 +257,29 @@ public class CodeBufferTest { assertThat(code.pop(), is( -1)); } + /** + * Backward compatibility with a COBOL plugin: filter returns 0 instead of -1, when end of the stream has been reached. + */ + @Test(timeout = 1000) + public void testWrongEndOfStreamFilter() { + CodeReaderConfiguration configuration = new CodeReaderConfiguration(); + configuration.setCodeReaderFilters(new WrongEndOfStreamFilter()); + new CodeBuffer("foo", configuration); + } + + class WrongEndOfStreamFilter extends CodeReaderFilter<Object> { + @Override + public int read(char[] filteredBuffer, int offset, int length) throws IOException { + return 0; + } + } + class ReplaceNumbersFilter extends CodeReaderFilter<Object> { private Pattern pattern = Pattern.compile("\\d"); private String REPLACEMENT = "-"; + @Override public int read(char[] cbuf, int off, int len) throws IOException { char[] tempBuffer = new char[cbuf.length]; int charCount = getReader().read(tempBuffer, off, len); @@ -278,6 +296,7 @@ public class CodeBufferTest { private Pattern pattern = Pattern.compile("[a-zA-Z]"); private String REPLACEMENT = "*"; + @Override public int read(char[] cbuf, int off, int len) throws IOException { char[] tempBuffer = new char[cbuf.length]; int charCount = getReader().read(tempBuffer, off, len); |