View Javadoc

    1   /***
    2    *
    3    * Copyright 2004 Protique Ltd
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18  package org.codehaus.activemq.message;
    19
    20  import java.io.ByteArrayOutputStream;
    21  import java.io.DataOutput;
    22  import java.io.DataOutputStream;
    23  import java.io.IOException;
    24  import java.io.ObjectOutputStream;
    25  import java.util.Iterator;
    26  import java.util.Set;
    27  import org.codehaus.activemq.util.BitArray;
    28
    29  /***
    30   * Allows instances implementing Packet interface to be serailized/deserailized
    31   */
    32  public abstract class AbstractPacketWriter implements PacketWriter {
    33      /***
    34       * simple helper method to ensure null strings are catered for
    35       *
    36       * @param str
    37       * @param dataOut
    38       * @throws IOException
    39       */
    40      protected void writeUTF(String str, DataOutput dataOut) throws IOException {
    41          if (str == null) {
    42              str = "";
    43          }
    44          dataOut.writeUTF(str);
    45      }
    46
    47      /***
    48       * @param packet
    49       * @return true if this PacketWriter can write this type of Packet
    50       */
    51      public boolean canWrite(Packet packet) {
    52          return packet.getPacketType() == this.getPacketType();
    53      }
    54
    55      /***
    56       * Simple (but inefficent) utility method to write an object on to a stream
    57       *
    58       * @param object
    59       * @param dataOut
    60       * @throws IOException
    61       */
    62      protected void writeObject(Object object, DataOutput dataOut) throws IOException {
    63          if (object != null) {
    64              ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
    65              ObjectOutputStream objOut = new ObjectOutputStream(bytesOut);
    66              objOut.writeObject(object);
    67              objOut.flush();
    68              byte[] data = bytesOut.toByteArray();
    69              dataOut.writeInt(data.length);
    70              dataOut.write(data);
    71          }
    72          else {
    73              dataOut.writeInt(0);
    74          }
    75      }
    76
    77      /***
    78       * Serializes a Packet int a byte array
    79       *
    80       * @param packet
    81       * @return the byte[]
    82       * @throws IOException
    83       */
    84      public byte[] writePacketToByteArray(Packet packet) throws IOException {
    85          ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
    86          DataOutputStream dataOut = new DataOutputStream(bytesOut);
    87          writePacket(packet, dataOut);
    88          dataOut.flush();
    89          return bytesOut.toByteArray();
    90      }
    91
    92      /***
    93       * Write a Packet instance to data output stream
    94       *
    95       * @param p  the instance to be seralized
    96       * @param dataOut the output stream
    97       * @throws IOException thrown if an error occurs
    98       */
    99      public void writePacket(Packet p, DataOutput dataOut) throws IOException {
    100         AbstractPacket packet = (AbstractPacket)p;
    101         writeUTF(packet.getId(), dataOut);
    102         BitArray ba = packet.getBitArray();
    103         ba.set(AbstractPacket.RECEIPT_REQUIRED_INDEX, packet.isReceiptRequired());
    104         Object[] visited = packet.getBrokersVisited();
    105         boolean writeVisited = visited != null && visited.length > 0;
    106         ba.set(AbstractPacket.BROKERS_VISITED_INDEX,writeVisited);
    107         ba.writeToStream(dataOut);
    108         if (writeVisited){
    109             dataOut.writeShort(visited.length);
    110             for(int i =0; i < visited.length; i++){
    111                 dataOut.writeUTF(visited[i].toString());
    112             }
    113         }
    114     }
    115 }