From df62fc28559228713caf3382183556d4c41e5647 Mon Sep 17 00:00:00 2001 From: Evgeny Mandrikov Date: Fri, 21 Oct 2011 14:22:52 +0400 Subject: SONAR-2632 Remove sonar-channel buffer restriction * Add test for backward compatibility with a COBOL plugin * Fix violations * Simplify code --- .../org/sonar/channel/ChannelCodeReaderFilter.java | 8 +++-- .../java/org/sonar/channel/ChannelDispatcher.java | 6 ++-- .../main/java/org/sonar/channel/CodeBuffer.java | 36 ++++++++-------------- .../main/java/org/sonar/channel/CodeReader.java | 5 +-- 4 files changed, 24 insertions(+), 31 deletions(-) (limited to 'sonar-channel/src/main') 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 extends CodeReaderFilter extends CodeReaderFilter extends Channel { - 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 extends Channel { } } 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) { -- cgit v1.2.3