Author: elecharny
Date: Thu Dec 1 00:10:51 2011
New Revision: 1208900
URL: http://svn.apache.org/viewvc?rev=1208900&view=rev
Log:
Used IoBuffer to read data from the incoming socket.
Modified:
mina/trunk/core/src/main/java/org/apache/mina/filter/codec/ProtocolCodecFilter.java
mina/trunk/core/src/main/java/org/apache/mina/filter/codec/ProtocolDecoder.java
mina/trunk/core/src/main/java/org/apache/mina/filter/logging/LoggingFilter.java
mina/trunk/core/src/main/java/org/apache/mina/session/AbstractIoSession.java
mina/trunk/core/src/main/java/org/apache/mina/transport/tcp/NioSelectorProcessor.java
mina/trunk/core/src/main/java/org/apache/mina/util/ByteBufferDumper.java
mina/trunk/core/src/main/java/org/apache/mina/util/IoBuffer.java
mina/trunk/core/src/test/java/org/apache/mina/util/IoBufferTest.java
mina/trunk/examples/src/main/java/org/apache/mina/examples/http/HttpTest.java
mina/trunk/http/src/main/java/org/apache/mina/http/HttpServerDecoder.java
mina/trunk/ldap/src/main/java/org/apache/mina/ldap/LdapProtocolDecoder.java
Modified:
mina/trunk/core/src/main/java/org/apache/mina/filter/codec/ProtocolCodecFilter.java
URL:
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/filter/codec/ProtocolCodecFilter.java?rev=1208900&r1=1208899&r2=1208900&view=diff
==============================================================================
---
mina/trunk/core/src/main/java/org/apache/mina/filter/codec/ProtocolCodecFilter.java
(original)
+++
mina/trunk/core/src/main/java/org/apache/mina/filter/codec/ProtocolCodecFilter.java
Thu Dec 1 00:10:51 2011
@@ -19,13 +19,12 @@
*/
package org.apache.mina.filter.codec;
-import java.nio.ByteBuffer;
-
import org.apache.mina.api.DefaultIoFilter;
import org.apache.mina.api.IoFilter;
import org.apache.mina.api.IoSession;
import org.apache.mina.filterchain.ReadFilterChainController;
import org.apache.mina.filterchain.WriteFilterChainController;
+import org.apache.mina.util.IoBuffer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -69,7 +68,7 @@ public class ProtocolCodecFilter extends
/**
* Creates a new instance of ProtocolCodecFilter, without any factory.
* The encoder/decoder factory will be created as an inner class, using
- * the two parameters (encoder and decoder).
+ * the two parameters (encoder and decoder).
*
* @param encoder The class responsible for encoding the message
* @param decoder The class responsible for decoding the message
@@ -188,7 +187,7 @@ public class ProtocolCodecFilter extends
* throws an exception.
* <code>
* while ( buffer not empty )
- * try
+ * try
* decode ( buffer )
* catch
* break;
@@ -198,17 +197,17 @@ public class ProtocolCodecFilter extends
public void messageReceived(IoSession session, Object message,
ReadFilterChainController controller) {
LOGGER.debug("Processing a MESSAGE_RECEIVED for session {}", session);
- if (!(message instanceof ByteBuffer)) {
+ if (!(message instanceof IoBuffer)) {
controller.callReadNextFilter(session, message);
return;
}
- ByteBuffer in = (ByteBuffer) message;
+ IoBuffer in = (IoBuffer) message;
ProtocolDecoder decoder = getDecoder(session);
// Loop until we don't have anymore byte in the buffer,
- // or until the decoder throws an unrecoverable exception or
- // can't decoder a message, because there are not enough
+ // or until the decoder throws an unrecoverable exception or
+ // can't decoder a message, because there are not enough
// data in the buffer
while (in.hasRemaining()) {
// Call the decoder with the read bytes
Modified:
mina/trunk/core/src/main/java/org/apache/mina/filter/codec/ProtocolDecoder.java
URL:
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/filter/codec/ProtocolDecoder.java?rev=1208900&r1=1208899&r2=1208900&view=diff
==============================================================================
---
mina/trunk/core/src/main/java/org/apache/mina/filter/codec/ProtocolDecoder.java
(original)
+++
mina/trunk/core/src/main/java/org/apache/mina/filter/codec/ProtocolDecoder.java
Thu Dec 1 00:10:51 2011
@@ -19,10 +19,9 @@
*/
package org.apache.mina.filter.codec;
-import java.nio.ByteBuffer;
-
import org.apache.mina.api.IoSession;
import org.apache.mina.filterchain.ReadFilterChainController;
+import org.apache.mina.util.IoBuffer;
/**
* Decodes binary or protocol-specific data into higher-level message objects.
@@ -49,7 +48,7 @@ public interface ProtocolDecoder {
*
* @throws Exception if the read data violated protocol specification
*/
- Object decode(IoSession session, ByteBuffer in, ReadFilterChainController
controller);// throws Exception;
+ Object decode(IoSession session, IoBuffer in, ReadFilterChainController
controller);// throws Exception;
/**
* Invoked when the specified <tt>session</tt> is closed. This method is
useful
Modified:
mina/trunk/core/src/main/java/org/apache/mina/filter/logging/LoggingFilter.java
URL:
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/filter/logging/LoggingFilter.java?rev=1208900&r1=1208899&r2=1208900&view=diff
==============================================================================
---
mina/trunk/core/src/main/java/org/apache/mina/filter/logging/LoggingFilter.java
(original)
+++
mina/trunk/core/src/main/java/org/apache/mina/filter/logging/LoggingFilter.java
Thu Dec 1 00:10:51 2011
@@ -27,6 +27,7 @@ import org.apache.mina.api.IoSession;
import org.apache.mina.filterchain.ReadFilterChainController;
import org.apache.mina.filterchain.WriteFilterChainController;
import org.apache.mina.util.ByteBufferDumper;
+import org.apache.mina.util.IoBuffer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -94,7 +95,7 @@ public class LoggingFilter implements Io
/**
* Log if the logger and the current event log level are compatible. We log
- * a formated message and its parameters.
+ * a formated message and its parameters.
*
* @param eventLevel the event log level as requested by the user
* @param message the formated message to log
@@ -124,7 +125,7 @@ public class LoggingFilter implements Io
/**
* Log if the logger and the current event log level are compatible. We log
- * a simple message.
+ * a simple message.
*
* @param eventLevel the event log level as requested by the user
* @param message the message to log
@@ -189,8 +190,8 @@ public class LoggingFilter implements Io
*/
@Override
public void messageReceived(IoSession session, Object message,
ReadFilterChainController controller) {
- if (message instanceof ByteBuffer) {
- log(messageReceivedLevel, "RECEIVED: {}",
ByteBufferDumper.dump((ByteBuffer) message));
+ if (message instanceof IoBuffer) {
+ log(messageReceivedLevel, "RECEIVED: {}",
ByteBufferDumper.dump((IoBuffer) message));
} else {
log(messageReceivedLevel, "RECEIVED: {}", message);
}
Modified:
mina/trunk/core/src/main/java/org/apache/mina/session/AbstractIoSession.java
URL:
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/session/AbstractIoSession.java?rev=1208900&r1=1208899&r2=1208900&view=diff
==============================================================================
---
mina/trunk/core/src/main/java/org/apache/mina/session/AbstractIoSession.java
(original)
+++
mina/trunk/core/src/main/java/org/apache/mina/session/AbstractIoSession.java
Thu Dec 1 00:10:51 2011
@@ -32,6 +32,7 @@ import org.apache.mina.api.IoSession;
import org.apache.mina.filterchain.DefaultIoFilterController;
import org.apache.mina.filterchain.IoFilterController;
import org.apache.mina.service.SelectorProcessor;
+import org.apache.mina.util.IoBuffer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -85,6 +86,9 @@ public abstract class AbstractIoSession
private Queue<WriteRequest> writeQueue = new DefaultWriteQueue();
private IoFilterController filterProcessor;
+
+ /** The internal buffer used to store incoming data */
+ private IoBuffer ioBuffer;
/**
* Create an {@link org.apache.mina.api.IoSession} with a unique
identifier (
@@ -100,6 +104,7 @@ public abstract class AbstractIoSession
this.service = service;
this.writeProcessor = writeProcessor;
this.filterProcessor = new
DefaultIoFilterController(service.getFilters());
+ ioBuffer = new IoBuffer();
LOG.debug("Created new session with id : {}", id);
synchronized (stateMonitor) {
@@ -281,4 +286,11 @@ public abstract class AbstractIoSession
public IoFilterController getFilterChain() {
return filterProcessor;
}
+
+ /**
+ * @return The inner buffer
+ */
+ public IoBuffer getIoBuffer() {
+ return ioBuffer;
+ }
}
\ No newline at end of file
Modified:
mina/trunk/core/src/main/java/org/apache/mina/transport/tcp/NioSelectorProcessor.java
URL:
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/transport/tcp/NioSelectorProcessor.java?rev=1208900&r1=1208899&r2=1208900&view=diff
==============================================================================
---
mina/trunk/core/src/main/java/org/apache/mina/transport/tcp/NioSelectorProcessor.java
(original)
+++
mina/trunk/core/src/main/java/org/apache/mina/transport/tcp/NioSelectorProcessor.java
Thu Dec 1 00:10:51 2011
@@ -46,6 +46,7 @@ import org.apache.mina.service.SelectorS
import org.apache.mina.session.DefaultWriteFuture;
import org.apache.mina.session.WriteRequest;
import org.apache.mina.transport.tcp.nio.NioTcpServer;
+import org.apache.mina.util.IoBuffer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -337,7 +338,6 @@ public class NioSelectorProcessor implem
LOGGER.debug("... done selecting : {}", readyCount);
if (readyCount > 0) {
-
// process selected keys
Iterator<SelectionKey> selectedKeys =
selector.selectedKeys().iterator();
@@ -348,27 +348,37 @@ public class NioSelectorProcessor implem
if (!key.isValid()) {
continue;
}
+
selector.selectedKeys().remove(key);
if (key.isReadable()) {
LOGGER.debug("readable client {}", key);
NioTcpSession session = (NioTcpSession)
key.attachment();
SocketChannel channel =
session.getSocketChannel();
- readBuffer.rewind();
- int readCount = channel.read(readBuffer);
- LOGGER.debug("read {} bytes", readCount);
- if (readCount < 0) {
- // session closed by the remote peer
- LOGGER.debug("session closed by the remote
peer");
- sessionsToClose.add(session);
- } else {
- // we have read some data
- // limit at the current position & rewind
buffer back to start & push to the chain
- readBuffer.flip();
-
session.getFilterChain().processMessageReceived(session, readBuffer);
- }
-
+ int readCount = 0;
+ IoBuffer ioBuffer = session.getIoBuffer();
+
+ do {
+ ByteBuffer readBuffer =
ByteBuffer.allocate(1024);
+ readCount = channel.read(readBuffer);
+ LOGGER.debug("read {} bytes", readCount);
+
+ if (readCount < 0) {
+ // session closed by the remote peer
+ LOGGER.debug("session closed by the
remote peer");
+ sessionsToClose.add(session);
+ break;
+ } else if (readCount > 0) {
+ readBuffer.flip();
+ ioBuffer.add(readBuffer);
+ }
+ } while (readCount > 0);
+
+ // we have read some data
+ // limit at the current position & rewind
buffer back to start & push to the chain
+
session.getFilterChain().processMessageReceived(session, ioBuffer);
}
+
if (key.isWritable()) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("writable session : {}",
key.attachment());
@@ -377,6 +387,7 @@ public class NioSelectorProcessor implem
session.setNotRegisteredForWrite();
// write from the session write queue
Queue<WriteRequest> queue =
session.getWriteQueue();
+
do {
// get a write request from the queue
WriteRequest wreq = queue.peek();
Modified:
mina/trunk/core/src/main/java/org/apache/mina/util/ByteBufferDumper.java
URL:
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/util/ByteBufferDumper.java?rev=1208900&r1=1208899&r2=1208900&view=diff
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/util/ByteBufferDumper.java
(original)
+++ mina/trunk/core/src/main/java/org/apache/mina/util/ByteBufferDumper.java
Thu Dec 1 00:10:51 2011
@@ -55,6 +55,28 @@ public class ByteBufferDumper {
highDigits = high;
lowDigits = low;
}
+
+ public static String dump(IoBuffer buffer) {
+ StringBuilder sb = new StringBuilder();
+
+ boolean isFirst = true;
+
+ for (int i = 0; i < buffer.limit(); i++) {
+ int byteValue = buffer.get(i) & 0xFF;
+
+ if (isFirst) {
+ isFirst = false;
+ } else {
+ sb.append(' ');
+ }
+
+ sb.append("0x");
+ sb.append((char) highDigits[byteValue]);
+ sb.append((char) lowDigits[byteValue]);
+ }
+
+ return sb.toString();
+ }
public static String dump(ByteBuffer buffer) {
Modified: mina/trunk/core/src/main/java/org/apache/mina/util/IoBuffer.java
URL:
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/util/IoBuffer.java?rev=1208900&r1=1208899&r2=1208900&view=diff
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/util/IoBuffer.java (original)
+++ mina/trunk/core/src/main/java/org/apache/mina/util/IoBuffer.java Thu Dec 1
00:10:51 2011
@@ -22,14 +22,7 @@ import java.nio.Buffer;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
-import java.nio.CharBuffer;
-import java.nio.DoubleBuffer;
-import java.nio.FloatBuffer;
-import java.nio.IntBuffer;
-import java.nio.LongBuffer;
import java.nio.ReadOnlyBufferException;
-import java.nio.ShortBuffer;
-
import java.lang.UnsupportedOperationException;
/**
@@ -71,6 +64,9 @@ public class IoBuffer {
/** A empty bytes array */
private final static byte[] EMPTY_BYTES = new byte[]{};
+ /** <code>UNSET_MARK</code> means the mark has not been set. */
+ private final static int UNSET_MARK = -1;
+
/**
* Construct a IoBuffer, with no buffer in it
*/
@@ -117,17 +113,28 @@ public class IoBuffer {
}
/**
- * Construct a IoBuffer with no ByteBuffer. The IoBuffer type is not
direct.
- * (ie direct or heap will be deduce from this first ByteBuffer
characteristic.
- * @param byteBuffer the first ByteBuffer added to the IoBuffer list
+ * Construct a IoBuffer from an existing IoBuffer.
+ * @param ioBuffer the IoBuffer we want to copy
*/
- public IoBuffer(ByteBuffer byteBuffer) {
- buffers.add(byteBuffer);
- position = 0;
+ public IoBuffer(IoBuffer ioBuffer) {
+ // Find the position to start with
+ BufferNode node = ioBuffer.buffers.getFirst();
+ int pos = 0;
+
+ while (node != null) {
+ if (node.offset + node.buffer.limit() < position) {
+ node = buffers.getNext();
+ pos = node.offset + node.buffer.limit();
+ } else {
+ buffers.add(node.buffer);
+ }
+ }
+
+ position = position - pos;
mark = 0;
- limit = byteBuffer.limit();
- type = byteBuffer.isDirect() ? BufferType.DIRECT : BufferType.HEAP;
- order = byteBuffer.order();
+ limit = ioBuffer.limit() - pos;
+ type = ioBuffer.type;
+ order = ioBuffer.order();
}
/**
@@ -227,13 +234,14 @@ public class IoBuffer {
if (isReadOnly()) {
throw new ReadOnlyBufferException();
}
+
// The offset is always 0
return 0;
}
/**
* @see ByteBuffer#asCharBuffer()
- */
+ *
public CharBuffer asCharBuffer() {
// TODO code me !
throw new UnsupportedOperationException();
@@ -241,7 +249,7 @@ public class IoBuffer {
/**
* @see ByteBuffer#asDoubleBuffer()
- */
+ *
public DoubleBuffer asDoubleBuffer() {
// TODO code me !
throw new UnsupportedOperationException();
@@ -249,7 +257,7 @@ public class IoBuffer {
/**
* @see ByteBuffer#asFloatBuffer()
- */
+ *
public FloatBuffer asFloatBuffer() {
// TODO code me !
throw new UnsupportedOperationException();
@@ -270,7 +278,7 @@ public class IoBuffer {
* position, limit and mark are independent.
*
* @return a int buffer which is based on the content of this byte buffer.
- */
+ *
public IntBuffer asIntBuffer() {
// TODO code me !
throw new UnsupportedOperationException();
@@ -278,7 +286,7 @@ public class IoBuffer {
/**
* @see ByteBuffer#asLongBuffer()
- */
+ *
public LongBuffer asLongBuffer() {
// TODO code me !
throw new UnsupportedOperationException();
@@ -294,7 +302,7 @@ public class IoBuffer {
/**
* @see ByteBuffer#asShortBuffer()
- */
+ *
public ShortBuffer asShortBuffer() {
// TODO code me !
throw new UnsupportedOperationException();
@@ -319,8 +327,10 @@ public class IoBuffer {
* @return this buffer.
*/
public IoBuffer clear() {
- // TODO code me !
- throw new UnsupportedOperationException();
+ position = 0;
+ mark = UNSET_MARK;
+
+ return this;
}
/**
@@ -367,8 +377,11 @@ public class IoBuffer {
* @return this IoBuffer.
*/
public IoBuffer flip() {
- // TODO code me !
- throw new UnsupportedOperationException();
+ limit = position;
+ position = 0;
+ mark = UNSET_MARK;
+
+ return this;
}
/**
@@ -446,10 +459,38 @@ public class IoBuffer {
/**
* @see ByteBuffer#get(int)
+ * Returns the byte at the specified index and does not change the
position.
+ *
+ * @param index the index, must not be negative and less than limit.
+ * @return the byte at the specified index.
+ * @exception IndexOutOfBoundsException if index is invalid.
*/
- public IoBuffer get(int index) {
- // TODO code me !
- throw new UnsupportedOperationException();
+ public byte get(int index) {
+ if ((index < 0) || (index >= limit)) {
+ throw new IndexOutOfBoundsException();
+ }
+
+ BufferNode currentNode = buffers.current;
+ BufferNode node = buffers.getFirst();
+
+ while (node != null) {
+ if (node.offset + node.buffer.limit() > index) {
+ byte result = node.buffer.get(index - node.offset);
+
+ // Reset the initial position before returning
+ buffers.current = currentNode;
+
+ return result;
+ } else {
+ node = buffers.getNext();
+ }
+ }
+
+ // Reset the initial position before returning
+ buffers.current = currentNode;
+
+ // Unlikely to happen
+ throw new IndexOutOfBoundsException();
}
/**
@@ -586,7 +627,7 @@ public class IoBuffer {
/**
* @see ByteBuffer#hasArray()
- */
+ *
public boolean hasArray() {
// TODO code me !
throw new UnsupportedOperationException();
@@ -651,7 +692,7 @@ public class IoBuffer {
* @param newLimit the new limit, must not be negative and not greater
than capacity.
* @return this IoBuffer.
* @exception IllegalArgumentException if <code>newLimit</code> is invalid.
- */
+ *
public IoBuffer limit(int newLimit) {
// TODO code me !
throw new UnsupportedOperationException();
@@ -665,8 +706,9 @@ public class IoBuffer {
* @return this IoBuffer.
*/
public IoBuffer mark() {
- // TODO code me !
- throw new UnsupportedOperationException();
+ mark = position;
+
+ return this;
}
/**
@@ -725,13 +767,14 @@ public class IoBuffer {
* @exception IllegalArgumentException if <code>newPosition</code> is
invalid.
*/
public IoBuffer position(int newPosition) {
- // TODO code me !
- throw new UnsupportedOperationException();
+ position = newPosition;
+
+ return this;
}
/**
* @see ByteBuffer#put(byte)
- */
+ *
public IoBuffer put(byte b) {
// TODO code me !
throw new UnsupportedOperationException();
@@ -739,7 +782,7 @@ public class IoBuffer {
/**
* @see ByteBuffer#put(byte[])
- */
+ *
public IoBuffer put(byte[] src) {
// TODO code me !
throw new UnsupportedOperationException();
@@ -747,7 +790,7 @@ public class IoBuffer {
/**
* @see ByteBuffer#put(byte[], int, int)
- */
+ *
public IoBuffer put(byte[] src, int offset, int length) {
// TODO code me !
throw new UnsupportedOperationException();
@@ -755,7 +798,7 @@ public class IoBuffer {
/**
* @see ByteBuffer#put(int, byte)
- */
+ *
public IoBuffer put(int index, byte b) {
// TODO code me !
throw new UnsupportedOperationException();
@@ -766,7 +809,7 @@ public class IoBuffer {
* The argument is also supposed to have a position set to 0.
* @param byteBuffer The added ByteBuffer.
* @return The IoBuffer instance
- */
+ *
public IoBuffer put(ByteBuffer byteBuffer) {
assert(byteBuffer != null);
@@ -782,7 +825,7 @@ public class IoBuffer {
/**
* @see ByteBuffer#putChar(char)
- */
+ *
public IoBuffer putChar(char value) {
// TODO code me !
throw new UnsupportedOperationException();
@@ -790,7 +833,7 @@ public class IoBuffer {
/**
* @see ByteBuffer#putChar(int, char)
- */
+ *
public IoBuffer putChar(int index, char value) {
// TODO code me !
throw new UnsupportedOperationException();
@@ -798,7 +841,7 @@ public class IoBuffer {
/**
* @see ByteBuffer#putDouble(double)
- */
+ *
public IoBuffer putDouble(double value) {
// TODO code me !
throw new UnsupportedOperationException();
@@ -806,7 +849,7 @@ public class IoBuffer {
/**
* @see ByteBuffer#putDouble(int, double)
- */
+ *
public IoBuffer putDouble(int index, double value) {
// TODO code me !
throw new UnsupportedOperationException();
@@ -814,7 +857,7 @@ public class IoBuffer {
/**
* @see ByteBuffer#putFloat(float)
- */
+ *
public IoBuffer putFloat(float value) {
// TODO code me !
throw new UnsupportedOperationException();
@@ -822,7 +865,7 @@ public class IoBuffer {
/**
* @see ByteBuffer#putFloat(int, float)
- */
+ *
public IoBuffer putFloat(int index, float value) {
// TODO code me !
throw new UnsupportedOperationException();
@@ -830,7 +873,7 @@ public class IoBuffer {
/**
* @see ByteBuffer#putInt(int)
- */
+ *
public IoBuffer putInt(int value) {
// TODO code me !
throw new UnsupportedOperationException();
@@ -838,7 +881,7 @@ public class IoBuffer {
/**
* @see ByteBuffer#putInt(int, int)
- */
+ *
public IoBuffer putInt(int index, int value) {
// TODO code me !
throw new UnsupportedOperationException();
@@ -846,7 +889,7 @@ public class IoBuffer {
/**
* @see ByteBuffer#putLong(long)
- */
+ *
public IoBuffer putLong(long value) {
// TODO code me !
throw new UnsupportedOperationException();
@@ -854,7 +897,7 @@ public class IoBuffer {
/**
* @see ByteBuffer#putLong(int, long)
- */
+ *
public IoBuffer putLong(int index, long value) {
// TODO code me !
throw new UnsupportedOperationException();
@@ -862,7 +905,7 @@ public class IoBuffer {
/**
* @see ByteBuffer#putShort(short)
- */
+ *
public IoBuffer putShort(short value) {
// TODO code me !
throw new UnsupportedOperationException();
@@ -870,7 +913,7 @@ public class IoBuffer {
/**
* @see ByteBuffer#putShort(int, short)
- */
+ *
public IoBuffer putShort(int index, short value) {
// TODO code me !
throw new UnsupportedOperationException();
Modified: mina/trunk/core/src/test/java/org/apache/mina/util/IoBufferTest.java
URL:
http://svn.apache.org/viewvc/mina/trunk/core/src/test/java/org/apache/mina/util/IoBufferTest.java?rev=1208900&r1=1208899&r2=1208900&view=diff
==============================================================================
--- mina/trunk/core/src/test/java/org/apache/mina/util/IoBufferTest.java
(original)
+++ mina/trunk/core/src/test/java/org/apache/mina/util/IoBufferTest.java Thu
Dec 1 00:10:51 2011
@@ -435,4 +435,120 @@ public class IoBufferTest
ioBuffer.getInt();
}
+
+ /**
+ * test the get(int) method on one buffer
+ */
+ @Test
+ public void testGetIntOneBuffer() {
+ ByteBuffer bb = ByteBuffer.allocate(4);
+ bb.put("0123".getBytes());
+ bb.flip();
+
+ IoBuffer ioBuffer = new IoBuffer(bb);
+
+ assertEquals('0', ioBuffer.get());
+ assertEquals('1', ioBuffer.get());
+ assertEquals('0', ioBuffer.get(0));
+ assertEquals('3', ioBuffer.get(3));
+ assertEquals('1', ioBuffer.get(1));
+ assertEquals('2', ioBuffer.get(2));
+ assertEquals('2', ioBuffer.get());
+
+ try {
+ ioBuffer.get(4);
+ fail();
+ } catch (IndexOutOfBoundsException ioobe) {
+ // expected
+ assertTrue(true);
+ }
+ }
+
+ /**
+ * test the get(int) method on two buffers
+ */
+ @Test
+ public void testGetIntTwoBuffer() {
+ ByteBuffer bb1 = ByteBuffer.allocate(4);
+ bb1.put("0123".getBytes());
+ bb1.flip();
+
+ ByteBuffer bb2 = ByteBuffer.allocate(4);
+ bb2.put("4567".getBytes());
+ bb2.flip();
+
+ IoBuffer ioBuffer = new IoBuffer(bb1, bb2);
+
+ assertEquals('0', ioBuffer.get());
+ assertEquals('1', ioBuffer.get());
+ assertEquals('0', ioBuffer.get(0));
+ assertEquals('4', ioBuffer.get(4));
+ assertEquals('7', ioBuffer.get(7));
+ assertEquals('2', ioBuffer.get(2));
+ assertEquals('2', ioBuffer.get());
+ assertEquals('3', ioBuffer.get());
+ assertEquals('4', ioBuffer.get());
+
+ try {
+ ioBuffer.get(8);
+ fail();
+ } catch (IndexOutOfBoundsException ioobe) {
+ // expected
+ assertTrue(true);
+ }
+ }
+
+ /**
+ * Test the clear() method
+ */
+ @Test
+ public void testClear() {
+ ByteBuffer bb1 = ByteBuffer.allocate(4);
+ bb1.put("0123".getBytes());
+ bb1.flip();
+
+ ByteBuffer bb2 = ByteBuffer.allocate(4);
+ bb2.put("4567".getBytes());
+ bb2.flip();
+
+ IoBuffer ioBuffer = new IoBuffer(bb1, bb2);
+
+ // Move forward a bit
+ ioBuffer.get();
+ ioBuffer.get();
+
+ // Clear
+ ioBuffer.clear();
+
+ // We should be back to the origin
+ assertEquals(0, ioBuffer.position());
+ assertEquals(8, ioBuffer.limit());
+ }
+
+ /**
+ * Test the flip() method
+ */
+ @Test
+ public void testFlip() {
+ ByteBuffer bb1 = ByteBuffer.allocate(4);
+ bb1.put("0123".getBytes());
+ bb1.flip();
+
+ ByteBuffer bb2 = ByteBuffer.allocate(4);
+ bb2.put("4567".getBytes());
+ bb2.flip();
+
+ IoBuffer ioBuffer = new IoBuffer(bb1, bb2);
+
+ // Move forward a bit
+ ioBuffer.get();
+ ioBuffer.get();
+
+ // Clear
+ ioBuffer.clear();
+
+ // We should be back to the origin
+ assertEquals(0, ioBuffer.position());
+ assertEquals(8, ioBuffer.limit());
+ }
}
Modified:
mina/trunk/examples/src/main/java/org/apache/mina/examples/http/HttpTest.java
URL:
http://svn.apache.org/viewvc/mina/trunk/examples/src/main/java/org/apache/mina/examples/http/HttpTest.java?rev=1208900&r1=1208899&r2=1208900&view=diff
==============================================================================
---
mina/trunk/examples/src/main/java/org/apache/mina/examples/http/HttpTest.java
(original)
+++
mina/trunk/examples/src/main/java/org/apache/mina/examples/http/HttpTest.java
Thu Dec 1 00:10:51 2011
@@ -6,16 +6,16 @@
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
- * under the License.
- *
+ * under the License.
+ *
*/
package org.apache.mina.examples.http;
@@ -62,7 +62,7 @@ public class HttpTest {
acceptor.bind(new InetSocketAddress(8080));
// run for 20 seconds
- Thread.sleep(20000);
+ Thread.sleep(200000);
acceptor.unbindAll();
}
Modified:
mina/trunk/http/src/main/java/org/apache/mina/http/HttpServerDecoder.java
URL:
http://svn.apache.org/viewvc/mina/trunk/http/src/main/java/org/apache/mina/http/HttpServerDecoder.java?rev=1208900&r1=1208899&r2=1208900&view=diff
==============================================================================
--- mina/trunk/http/src/main/java/org/apache/mina/http/HttpServerDecoder.java
(original)
+++ mina/trunk/http/src/main/java/org/apache/mina/http/HttpServerDecoder.java
Thu Dec 1 00:10:51 2011
@@ -6,20 +6,19 @@
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
- * under the License.
- *
+ * under the License.
+ *
*/
package org.apache.mina.http;
-import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
@@ -31,6 +30,7 @@ import org.apache.mina.filterchain.ReadF
import org.apache.mina.http.api.HttpEndOfContent;
import org.apache.mina.http.api.HttpMethod;
import org.apache.mina.http.api.HttpVersion;
+import org.apache.mina.util.IoBuffer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -71,18 +71,12 @@ public class HttpServerDecoder implement
public static final Pattern COOKIE_SEPARATOR_PATTERN =
Pattern.compile(";");
@Override
- public Object decode(IoSession session, ByteBuffer msg,
ReadFilterChainController controller) {
+ public Object decode(IoSession session, IoBuffer msg,
ReadFilterChainController controller) {
DecoderState state = session.getAttribute(DECODER_STATE_ATT);
switch (state) {
case HEAD:
LOG.debug("decoding HEAD");
- // grab the stored a partial HEAD request
- ByteBuffer oldBuffer = session.getAttribute(PARTIAL_HEAD_ATT);
- // concat the old buffer and the new incoming one
- msg = ByteBuffer.allocate(oldBuffer.remaining() +
msg.remaining()).put(oldBuffer).put(msg);
- msg.flip();
- // now let's decode like it was a new message
case NEW:
LOG.debug("decoding NEW");
@@ -90,9 +84,8 @@ public class HttpServerDecoder implement
if (rq == null) {
// we copy the incoming BB because it's going to be recycled
by the inner IoProcessor for next reads
- ByteBuffer partial = ByteBuffer.allocate(msg.remaining());
- partial.put(msg);
- partial.flip();
+ //ByteBuffer partial = ByteBuffer.allocate(msg.remaining());
+ IoBuffer partial = new IoBuffer(msg);
// no request decoded, we accumulate
session.setAttribute(PARTIAL_HEAD_ATT, partial);
session.setAttribute(DECODER_STATE_ATT, DecoderState.HEAD);
@@ -157,7 +150,7 @@ public class HttpServerDecoder implement
public void dispose(IoSession session) throws Exception {
}
- private HttpRequestImpl parseHttpRequestHead(ByteBuffer buffer) {
+ private HttpRequestImpl parseHttpRequestHead(IoBuffer buffer) {
String raw = new String(buffer.array(), 0, buffer.limit(),
Charset.forName("ISO-8859-1"));
String[] headersAndBody = RAW_VALUE_PATTERN.split(raw, -1);
Modified:
mina/trunk/ldap/src/main/java/org/apache/mina/ldap/LdapProtocolDecoder.java
URL:
http://svn.apache.org/viewvc/mina/trunk/ldap/src/main/java/org/apache/mina/ldap/LdapProtocolDecoder.java?rev=1208900&r1=1208899&r2=1208900&view=diff
==============================================================================
--- mina/trunk/ldap/src/main/java/org/apache/mina/ldap/LdapProtocolDecoder.java
(original)
+++ mina/trunk/ldap/src/main/java/org/apache/mina/ldap/LdapProtocolDecoder.java
Thu Dec 1 00:10:51 2011
@@ -6,16 +6,16 @@
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
- * under the License.
- *
+ * under the License.
+ *
*/
package org.apache.mina.ldap;
@@ -34,6 +34,7 @@ import org.apache.directory.shared.util.
import org.apache.mina.api.IoSession;
import org.apache.mina.filter.codec.ProtocolDecoder;
import org.apache.mina.filterchain.ReadFilterChainController;
+import org.apache.mina.util.IoBuffer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -67,10 +68,10 @@ public class LdapProtocolDecoder impleme
/**
* {@inheritDoc}
*/
- public Object decode(IoSession session, ByteBuffer in,
ReadFilterChainController controller) {
+ public Object decode(IoSession session, IoBuffer in,
ReadFilterChainController controller) {
@SuppressWarnings("unchecked")
LdapMessageContainer<MessageDecorator<? extends Message>>
messageContainer =
- (LdapMessageContainer<MessageDecorator<? extends Message>>)
+ (LdapMessageContainer<MessageDecorator<? extends Message>>)
session.getAttribute("messageContainer");
if (session.containsAttribute("maxPDUSize")) {
@@ -83,7 +84,10 @@ public class LdapProtocolDecoder impleme
Object message = null;
do {
- message = decode( in, messageContainer );
+ byte[] bytes = in.array();
+ ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
+ buffer.wrap(bytes);
+ message = decode( buffer, messageContainer );
controller.callReadNextFilter(session, message );
} while( message != null);
@@ -97,7 +101,7 @@ public class LdapProtocolDecoder impleme
/**
- * Decode an incoming buffer into LDAP messages. The result can be 0, 1 or
many
+ * Decode an incoming buffer into LDAP messages. The result can be 0, 1 or
many
* LDAP messages, which will be stored into the array the caller has
created.
*
* @param buffer The incoming byte buffer