aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-channel
diff options
context:
space:
mode:
Diffstat (limited to 'sonar-channel')
-rw-r--r--sonar-channel/src/main/java/org/sonar/channel/ChannelCodeReaderFilter.java8
-rw-r--r--sonar-channel/src/main/java/org/sonar/channel/ChannelDispatcher.java6
-rw-r--r--sonar-channel/src/main/java/org/sonar/channel/CodeBuffer.java36
-rw-r--r--sonar-channel/src/main/java/org/sonar/channel/CodeReader.java5
-rw-r--r--sonar-channel/src/test/java/org/sonar/channel/CodeBufferTest.java19
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);