directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r122702 - in incubator/directory/network/trunk/mina/src: examples/org/apache/mina/examples/echoserver java/org/apache/mina/io java/org/apache/mina/io/socket
Date Sat, 18 Dec 2004 04:22:50 GMT
Author: trustin
Date: Fri Dec 17 20:22:47 2004
New Revision: 122702

URL: http://svn.apache.org/viewcvs?view=rev&rev=122702
Log:
 * Added: WriteBuffer.putAsPossible() which puts as much as possible and returns the number
of bytes put (similar to write())
 * Improved EchoProtocolHandler to utilize putAsPossible().
Modified:
   incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/echoserver/EchoProtocolHandler.java
   incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/WriteBuffer.java
   incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpWriteBuffer.java

Modified: incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/echoserver/EchoProtocolHandler.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/echoserver/EchoProtocolHandler.java?view=diff&rev=122702&p1=incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/echoserver/EchoProtocolHandler.java&r1=122701&p2=incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/echoserver/EchoProtocolHandler.java&r2=122702
==============================================================================
--- incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/echoserver/EchoProtocolHandler.java
(original)
+++ incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/echoserver/EchoProtocolHandler.java
Fri Dec 17 20:22:47 2004
@@ -61,11 +61,9 @@
         ReadBuffer rb = IoSession.getReadBuffer();
         WriteBuffer wb = IoSession.getWriteBuffer();
 
-        if( rb.remaining() <= wb.remaining() )
-        {
-            wb.put( rb );
-            wb.flush();
+        if (wb.putAsPossible(rb) > 0) {
             rb.signal();
+            wb.flush();
         }
     }
 
@@ -77,11 +75,9 @@
         ReadBuffer rb = IoSession.getReadBuffer();
         WriteBuffer wb = IoSession.getWriteBuffer();
 
-        if( rb.hasRemaining() && rb.remaining() <= wb.remaining() )
-        {
-            wb.put( rb );
-            wb.flush();
+        if (wb.putAsPossible(rb) > 0) {
             rb.signal();
+            wb.flush();
         }
     }
 

Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/WriteBuffer.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/WriteBuffer.java?view=diff&rev=122702&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/WriteBuffer.java&r1=122701&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/WriteBuffer.java&r2=122702
==============================================================================
--- incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/WriteBuffer.java	(original)
+++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/WriteBuffer.java	Fri
Dec 17 20:22:47 2004
@@ -52,6 +52,10 @@
     WriteBuffer put( ByteBuffer buf );
 
     WriteBuffer put( ReadBuffer buf );
+    
+    int putAsPossible(ByteBuffer buf);
+    
+    int putAsPossible(ReadBuffer buf);
 
     WriteBuffer put( byte b );
 

Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpWriteBuffer.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpWriteBuffer.java?view=diff&rev=122702&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpWriteBuffer.java&r1=122701&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpWriteBuffer.java&r2=122702
==============================================================================
--- incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpWriteBuffer.java
(original)
+++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpWriteBuffer.java
Fri Dec 17 20:22:47 2004
@@ -71,12 +71,7 @@
 
     public WriteBuffer put( ReadBuffer buf )
     {
-        if( ! ( buf instanceof TcpReadBuffer ) )
-        {
-            throw new IllegalArgumentException( "Incompatible buffer type: "
-                                                + buf.getClass() );
-        }
-
+        checkBufferCompatibility( buf );
         this.buf.put( ( ( TcpReadBuffer ) buf ).buf() );
         return this;
     }
@@ -85,6 +80,32 @@
     {
         this.buf.put( buf );
         return this;
+    }
+    
+    public int putAsPossible( ReadBuffer buf ) {
+        checkBufferCompatibility( buf );
+        return this.putAsPossible( ( ( TcpReadBuffer ) buf ).buf() );
+    }
+    
+    public int putAsPossible( ByteBuffer buf ) {
+        int length = this.buf.remaining() < buf.remaining()? this.buf.remaining() : buf.remaining();
+        if (length > 0) {
+            int oldLimit = buf.limit();
+            buf.limit(buf.position() + length);
+            this.buf.put(buf);
+            buf.limit(oldLimit);
+        }
+        
+        return length;
+    }
+
+    private void checkBufferCompatibility( ReadBuffer buf )
+    {
+        if( ! ( buf instanceof TcpReadBuffer ) )
+        {
+            throw new IllegalArgumentException( "Incompatible buffer type: "
+                                                + buf.getClass() );
+        }
     }
 
     public WriteBuffer put( byte b )

Mime
View raw message