You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by jv...@apache.org on 2013/04/10 14:27:57 UTC
git commit: now return only one object and not an array from the
ProtocolDecoder
Updated Branches:
refs/heads/trunk 239902793 -> 3d3767d62
now return only one object and not an array from the ProtocolDecoder
Project: http://git-wip-us.apache.org/repos/asf/mina/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina/commit/3d3767d6
Tree: http://git-wip-us.apache.org/repos/asf/mina/tree/3d3767d6
Diff: http://git-wip-us.apache.org/repos/asf/mina/diff/3d3767d6
Branch: refs/heads/trunk
Commit: 3d3767d6287bb12377b96fa12922aeccfdc9d02e
Parents: 2399027
Author: jvermillard <jv...@apache.org>
Authored: Wed Apr 10 14:27:18 2013 +0200
Committer: jvermillard <jv...@apache.org>
Committed: Wed Apr 10 14:27:18 2013 +0200
----------------------------------------------------------------------
.../org/apache/mina/codec/ProtocolDecoder.java | 4 +-
.../mina/codec/textline/TextLineDecoder.java | 83 ++++++---------
.../codec/textline/AutoTextLineDecoderTest.java | 58 ++++------
.../codec/textline/UnixTextLineDecoderTest.java | 57 ++++------
.../codec/textline/WindowsTextLineDecoderTest.java | 57 ++++------
.../mina/filter/codec/ProtocolCodecFilter.java | 8 +-
.../java/org/apache/mina/http/DecoderState.java | 3 +-
.../org/apache/mina/http/HttpServerDecoder.java | 11 ++-
.../apache/mina/http/HttpServerDecoderTest.java | 16 ++--
9 files changed, 128 insertions(+), 169 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mina/blob/3d3767d6/codec/src/main/java/org/apache/mina/codec/ProtocolDecoder.java
----------------------------------------------------------------------
diff --git a/codec/src/main/java/org/apache/mina/codec/ProtocolDecoder.java b/codec/src/main/java/org/apache/mina/codec/ProtocolDecoder.java
index c1dea93..cc0e30d 100644
--- a/codec/src/main/java/org/apache/mina/codec/ProtocolDecoder.java
+++ b/codec/src/main/java/org/apache/mina/codec/ProtocolDecoder.java
@@ -49,10 +49,10 @@ public interface ProtocolDecoder<INPUT, OUTPUT, DECODING_STATE> {
*
* @param input the received message to decode
* @param context the decoding context (will be stored in the session for the next decode call)
- * @return the decoded messages or <code>null</code> if nothing was decoded
+ * @return the decoded message or <code>null</code> if nothing was decoded
* @throws ProtocolDecoderException if something wrong happen during decoding (e.g. : a malformed input message)
*/
- OUTPUT[] decode(INPUT input, DECODING_STATE context) throws ProtocolDecoderException;
+ OUTPUT decode(INPUT input, DECODING_STATE context) throws ProtocolDecoderException;
/**
* Finish decoding, for example if the decoder accumulated some unused input, it should discard it, or throw an
http://git-wip-us.apache.org/repos/asf/mina/blob/3d3767d6/codec/src/main/java/org/apache/mina/codec/textline/TextLineDecoder.java
----------------------------------------------------------------------
diff --git a/codec/src/main/java/org/apache/mina/codec/textline/TextLineDecoder.java b/codec/src/main/java/org/apache/mina/codec/textline/TextLineDecoder.java
index 845ee24..9ccb324 100644
--- a/codec/src/main/java/org/apache/mina/codec/textline/TextLineDecoder.java
+++ b/codec/src/main/java/org/apache/mina/codec/textline/TextLineDecoder.java
@@ -24,14 +24,12 @@ import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
-import java.util.ArrayList;
-import java.util.List;
import org.apache.mina.codec.ProtocolDecoder;
/**
* A {@link ProtocolDecoder} which decodes a text line into a string.
- *
+ *
* @author <a href="http://mina.apache.org">Apache MINA Project</a>
*/
public class TextLineDecoder implements ProtocolDecoder<ByteBuffer, String, TextLineDecoder.Context> {
@@ -50,48 +48,42 @@ public class TextLineDecoder implements ProtocolDecoder<ByteBuffer, String, Text
private int bufferLength = 128;
/**
- * Creates a new instance with the current default {@link Charset}
- * and {@link LineDelimiter#AUTO} delimiter.
+ * Creates a new instance with the current default {@link Charset} and {@link LineDelimiter#AUTO} delimiter.
*/
public TextLineDecoder() {
this(LineDelimiter.AUTO);
}
/**
- * Creates a new instance with the current default {@link Charset}
- * and the specified <tt>delimiter</tt>.
+ * Creates a new instance with the current default {@link Charset} and the specified <tt>delimiter</tt>.
*/
public TextLineDecoder(String delimiter) {
this(new LineDelimiter(delimiter));
}
/**
- * Creates a new instance with the current default {@link Charset}
- * and the specified <tt>delimiter</tt>.
+ * Creates a new instance with the current default {@link Charset} and the specified <tt>delimiter</tt>.
*/
public TextLineDecoder(LineDelimiter delimiter) {
this(Charset.defaultCharset(), delimiter);
}
/**
- * Creates a new instance with the spcified <tt>charset</tt>
- * and {@link LineDelimiter#AUTO} delimiter.
+ * Creates a new instance with the spcified <tt>charset</tt> and {@link LineDelimiter#AUTO} delimiter.
*/
public TextLineDecoder(Charset charset) {
this(charset, LineDelimiter.AUTO);
}
/**
- * Creates a new instance with the spcified <tt>charset</tt>
- * and the specified <tt>delimiter</tt>.
+ * Creates a new instance with the spcified <tt>charset</tt> and the specified <tt>delimiter</tt>.
*/
public TextLineDecoder(Charset charset, String delimiter) {
this(charset, new LineDelimiter(delimiter));
}
/**
- * Creates a new instance with the specified <tt>charset</tt>
- * and the specified <tt>delimiter</tt>.
+ * Creates a new instance with the specified <tt>charset</tt> and the specified <tt>delimiter</tt>.
*/
public TextLineDecoder(Charset charset, LineDelimiter delimiter) {
if (charset == null) {
@@ -114,20 +106,16 @@ public class TextLineDecoder implements ProtocolDecoder<ByteBuffer, String, Text
}
/**
- * Returns the allowed maximum size of the line to be decoded.
- * If the size of the line to be decoded exceeds this value, the
- * decoder will throw a {@link BufferDataException}. The default
- * value is <tt>1024</tt> (1KB).
+ * Returns the allowed maximum size of the line to be decoded. If the size of the line to be decoded exceeds this
+ * value, the decoder will throw a {@link BufferDataException}. The default value is <tt>1024</tt> (1KB).
*/
public int getMaxLineLength() {
return maxLineLength;
}
/**
- * Sets the allowed maximum size of the line to be decoded.
- * If the size of the line to be decoded exceeds this value, the
- * decoder will throw a {@link BufferDataException}. The default
- * value is <tt>1024</tt> (1KB).
+ * Sets the allowed maximum size of the line to be decoded. If the size of the line to be decoded exceeds this
+ * value, the decoder will throw a {@link BufferDataException}. The default value is <tt>1024</tt> (1KB).
*/
public void setMaxLineLength(int maxLineLength) {
if (maxLineLength <= 0) {
@@ -138,9 +126,8 @@ public class TextLineDecoder implements ProtocolDecoder<ByteBuffer, String, Text
}
/**
- * Sets the default buffer size. This buffer is used in the Context
- * to store the decoded line.
- *
+ * Sets the default buffer size. This buffer is used in the Context to store the decoded line.
+ *
* @param bufferLength The default bufer size
*/
public void setBufferLength(int bufferLength) {
@@ -153,8 +140,7 @@ public class TextLineDecoder implements ProtocolDecoder<ByteBuffer, String, Text
}
/**
- * Returns the allowed buffer size used to store the decoded line
- * in the Context instance.
+ * Returns the allowed buffer size used to store the decoded line in the Context instance.
*/
public int getBufferLength() {
return bufferLength;
@@ -168,7 +154,8 @@ public class TextLineDecoder implements ProtocolDecoder<ByteBuffer, String, Text
/**
* {@inheritDoc}
*/
- public String[] decode(ByteBuffer in, Context ctx) {
+ @Override
+ public String decode(ByteBuffer in, Context ctx) {
if (LineDelimiter.AUTO.equals(delimiter)) {
return decodeAuto(ctx, in);
} else {
@@ -179,21 +166,22 @@ public class TextLineDecoder implements ProtocolDecoder<ByteBuffer, String, Text
/**
* {@inheritDoc}
*/
+ @Override
public void finishDecode(Context ctx) {
}
/**
* Decode a line using the default delimiter on the current system
*/
- private String[] decodeAuto(Context ctx, ByteBuffer in) {
- List<String> decoded = new ArrayList<String>();
+ private String decodeAuto(Context ctx, ByteBuffer in) {
+ String decoded = null;
int matchCount = ctx.getMatchCount();
// Try to find a match
int oldPos = in.position();
int oldLimit = in.limit();
- while (in.hasRemaining()) {
+ while (in.hasRemaining() && decoded == null) {
byte b = in.get();
boolean matched = false;
@@ -233,8 +221,7 @@ public class TextLineDecoder implements ProtocolDecoder<ByteBuffer, String, Text
CharsetDecoder decoder = ctx.getDecoder();
CharBuffer buffer = decoder.decode(buf);
- String str = new String(buffer.array());
- decoded.add(str);
+ decoded = new String(buffer.array());
} else {
int overflowPosition = ctx.getOverflowLength();
throw new IllegalStateException("Line is too long: " + overflowPosition);
@@ -254,22 +241,23 @@ public class TextLineDecoder implements ProtocolDecoder<ByteBuffer, String, Text
ctx.append(in);
ctx.setMatchCount(matchCount);
- return decoded.toArray(new String[decoded.size()]);
+ return decoded;
}
/**
* Decode a line using the delimiter defined by the caller
- * @return
+ *
+ * @return
*/
- private String[] decodeNormal(Context ctx, ByteBuffer in) {
- List<String> decoded = new ArrayList<String>();
+ private String decodeNormal(Context ctx, ByteBuffer in) {
+ String decoded = null;
int matchCount = ctx.getMatchCount();
// Try to find a match
int oldPos = in.position();
int oldLimit = in.limit();
- while (in.hasRemaining()) {
+ while (in.hasRemaining() && decoded == null) {
byte b = in.get();
if (delimBuf.get(matchCount) == b) {
@@ -294,8 +282,7 @@ public class TextLineDecoder implements ProtocolDecoder<ByteBuffer, String, Text
CharsetDecoder decoder = ctx.getDecoder();
CharBuffer buffer = decoder.decode(buf);
- String str = new String(buffer.array());
- decoded.add(str);
+ decoded = new String(buffer.array());
} else {
int overflowLength = ctx.getOverflowLength();
throw new IllegalStateException("Line is too long: " + overflowLength);
@@ -305,7 +292,6 @@ public class TextLineDecoder implements ProtocolDecoder<ByteBuffer, String, Text
} finally {
ctx.reset();
}
-
oldPos = pos;
matchCount = 0;
@@ -322,13 +308,13 @@ public class TextLineDecoder implements ProtocolDecoder<ByteBuffer, String, Text
ctx.append(in);
ctx.setMatchCount(matchCount);
- return decoded.toArray(new String[decoded.size()]);
+ return decoded;
}
/**
- * A Context used during the decoding of a lin. It stores the decoder,
- * the temporary buffer containing the decoded line, and other status flags.
- *
+ * A Context used during the decoding of a lin. It stores the decoder, the temporary buffer containing the decoded
+ * line, and other status flags.
+ *
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
* @version $Rev$, $Date$
*/
@@ -341,7 +327,7 @@ public class TextLineDecoder implements ProtocolDecoder<ByteBuffer, String, Text
/** The number of lines found so far */
private int matchCount = 0;
-
+
/**
* Overflow length
*/
@@ -368,7 +354,7 @@ public class TextLineDecoder implements ProtocolDecoder<ByteBuffer, String, Text
public void setMatchCount(int matchCount) {
this.matchCount = matchCount;
}
-
+
public int getOverflowLength() {
return overflowLength;
}
@@ -388,6 +374,7 @@ public class TextLineDecoder implements ProtocolDecoder<ByteBuffer, String, Text
buf = b;
}
}
+
public void append(ByteBuffer in) {
if (buf.position() > maxLineLength - in.remaining()) {
overflowLength = buf.position() + in.remaining();
http://git-wip-us.apache.org/repos/asf/mina/blob/3d3767d6/codec/src/test/java/org/apache/mina/codec/textline/AutoTextLineDecoderTest.java
----------------------------------------------------------------------
diff --git a/codec/src/test/java/org/apache/mina/codec/textline/AutoTextLineDecoderTest.java b/codec/src/test/java/org/apache/mina/codec/textline/AutoTextLineDecoderTest.java
index ca087c4..54c13fe 100644
--- a/codec/src/test/java/org/apache/mina/codec/textline/AutoTextLineDecoderTest.java
+++ b/codec/src/test/java/org/apache/mina/codec/textline/AutoTextLineDecoderTest.java
@@ -19,8 +19,7 @@
*/
package org.apache.mina.codec.textline;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.*;
import java.nio.ByteBuffer;
@@ -29,7 +28,7 @@ import org.junit.Test;
/**
* A {@link TextLineDecoder} test.
- *
+ *
* @author <a href="http://mina.apache.org">Apache MINA Project</a>
*/
public class AutoTextLineDecoderTest {
@@ -38,56 +37,49 @@ public class AutoTextLineDecoderTest {
public void testThatEmptyBufferReturnsEmptyResult() {
TextLineDecoder decoder = new TextLineDecoder();
Context context = decoder.createDecoderState();
- String[] results = decoder.decode(ByteBuffer.allocate(0), context);
- assertNotNull(results);
- assertEquals(0, results.length);
+ String results = decoder.decode(ByteBuffer.allocate(0), context);
+ assertNull(results);
}
-
+
@Test
public void testThatNonLineTerminatedStringReturnsEmptyResult() {
TextLineDecoder decoder = new TextLineDecoder();
Context context = decoder.createDecoderState();
- String[] results = decoder.decode(ByteBuffer.wrap("a string".getBytes()), context);
- assertNotNull(results);
- assertEquals(0, results.length);
+ String results = decoder.decode(ByteBuffer.wrap("a string".getBytes()), context);
+ assertNull(results);
assertEquals(8, context.getBuffer().position());
}
-
+
@Test
public void testThatUnixLineTerminatedStringReturnsNonEmptyResult() {
TextLineDecoder decoder = new TextLineDecoder();
Context context = decoder.createDecoderState();
- String[] results = decoder.decode(ByteBuffer.wrap("a string\n".getBytes()), context);
- assertNotNull(results);
- assertEquals(1, results.length);
- assertEquals("a string", results[0]);
+ String results = decoder.decode(ByteBuffer.wrap("a string\n".getBytes()), context);
+ assertEquals("a string", results);
assertEquals(0, context.getBuffer().position());
}
-
+
@Test
public void testThatWindowsLineTerminatedStringReturnsNonEmptyResult() {
TextLineDecoder decoder = new TextLineDecoder();
Context context = decoder.createDecoderState();
- String[] results = decoder.decode(ByteBuffer.wrap("a string\r\n".getBytes()), context);
+ String results = decoder.decode(ByteBuffer.wrap("a string\r\n".getBytes()), context);
assertNotNull(results);
- assertEquals(1, results.length);
- assertEquals("a string", results[0]);
+ assertEquals("a string", results);
assertEquals(0, context.getBuffer().position());
}
-
+
@Test
public void testThatContextIsMaintainedBetweenMessages() {
TextLineDecoder decoder = new TextLineDecoder();
Context context = decoder.createDecoderState();
- String[] results = decoder.decode(ByteBuffer.wrap("a string\na".getBytes()), context);
+ String results = decoder.decode(ByteBuffer.wrap("a string\na".getBytes()), context);
assertNotNull(results);
- assertEquals(1, results.length);
- assertEquals("a string", results[0]);
+ assertEquals("a string", results);
assertEquals(1, context.getBuffer().position());
results = decoder.decode(ByteBuffer.wrap(" string\n".getBytes()), context);
assertNotNull(results);
- assertEquals(1, results.length);
- assertEquals("a string", results[0]);
+ assertEquals("a string", results);
assertEquals(0, context.getBuffer().position());
}
@@ -96,28 +88,26 @@ public class AutoTextLineDecoderTest {
TextLineDecoder decoder = new TextLineDecoder();
Context context = decoder.createDecoderState();
StringBuffer sb = new StringBuffer();
- for(int i=0; i < 100;++i) {
+ for (int i = 0; i < 100; ++i) {
sb.append("a string");
}
- String[] results = decoder.decode(ByteBuffer.wrap((sb.toString() + "\n").getBytes()), context);
+ String results = decoder.decode(ByteBuffer.wrap((sb.toString() + "\n").getBytes()), context);
assertNotNull(results);
- assertEquals(1, results.length);
- assertEquals(sb.toString(), results[0]);
+ assertEquals(sb.toString(), results);
assertEquals(0, context.getBuffer().position());
}
-
+
@Test
public void testThatWindowsLineTerminatedLongStringReturnsNonEmptyResult() {
TextLineDecoder decoder = new TextLineDecoder();
Context context = decoder.createDecoderState();
StringBuffer sb = new StringBuffer();
- for(int i=0; i < 100;++i) {
+ for (int i = 0; i < 100; ++i) {
sb.append("a string");
}
- String[] results = decoder.decode(ByteBuffer.wrap((sb.toString() + "\r\n").getBytes()), context);
+ String results = decoder.decode(ByteBuffer.wrap((sb.toString() + "\r\n").getBytes()), context);
assertNotNull(results);
- assertEquals(1, results.length);
- assertEquals(sb.toString(), results[0]);
+ assertEquals(sb.toString(), results);
assertEquals(0, context.getBuffer().position());
}
}
http://git-wip-us.apache.org/repos/asf/mina/blob/3d3767d6/codec/src/test/java/org/apache/mina/codec/textline/UnixTextLineDecoderTest.java
----------------------------------------------------------------------
diff --git a/codec/src/test/java/org/apache/mina/codec/textline/UnixTextLineDecoderTest.java b/codec/src/test/java/org/apache/mina/codec/textline/UnixTextLineDecoderTest.java
index 2023187..4eac157 100644
--- a/codec/src/test/java/org/apache/mina/codec/textline/UnixTextLineDecoderTest.java
+++ b/codec/src/test/java/org/apache/mina/codec/textline/UnixTextLineDecoderTest.java
@@ -19,8 +19,7 @@
*/
package org.apache.mina.codec.textline;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.*;
import java.nio.ByteBuffer;
@@ -29,7 +28,7 @@ import org.junit.Test;
/**
* A {@link TextLineDecoder} test.
- *
+ *
* @author <a href="http://mina.apache.org">Apache MINA Project</a>
*/
public class UnixTextLineDecoderTest {
@@ -38,56 +37,50 @@ public class UnixTextLineDecoderTest {
public void testThatEmptyBufferReturnsEmptyResult() {
TextLineDecoder decoder = new TextLineDecoder(LineDelimiter.UNIX);
Context context = decoder.createDecoderState();
- String[] results = decoder.decode(ByteBuffer.allocate(0), context);
- assertNotNull(results);
- assertEquals(0, results.length);
+ String results = decoder.decode(ByteBuffer.allocate(0), context);
+ assertNull(results);
}
-
+
@Test
public void testThatNonLineTerminatedStringReturnsEmptyResult() {
TextLineDecoder decoder = new TextLineDecoder(LineDelimiter.UNIX);
Context context = decoder.createDecoderState();
- String[] results = decoder.decode(ByteBuffer.wrap("a string".getBytes()), context);
- assertNotNull(results);
- assertEquals(0, results.length);
+ String results = decoder.decode(ByteBuffer.wrap("a string".getBytes()), context);
+ assertNull(results);
assertEquals(8, context.getBuffer().position());
}
-
+
@Test
public void testThatUnixLineTerminatedStringReturnsNonEmptyResult() {
TextLineDecoder decoder = new TextLineDecoder(LineDelimiter.UNIX);
Context context = decoder.createDecoderState();
- String[] results = decoder.decode(ByteBuffer.wrap("a string\n".getBytes()), context);
+ String results = decoder.decode(ByteBuffer.wrap("a string\n".getBytes()), context);
assertNotNull(results);
- assertEquals(1, results.length);
- assertEquals("a string", results[0]);
+ assertEquals("a string", results);
assertEquals(0, context.getBuffer().position());
}
-
+
@Test
public void testThatWindowsLineTerminatedStringReturnsNonEmptyResult() {
TextLineDecoder decoder = new TextLineDecoder(LineDelimiter.UNIX);
Context context = decoder.createDecoderState();
- String[] results = decoder.decode(ByteBuffer.wrap("a string\r\n".getBytes()), context);
+ String results = decoder.decode(ByteBuffer.wrap("a string\r\n".getBytes()), context);
assertNotNull(results);
- assertEquals(1, results.length);
- assertEquals("a string\r", results[0]);
+ assertEquals("a string\r", results);
assertEquals(0, context.getBuffer().position());
}
-
+
@Test
public void testThatContextIsMaintainedBetweenMessages() {
TextLineDecoder decoder = new TextLineDecoder(LineDelimiter.UNIX);
Context context = decoder.createDecoderState();
- String[] results = decoder.decode(ByteBuffer.wrap("a string\na".getBytes()), context);
+ String results = decoder.decode(ByteBuffer.wrap("a string\na".getBytes()), context);
assertNotNull(results);
- assertEquals(1, results.length);
- assertEquals("a string", results[0]);
+ assertEquals("a string", results);
assertEquals(1, context.getBuffer().position());
results = decoder.decode(ByteBuffer.wrap(" string\n".getBytes()), context);
assertNotNull(results);
- assertEquals(1, results.length);
- assertEquals("a string", results[0]);
+ assertEquals("a string", results);
assertEquals(0, context.getBuffer().position());
}
@@ -96,28 +89,26 @@ public class UnixTextLineDecoderTest {
TextLineDecoder decoder = new TextLineDecoder(LineDelimiter.UNIX);
Context context = decoder.createDecoderState();
StringBuffer sb = new StringBuffer();
- for(int i=0; i < 100;++i) {
+ for (int i = 0; i < 100; ++i) {
sb.append("a string");
}
- String[] results = decoder.decode(ByteBuffer.wrap((sb.toString() + "\n").getBytes()), context);
+ String results = decoder.decode(ByteBuffer.wrap((sb.toString() + "\n").getBytes()), context);
assertNotNull(results);
- assertEquals(1, results.length);
- assertEquals(sb.toString(), results[0]);
+ assertEquals(sb.toString(), results);
assertEquals(0, context.getBuffer().position());
}
-
+
@Test
public void testThatWindowsLineTerminatedLongStringReturnsNonEmptyResult() {
TextLineDecoder decoder = new TextLineDecoder(LineDelimiter.UNIX);
Context context = decoder.createDecoderState();
StringBuffer sb = new StringBuffer();
- for(int i=0; i < 100;++i) {
+ for (int i = 0; i < 100; ++i) {
sb.append("a string");
}
- String[] results = decoder.decode(ByteBuffer.wrap((sb.toString() + "\r\n").getBytes()), context);
+ String results = decoder.decode(ByteBuffer.wrap((sb.toString() + "\r\n").getBytes()), context);
assertNotNull(results);
- assertEquals(1, results.length);
- assertEquals(sb.toString() + "\r", results[0]);
+ assertEquals(sb.toString() + "\r", results);
assertEquals(0, context.getBuffer().position());
}
}
http://git-wip-us.apache.org/repos/asf/mina/blob/3d3767d6/codec/src/test/java/org/apache/mina/codec/textline/WindowsTextLineDecoderTest.java
----------------------------------------------------------------------
diff --git a/codec/src/test/java/org/apache/mina/codec/textline/WindowsTextLineDecoderTest.java b/codec/src/test/java/org/apache/mina/codec/textline/WindowsTextLineDecoderTest.java
index 727fd17..139d211 100644
--- a/codec/src/test/java/org/apache/mina/codec/textline/WindowsTextLineDecoderTest.java
+++ b/codec/src/test/java/org/apache/mina/codec/textline/WindowsTextLineDecoderTest.java
@@ -19,8 +19,7 @@
*/
package org.apache.mina.codec.textline;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.*;
import java.nio.ByteBuffer;
@@ -29,7 +28,7 @@ import org.junit.Test;
/**
* A {@link TextLineDecoder} test.
- *
+ *
* @author <a href="http://mina.apache.org">Apache MINA Project</a>
*/
public class WindowsTextLineDecoderTest {
@@ -38,55 +37,49 @@ public class WindowsTextLineDecoderTest {
public void testThatEmptyBufferReturnsEmptyResult() {
TextLineDecoder decoder = new TextLineDecoder(LineDelimiter.WINDOWS);
Context context = decoder.createDecoderState();
- String[] results = decoder.decode(ByteBuffer.allocate(0), context);
- assertNotNull(results);
- assertEquals(0, results.length);
+ String results = decoder.decode(ByteBuffer.allocate(0), context);
+ assertNull(results);
}
-
+
@Test
public void testThatNonLineTerminatedStringReturnsEmptyResult() {
TextLineDecoder decoder = new TextLineDecoder(LineDelimiter.WINDOWS);
Context context = decoder.createDecoderState();
- String[] results = decoder.decode(ByteBuffer.wrap("a string".getBytes()), context);
- assertNotNull(results);
- assertEquals(0, results.length);
+ String results = decoder.decode(ByteBuffer.wrap("a string".getBytes()), context);
+ assertNull(results);
assertEquals(8, context.getBuffer().position());
}
-
+
@Test
public void testThatUnixLineTerminatedStringReturnsEmptyResult() {
TextLineDecoder decoder = new TextLineDecoder(LineDelimiter.WINDOWS);
Context context = decoder.createDecoderState();
- String[] results = decoder.decode(ByteBuffer.wrap("a string\n".getBytes()), context);
- assertNotNull(results);
- assertEquals(0, results.length);
+ String results = decoder.decode(ByteBuffer.wrap("a string\n".getBytes()), context);
+ assertNull(results);
assertEquals(9, context.getBuffer().position());
}
-
+
@Test
public void testThatWindowsLineTerminatedStringReturnsNonEmptyResult() {
TextLineDecoder decoder = new TextLineDecoder(LineDelimiter.WINDOWS);
Context context = decoder.createDecoderState();
- String[] results = decoder.decode(ByteBuffer.wrap("a string\r\n".getBytes()), context);
+ String results = decoder.decode(ByteBuffer.wrap("a string\r\n".getBytes()), context);
assertNotNull(results);
- assertEquals(1, results.length);
- assertEquals("a string", results[0]);
+ assertEquals("a string", results);
assertEquals(0, context.getBuffer().position());
}
-
+
@Test
public void testThatContextIsMaintainedBetweenMessages() {
TextLineDecoder decoder = new TextLineDecoder(LineDelimiter.WINDOWS);
Context context = decoder.createDecoderState();
- String[] results = decoder.decode(ByteBuffer.wrap("a string\r\na".getBytes()), context);
+ String results = decoder.decode(ByteBuffer.wrap("a string\r\na".getBytes()), context);
assertNotNull(results);
- assertEquals(1, results.length);
- assertEquals("a string", results[0]);
+ assertEquals("a string", results);
assertEquals(1, context.getBuffer().position());
results = decoder.decode(ByteBuffer.wrap(" string\r\n".getBytes()), context);
assertNotNull(results);
- assertEquals(1, results.length);
- assertEquals("a string", results[0]);
+ assertEquals("a string", results);
assertEquals(0, context.getBuffer().position());
}
@@ -95,27 +88,25 @@ public class WindowsTextLineDecoderTest {
TextLineDecoder decoder = new TextLineDecoder(LineDelimiter.WINDOWS);
Context context = decoder.createDecoderState();
StringBuffer sb = new StringBuffer();
- for(int i=0; i < 100;++i) {
+ for (int i = 0; i < 100; ++i) {
sb.append("a string");
}
- String[] results = decoder.decode(ByteBuffer.wrap((sb.toString() + "\n").getBytes()), context);
- assertNotNull(results);
- assertEquals(0, results.length);
+ String results = decoder.decode(ByteBuffer.wrap((sb.toString() + "\n").getBytes()), context);
+ assertNull(results);
assertEquals(801, context.getBuffer().position());
}
-
+
@Test
public void testThatWindowsLineTerminatedLongStringReturnsNonEmptyResult() {
TextLineDecoder decoder = new TextLineDecoder(LineDelimiter.WINDOWS);
Context context = decoder.createDecoderState();
StringBuffer sb = new StringBuffer();
- for(int i=0; i < 100;++i) {
+ for (int i = 0; i < 100; ++i) {
sb.append("a string");
}
- String[] results = decoder.decode(ByteBuffer.wrap((sb.toString() + "\r\n").getBytes()), context);
+ String results = decoder.decode(ByteBuffer.wrap((sb.toString() + "\r\n").getBytes()), context);
assertNotNull(results);
- assertEquals(1, results.length);
- assertEquals(sb.toString(), results[0]);
+ assertEquals(sb.toString(), results);
assertEquals(0, context.getBuffer().position());
}
}
http://git-wip-us.apache.org/repos/asf/mina/blob/3d3767d6/core/src/main/java/org/apache/mina/filter/codec/ProtocolCodecFilter.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mina/filter/codec/ProtocolCodecFilter.java b/core/src/main/java/org/apache/mina/filter/codec/ProtocolCodecFilter.java
index 1773a59..8f07ed7 100644
--- a/core/src/main/java/org/apache/mina/filter/codec/ProtocolCodecFilter.java
+++ b/core/src/main/java/org/apache/mina/filter/codec/ProtocolCodecFilter.java
@@ -85,12 +85,10 @@ public class ProtocolCodecFilter<MESSAGE, ENCODED, ENCODING_STATE, DECODING_STAT
DECODING_STATE state = getDecodingState(session);
// Loop until the decoder cannot decode more
- MESSAGE[] msg;
+ MESSAGE msg;
try {
- while (((msg = decoder.decode((ENCODED) in, state)) != null) && (msg.length > 0)) {
- for (MESSAGE m : msg) {
- controller.callReadNextFilter(m);
- }
+ while (((msg = decoder.decode((ENCODED) in, state)) != null)) {
+ controller.callReadNextFilter(msg);
}
} catch (ProtocolDecoderException e) {
LOGGER.debug("decoding exception : ", e);
http://git-wip-us.apache.org/repos/asf/mina/blob/3d3767d6/http/src/main/java/org/apache/mina/http/DecoderState.java
----------------------------------------------------------------------
diff --git a/http/src/main/java/org/apache/mina/http/DecoderState.java b/http/src/main/java/org/apache/mina/http/DecoderState.java
index ecf6dba..eb95bfe 100644
--- a/http/src/main/java/org/apache/mina/http/DecoderState.java
+++ b/http/src/main/java/org/apache/mina/http/DecoderState.java
@@ -26,5 +26,6 @@ package org.apache.mina.http;
public enum DecoderState {
NEW, // waiting for a new HTTP requests, the session is new of last request was completed
HEAD, // accumulating the HTTP request head (everything before the body)
- BODY // receiving HTTP body slices
+ BODY, // receiving HTTP body slices
+ DONE // end of decoding
}
http://git-wip-us.apache.org/repos/asf/mina/blob/3d3767d6/http/src/main/java/org/apache/mina/http/HttpServerDecoder.java
----------------------------------------------------------------------
diff --git a/http/src/main/java/org/apache/mina/http/HttpServerDecoder.java b/http/src/main/java/org/apache/mina/http/HttpServerDecoder.java
index f99caef..c5fed39 100644
--- a/http/src/main/java/org/apache/mina/http/HttpServerDecoder.java
+++ b/http/src/main/java/org/apache/mina/http/HttpServerDecoder.java
@@ -79,7 +79,7 @@ public class HttpServerDecoder implements ProtocolDecoder<ByteBuffer, HttpPdu, H
* {@inheritDoc}
*/
@Override
- public HttpPdu[] decode(ByteBuffer msg, HttpDecoderState context) throws ProtocolDecoderException {
+ public HttpPdu decode(ByteBuffer msg, HttpDecoderState context) throws ProtocolDecoderException {
LOG.debug("decode : {}", msg);
if (msg.remaining() <= 0) {
return null;
@@ -103,7 +103,7 @@ public class HttpServerDecoder implements ProtocolDecoder<ByteBuffer, HttpPdu, H
context.getPartial().put(msg);
context.getPartial().flip();
} else {
- return new HttpPdu[] { rq };
+ return rq;
}
return null;
case BODY:
@@ -118,10 +118,13 @@ public class HttpServerDecoder implements ProtocolDecoder<ByteBuffer, HttpPdu, H
LOG.debug("end of HTTP body");
context.setState(DecoderState.NEW);
context.setRemainingBytes(0);
- return new HttpPdu[] { chunk, new HttpEndOfContent() };
+ context.setState(DecoderState.DONE);
+ return chunk;
+
}
break;
-
+ case DONE:
+ return new HttpEndOfContent();
default:
throw new RuntimeException("Unknonwn decoder state : " + context.getState());
}
http://git-wip-us.apache.org/repos/asf/mina/blob/3d3767d6/http/src/test/java/org/apache/mina/http/HttpServerDecoderTest.java
----------------------------------------------------------------------
diff --git a/http/src/test/java/org/apache/mina/http/HttpServerDecoderTest.java b/http/src/test/java/org/apache/mina/http/HttpServerDecoderTest.java
index f811759..99dcc04 100644
--- a/http/src/test/java/org/apache/mina/http/HttpServerDecoderTest.java
+++ b/http/src/test/java/org/apache/mina/http/HttpServerDecoderTest.java
@@ -44,10 +44,10 @@ public class HttpServerDecoderTest {
buffer.rewind();
HttpServerDecoder decoder = new HttpServerDecoder();
HttpDecoderState state = decoder.createDecoderState();
- HttpPdu[] pdus = decoder.decode(buffer, state);
+ HttpPdu pdus = decoder.decode(buffer, state);
assertNotNull(pdus);
- assertEquals(1, pdus.length);
- assertEquals("localhost", ((HttpRequestImpl) pdus[0]).getHeader("host"));
+
+ assertEquals("localhost", ((HttpRequestImpl) pdus).getHeader("host"));
}
@Test
@@ -59,10 +59,9 @@ public class HttpServerDecoderTest {
buffer.rewind();
HttpServerDecoder decoder = new HttpServerDecoder();
HttpDecoderState state = decoder.createDecoderState();
- HttpPdu[] pdus = decoder.decode(buffer, state);
+ HttpPdu pdus = decoder.decode(buffer, state);
assertNotNull(pdus);
- assertEquals(1, pdus.length);
- assertEquals("localhost", ((HttpRequestImpl) pdus[0]).getHeader("host"));
+ assertEquals("localhost", ((HttpRequestImpl) pdus).getHeader("host"));
}
@Test
@@ -74,9 +73,8 @@ public class HttpServerDecoderTest {
buffer.rewind();
HttpServerDecoder decoder = new HttpServerDecoder();
HttpDecoderState state = decoder.createDecoderState();
- HttpPdu[] pdus = decoder.decode(buffer, state);
+ HttpPdu pdus = decoder.decode(buffer, state);
assertNotNull(pdus);
- assertEquals(1, pdus.length);
- assertEquals("localhost", ((HttpRequestImpl) pdus[0]).getHeader("host"));
+ assertEquals("localhost", ((HttpRequestImpl) pdus).getHeader("host"));
}
}