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
    26  /***
    27   * Allows instances implementing Packet interface to be serailized/deserailized
    28   */
    29  public abstract class AbstractPacketWriter implements PacketWriter {
    30      /***
    31       * simple helper method to ensure null strings are catered for
    32       *
    33       * @param str
    34       * @param dataOut
    35       * @throws IOException
    36       */
    37      protected void writeUTF(String str, DataOutput dataOut) throws IOException {
    38          if (str == null) {
    39              str = "";
    40          }
    41          dataOut.writeUTF(str);
    42      }
    43
    44      /***
    45       * @param packet
    46       * @return true if this PacketWriter can write this type of Packet
    47       */
    48      public boolean canWrite(Packet packet) {
    49          return packet.getPacketType() == this.getPacketType();
    50      }
    51
    52      /***
    53       * Simple (but inefficent) utility method to write an object on to a stream
    54       *
    55       * @param object
    56       * @param dataOut
    57       * @throws IOException
    58       */
    59      protected void writeObject(Object object, DataOutput dataOut) throws IOException {
    60          if (object != null) {
    61              ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
    62              ObjectOutputStream objOut = new ObjectOutputStream(bytesOut);
    63              objOut.writeObject(object);
    64              objOut.flush();
    65              byte[] data = bytesOut.toByteArray();
    66              dataOut.writeInt(data.length);
    67              dataOut.write(data);
    68          }
    69          else {
    70              dataOut.writeInt(0);
    71          }
    72      }
    73
    74      /***
    75       * Serializes a Packet int a byte array
    76       *
    77       * @param packet
    78       * @return the byte[]
    79       * @throws IOException
    80       */
    81      public byte[] writePacketToByteArray(Packet packet) throws IOException {
    82          ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
    83          DataOutputStream dataOut = new DataOutputStream(bytesOut);
    84          writePacket(packet, dataOut);
    85          dataOut.flush();
    86          return bytesOut.toByteArray();
    87      }
    88
    89      /***
    90       * Write a Packet instance to data output stream
    91       *
    92       * @param packet  the instance to be seralized
    93       * @param dataOut the output stream
    94       * @throws IOException thrown if an error occurs
    95       */
    96      public void writePacket(Packet packet, DataOutput dataOut) throws IOException {
    97          writeUTF(packet.getId(), dataOut);
    98          dataOut.writeBoolean(packet.isReceiptRequired());
    99      }
    100 }