cancel.write(dos);
dos.writeLong(cancel.getDeliveryId());  dos.writeInt(cancel.getDeliveryCount());
   public void write(Object obj, OutputStream out) throws IOException   {                DataOutputStream dos;            if (out instanceof DataOutputStream)      {         dos = (DataOutputStream)out;      }      else      {         //Further sanity check         if (out instanceof ObjectOutputStream)         {            throw new IllegalArgumentException("Invalid stream - are you sure you have configured socket wrappers?");         }                  //This would be the case for the HTTP transport for example         //Wrap the stream                  //TODO Ideally remoting would let us wrap this before invoking the marshaller         //but this does not appear to be possible         dos = new DataOutputStream(out);      }                        handleVersion(obj, dos);      try      {                  if (obj instanceof InvocationRequest)         {            if (trace) { log.trace("writing InvocationRequest"); }               InvocationRequest req = (InvocationRequest)obj;               Object param;                        // Unwrap Callback.            if (req.getParameter() instanceof InternalInvocation)            {               InternalInvocation ii = (InternalInvocation) req.getParameter();               Object[] params = ii.getParameters();                              if (params != null && params.length > 0 && params[0] instanceof Callback)               {                  Callback callback = (Callback) params[0];                  MessagingMarshallable mm = (MessagingMarshallable)callback.getParameter();                  param = mm.getLoad();               }               else               {                  param = req.getParameter();               }            }            else if (req.getParameter() instanceof MessagingMarshallable)            {               param = ((MessagingMarshallable)req.getParameter()).getLoad();            }            else            {               param = req.getParameter();            }               if (trace) { log.trace("param is " + param); }               if (param instanceof MethodInvocation)            {               MethodInvocation mi = (MethodInvocation)param;                  String methodName = mi.getMethod().getName();                  if (trace) { log.trace("methodInvocation (" + methodName + "())"); }                  if (methodName.equals("send"))               {                  dos.writeByte(SEND);                     writeHeader(mi, dos);                     JBossMessage m = (JBossMessage)mi.getArguments()[0];                     dos.writeByte(m.getType());                     m.write(dos);                     dos.flush();                     if (trace) { log.trace("wrote send()"); }               }               else if ("more".equals(methodName))               {                  dos.writeByte(MORE);                     writeHeader(mi, dos);                     dos.flush();                     if (trace) { log.trace("wrote activate()"); }               }                          else if ("acknowledge".equals(methodName))               {                  dos.writeByte(ACKNOWLEDGE);                     writeHeader(mi, dos);                                    Ack ack = (Ack)mi.getArguments()[0];                                    dos.writeLong(ack.getDeliveryId());                     dos.flush();                     if (trace) { log.trace("wrote acknowledge()"); }               }               else if ("acknowledgeBatch".equals(methodName))               {                  dos.writeByte(ACKNOWLEDGE_BATCH);                     writeHeader(mi, dos);                                    List acks = (List)mi.getArguments()[0];                     dos.writeInt(acks.size());                     Iterator iter = acks.iterator();                     while (iter.hasNext())                  {                     Ack ack = (Ack)iter.next();                     dos.writeLong(ack.getDeliveryId());                  }                     dos.flush();                     if (trace) { log.trace("wrote acknowledge()"); }               }               else if ("sendTransaction".equals(methodName))               {                  dos.writeByte(SEND_TRANSACTION);                     writeHeader(mi, dos);                     TransactionRequest request = (TransactionRequest)mi.getArguments()[0];                     request.write(dos);                     dos.flush();                     if (trace) { log.trace("wrote getMessageNow()"); }               }               else if ("getIdBlock".equals(methodName))               {                  dos.writeByte(GET_ID_BLOCK);                     writeHeader(mi, dos);                     int size = ((Integer)mi.getArguments()[0]).intValue();                     dos.writeInt(size);                     dos.flush();                     if (trace) { log.trace("wrote getIdBlock()"); }               }                          else if ("cancelDeliveries".equals(methodName) && mi.getArguments() != null)               {                  dos.writeByte(CANCEL_DELIVERIES);                     writeHeader(mi, dos);                     List ids = (List)mi.getArguments()[0];                     dos.writeInt(ids.size());                     Iterator iter = ids.iterator();                     while (iter.hasNext())                  {                     Cancel cancel = (Cancel)iter.next();                     cancel.write(dos);                  }                     dos.flush();                     if (trace) { log.trace("wrote cancelDeliveries()"); }               }               else if ("recoverDeliveries".equals(methodName) && mi.getArguments() != null)               {                  dos.writeByte(RECOVER_DELIVERIES);                     writeHeader(mi, dos);                     List ids = (List)mi.getArguments()[0];                     dos.writeInt(ids.size());                     Iterator iter = ids.iterator();                     while (iter.hasNext())                  {                     DeliveryRecovery d = (DeliveryRecovery)iter.next();                     d.write(dos);                  }                     dos.flush();                     if (trace) { log.trace("wrote sendUnackedAckInfos()"); }               }               else if ("confirmDelivery".equals(methodName))               {                  dos.writeByte(CONFIRM_DELIVERY);                     writeHeader(mi, dos);                     Integer count = (Integer)mi.getArguments()[0];                     dos.writeInt(count.intValue());                                       dos.flush();                     if (trace) { log.trace("wrote confirmDelivery()"); }               }               else               {                  dos.write(SERIALIZED);                     // Delegate to serialization to handle the wire format                  serialize(dos, obj);                     if (trace) { log.trace("wrote using standard serialization"); }               }            }            else if (param instanceof ClientDelivery)            {               //Message delivery callback                  if (trace) { log.trace("DeliveryRunnable"); }                  ClientDelivery dr = (ClientDelivery)param;                  dos.writeByte(CALLBACK);                              dos.writeUTF(req.getSessionId());                  dr.write(dos);                  dos.flush();                  if (trace) { log.trace("wrote DeliveryRunnable"); }            }            else            {               //Internal invocation                  dos.write(SERIALIZED);                  //Delegate to serialization to handle the wire format               serialize(dos, obj);                  if (trace) { log.trace("wrote using standard serialization"); }            }         }         else if (obj instanceof InvocationResponse)         {            if (trace) { log.trace("writing InvocationResponse"); }               InvocationResponse resp = (InvocationResponse)obj;               Object res;               if (resp.getResult() instanceof MessagingMarshallable)            {               res = ((MessagingMarshallable)resp.getResult()).getLoad();            }            else            {               res = resp.getResult();            }               if (trace) { log.trace("result is " + res); }               if (res == null && !resp.isException())            {                      dos.write(NULL_RESPONSE);                  dos.flush();                  if (trace) { log.trace("wrote null response"); }            }                     else if (res instanceof IdBlock)            {               //Return value from getMessageNow               dos.write(ID_BLOCK_RESPONSE);                  IdBlock block = (IdBlock)res;                  block.write(dos);                  dos.flush();                  if (trace) { log.trace("wrote id block response"); }            }            else if (res instanceof HandleMessageResponse)            {                        //Return value from delivering messages to client               dos.write(HANDLE_MESSAGE_RESPONSE);                  HandleMessageResponse response = (HandleMessageResponse)res;                  response.write(dos);                  dos.flush();                  if (trace) { log.trace("wrote handle message response"); }            }            else if (res instanceof JBossMessage)            {               //Return value from browsing message               dos.write(BROWSE_MESSAGE_RESPONSE);                              JBossMessage msg = (JBossMessage)res;                              dos.writeByte(msg.getType());                              msg.write(dos);                              dos.flush();                              if (trace) { log.trace("wrote browse message response"); }            }                        else if (res instanceof Message[])            {               //Return value from browsing messages               dos.write(BROWSE_MESSAGES_RESPONSE);                              Message[] msgs = (Message[])res;                              dos.writeInt(msgs.length);                              for (int i = 0; i < msgs.length; i++)               {                  JBossMessage m = (JBossMessage)msgs[i];                                    dos.writeByte(m.getType());                                    m.write(dos);               }                              dos.flush();                              if (trace) { log.trace("wrote browse message response"); }            }            else if (res instanceof ArrayList &&                     ((ArrayList) res).size() > 0 &&                     ((ArrayList) res).get(0) instanceof Callback)            {               // Comes from polled Callbacks.               ArrayList callbackList = (ArrayList)res;               dos.write(CALLBACK_LIST);               dos.writeUTF(resp.getSessionId());               dos.writeInt(callbackList.size());                              Iterator it = callbackList.iterator();               while (it.hasNext())               {                  Callback callback = (Callback)it.next();                  // We don't use acknowledgeable push callbacks                  MessagingMarshallable mm = (MessagingMarshallable)callback.getParameter();                  ClientDelivery delivery = (ClientDelivery)mm.getLoad();                  delivery.write(dos);                  dos.flush();               }            }            else            {               dos.write(SERIALIZED);                  //Delegate to serialization to handle the wire format               serialize(dos, obj);                  if (trace) { log.trace("wrote using standard serialization"); }            }         }         else         {            throw new IllegalStateException("Invalid object " + obj);         }      }      catch (Exception e)      {         IOException e2 = new IOException(e.getMessage());         e2.setStackTrace(e.getStackTrace());         throw e2;      }   }