Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
 
 /*
  * Created on Jul 22, 2005
  */
  
 package org.jboss.remoting.transport.multiplex;
 
 
 import  org.jboss.logging.Logger;

MultiplexingOutputStream is the class returned by VirtualSocket.getOutputStream(). It supports the methods and behavior implemented by the OutputStream returned by java.net.Socket.getOutputStream(). For more information about the behavior of the methods, see the javadoc for java.io.OutputStream.

Copyright (c) 2005

Deprecated:
As of release 2.4.0 the multiplex transport will no longer be actively supported.
Author(s):
Ron Sigal
 
 public class MultiplexingOutputStream extends OutputStream
 {
    protected static final Logger log = Logger.getLogger(MultiplexingOutputStream.class);
    private MultiplexingManager manager;
    private VirtualSocket virtualSocket;
    private SocketId socketId;
    private boolean outputShutdown = false;
    private boolean closed = false;
    private IOException writeException;
    
    private static final int OPEN 				= 0;
    private static final int CONNECTION_RESET 	= 1;
    private static final int CLOSED 				= 2;
    private int connectionState = ;
 
    private byte[] oneByte = new byte[1];
    private byte[] fourBytes = new byte[4];
   

Parameters:
manager
socketId
 
    public MultiplexingOutputStream(MultiplexingManager managerSocketId socketId)
    {
       this(managernullsocketId);
    }
   
   

Parameters:
manager
virtualSocket
socketId
 
    public MultiplexingOutputStream(MultiplexingManager managerVirtualSocket virtualSocketSocketId socketId)
    {
       this. = manager;
       this. = virtualSocket;
       this. = socketId;
       this. = manager.getOutputMultiplexor();
    }
    
 //////////////////////////////////////////////////////////////////////////////////////////////////
 ///                  The following methods are required of all OutputStreams                  '///
 //////////////////////////////////////////////////////////////////////////////////////////////////
 
 
ok: public void write(int b) throws IOException; ok: public void write(byte b[]) throws IOException; ok: public void write(byte b[], int off, int len) throws IOException; public void flush() throws IOException; ok: public void close() throws IOException; /


   
See superclass javadoc.
   public void close() throws IOException
   {
      .debug("MultiplexingOutputStream.close() entered");
      
      if ()
         return;
         
       = true;
      
      if ( != null)
         .close();
   }
   
   
See superclass javadoc.
   public void flush() throws IOException
   {
      // Could use flush() to raise priority of messages waiting in OutputMultiplexor's write queue.
   }
   
   
See superclass javadoc.
   public void write(int ithrows IOException
   {
      checkStatus();
      [0] = (bytei;
   }
   
See superclass javadoc.
   public void write(byte[] arraythrows IOExceptionNullPointerException
   { 
      checkStatus();
      .write(array);
   }
   
   
See superclass javadoc.
   public void write(byte[] arrayint offint len)
   { 
      checkStatus();
      if (array == null)
         throw new NullPointerException();
      
      if (off < 0 || len < 0 || off + len > array.length)
         throw new IndexOutOfBoundsException();
      
      byte[] subArray = new byte[len];
      
      for (int i = 0; i < leni++)
         subArray[i] = array[off + i];
      .write(subArray);
   }
   
  
//////////////////////////////////////////////////////////////////////////////////////////////////
///              The following methods are specific to MultiplexingOutputStream               '///
//////////////////////////////////////////////////////////////////////////////////////////////////
   protected void setWriteException(IOException e)
   {
       = e;
   }
   
   
writeInt() is borrowed from DataOutputStream. It saves the extra expense of creating a DataOutputStream.

Parameters:
i
Throws:
IOException
   public void writeInt(int ithrows IOException
   {
      [0] = (byte) ((i >>> 24) & 0xff);
      [1] = (byte) ((i >>> 16) & 0xff);
      [2] = (byte) ((i >>>  8) & 0xff);
      [3] = (byte) ((i >>>  0) & 0xff);
   }
   
Determines how to handle a write request depending on what this socket knows about the state of the peer socket.

Once this socket knows that the peer socket has closed, no more write requests will be honored. There seem to be two ways for a socket A to learn that its peer socket B has closed.

  1. If socket A has executed a write, but no subsequent write is performed on B, then the acknowledgement of the write will carry back the information that B has closed.
  2. If socket B has no pending acknowledgements to send at the time it closes, and then socket A does a write after B has closed, the (negative) acknowledgement of the write will carry back the information that B has closed.

Java seems to respond differently to the two cases. The first write after this socket has learned of the peer's closing through the first scenario will result in a SocketException("Connection reset"). In the second scenario, the first write by this socket after the peer has closed will quietly fail (no exception is thrown). All subsequent writes after either of these two scenarios will result in a SocketException("Broken pipe").

Currently, MultiplexingOutputStream implements only a simplified version of this behavior. In particular, it allows in all cases one write to silently fail, after which all writes result in a SocketException("Broken pipe");

Note. This discussion is based on empirical observation on a linux system, not on examination of code. Your mileage may vary.

   protected void checkStatus() throws IOException
   {
      if ()
         throw new SocketException("Socket closed");
      
      if ()
         throw new SocketException("Broken pipe");
      
      if ( != null)
         throw ;
      
      switch ()
      {
         case :
            return;
         case :
             = ;
            return;
            
         case :
            throw new SocketException("Broken pipe");
            
         default:
            .error("unrecognized connection state: " + );
      }
   }
   
   
   protected void handleRemoteDisconnect()
   {
      .debug("entering handleRemoteDisconnect()");
      
      switch ()
      {
         case :
             = ;
            return;
            
         default:
             = ;
            .error("invalid connection state in handleRemoteDisconnect(): " + );
      }
   }
   
   
   protected void shutdown()
   {
       = true;
   }
   
   

Parameters:
i
brackets
Throws:
IOException
   protected void write(int iint bracketsthrows IOException
   {
      .debug("brackets: " + brackets);
      [0] = (bytei;
      .write(brackets);
   }
   
   

Parameters:
i
brackets
Throws:
IOException
   protected void writeInt(int iint bracketsthrows IOException
   {
      .debug("brackets: " + brackets);
      [0] = (byte) ((i >>> 24) & 0xff);
      [1] = (byte) ((i >>> 16) & 0xff);
      [2] = (byte) ((i >>>  8) & 0xff);
      [3] = (byte) ((i >>>  0) & 0xff);
      .write(brackets);
   }
New to GrepCode? Check out our FAQ X