Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/activemq/io/impl/AbstractPacketWriter.java


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.activemq.io.impl;
19  import java.io.ByteArrayOutputStream;
20  import java.io.DataOutput;
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  
24  import org.activemq.message.AbstractPacket;
25  import org.activemq.message.Packet;
26  import org.activemq.util.BitArray;
27  import org.activemq.util.SerializationHelper;
28  
29  /**
30   * Allows instances implementing Packet interface to be serailized/deserailized
31   */
32  public abstract class AbstractPacketWriter implements PacketWriter {
33      protected int wireFormatVersion = DefaultWireFormat.WIRE_FORMAT_VERSION;
34      
35      /**
36       * simple helper method to ensure null strings are catered for
37       *
38       * @param str
39       * @param dataOut
40       * @throws IOException
41       */
42      protected void writeUTF(String str, DataOutput dataOut) throws IOException {
43          if (str == null) {
44              str = "";
45          }
46          dataOut.writeUTF(str);
47      }
48  
49      /**
50       * @param packet
51       * @return true if this PacketWriter can write this type of Packet
52       */
53      public boolean canWrite(Packet packet) {
54          return packet.getPacketType() == this.getPacketType();
55      }
56  
57      /**
58       * Simple (but inefficent) utility method to write an object on to a stream
59       *
60       * @param object
61       * @param dataOut
62       * @throws IOException
63       */
64      protected void writeObject(Object object, DataOutput dataOut) throws IOException {
65          if (object != null) {            
66              byte[] data = SerializationHelper.writeObject(object);
67              dataOut.writeInt(data.length);
68              dataOut.write(data);
69          }
70          else {
71              dataOut.writeInt(0);
72          }
73      }
74  
75      /**
76       * Serializes a Packet int a byte array
77       *
78       * @param packet
79       * @return the byte[]
80       * @throws IOException
81       */
82      public byte[] writePacketToByteArray(Packet packet) throws IOException {
83          ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
84          DataOutputStream dataOut = new DataOutputStream(bytesOut);
85          writePacket(packet, dataOut);
86          dataOut.flush();
87          return bytesOut.toByteArray();
88      }
89  
90      /**
91       * Write a Packet instance to data output stream
92       *
93       * @param p  the instance to be seralized
94       * @param dataOut the output stream
95       * @throws IOException thrown if an error occurs
96       */
97      public void writePacket(Packet p, DataOutput dataOut) throws IOException {
98          AbstractPacket packet = (AbstractPacket)p;
99          dataOut.writeShort(packet.getId());
100         BitArray ba = packet.getBitArray();
101         ba.set(AbstractPacket.RECEIPT_REQUIRED_INDEX, packet.isReceiptRequired());
102         Object[] visited = packet.getBrokersVisited();
103         boolean writeVisited = visited != null && visited.length > 0;
104         ba.set(AbstractPacket.BROKERS_VISITED_INDEX,writeVisited);
105         ba.writeToStream(dataOut);
106         if (writeVisited){
107             dataOut.writeShort(visited.length);
108             for(int i =0; i < visited.length; i++){
109                 dataOut.writeUTF(visited[i].toString());
110             }
111         }
112     }
113     
114     /**
115      * Set the wire format version
116      * @param version
117      */
118     public void setWireFormatVersion(int version){
119         this.wireFormatVersion = version;
120     }
121     
122     /**
123      * @return the wire format version
124      */
125     public int getWireFormatVersion(){
126         return wireFormatVersion;
127     }
128 
129 }