You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ni...@apache.org on 2006/08/01 20:14:17 UTC

svn commit: r427667 - /directory/trunks/mina/core/src/main/java/org/apache/mina/handler/support/IoSessionOutputStream.java

Author: niklas
Date: Tue Aug  1 11:14:17 2006
New Revision: 427667

URL: http://svn.apache.org/viewvc?rev=427667&view=rev
Log:
Resolved DIRMINA-229 as suggested by Oleg Kalnichevski. The IoSessionOutputStream.write() methods will now block until the bytes have been written. If the write fails or the session has been closed an IOException will be thrown. The close() method will also block now.

Modified:
    directory/trunks/mina/core/src/main/java/org/apache/mina/handler/support/IoSessionOutputStream.java

Modified: directory/trunks/mina/core/src/main/java/org/apache/mina/handler/support/IoSessionOutputStream.java
URL: http://svn.apache.org/viewvc/directory/trunks/mina/core/src/main/java/org/apache/mina/handler/support/IoSessionOutputStream.java?rev=427667&r1=427666&r2=427667&view=diff
==============================================================================
--- directory/trunks/mina/core/src/main/java/org/apache/mina/handler/support/IoSessionOutputStream.java (original)
+++ directory/trunks/mina/core/src/main/java/org/apache/mina/handler/support/IoSessionOutputStream.java Tue Aug  1 11:14:17 2006
@@ -18,10 +18,12 @@
  */
 package org.apache.mina.handler.support;
 
+import java.io.IOException;
 import java.io.OutputStream;
 
 import org.apache.mina.common.ByteBuffer;
 import org.apache.mina.common.IoSession;
+import org.apache.mina.common.WriteFuture;
 
 /**
  * An {@link OutputStream} that forwards all write operations to
@@ -42,41 +44,38 @@
 
     public void close()
     {
-        session.close();
+        session.close().join();
     }
 
-    public void flush()
+    private void checkClosed() throws IOException
     {
-    }
-
-    public void write( byte[] b, int off, int len )
-    {
-        if( session.isConnected() )
+        if( ! session.isConnected() )
         {
-            ByteBuffer buf = ByteBuffer.wrap( b, off, len );
-            buf.acquire(); // prevent from being pooled.
-            session.write( buf );
+            throw new IOException( "The session has been closed." );
         }
     }
-
-    public void write( byte[] b )
+    
+    private void write( ByteBuffer buf ) throws IOException
     {
-        if( session.isConnected() )
+        checkClosed();
+        WriteFuture future = session.write( buf );
+        future.join();
+        if( ! future.isWritten() )
         {
-            ByteBuffer buf = ByteBuffer.wrap( b );
-            buf.acquire(); // prevent from being pooled.
-            session.write( buf );
+            throw new IOException( "The bytes could not be written to the session" );
         }
     }
+    
+    public void write( byte[] b, int off, int len ) throws IOException
+    {
+        write( ByteBuffer.wrap( b, off, len ) );
+    }
 
-    public void write( int b )
+    public void write( int b ) throws IOException
     {
-        if( session.isConnected() )
-        {
-            ByteBuffer buf = ByteBuffer.allocate( 1 );
-            buf.put( ( byte ) b );
-            buf.flip();
-            session.write( buf );
-        }
+        ByteBuffer buf = ByteBuffer.allocate( 1 );
+        buf.put( ( byte ) b );
+        buf.flip();
+        write( buf );
     }
 }