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
19 package org.codehaus.activemq.message;
20
21 import java.io.ByteArrayInputStream;
22 import java.io.DataInput;
23 import java.io.DataInputStream;
24 import java.io.IOException;
25 import java.io.ObjectInputStream;
26 import java.util.Set;
27 import org.codehaus.activemq.util.BitArray;
28
29 /***
30 * Allows instances implementing Packet interface to be deserailized
31 *
32 * @version $Revision: 1.12 $
33 */
34 public abstract class AbstractPacketReader implements PacketReader {
35
36 /***
37 * @param packetType
38 * @return true if this PacketReader can a Packet of this type
39 */
40 public boolean canRead(int packetType) {
41 return this.getPacketType() == packetType;
42 }
43
44 /***
45 * pointless method - but mirrors writer
46 *
47 * @param dataIn
48 * @return the String
49 * @throws IOException
50 */
51 protected String readUTF(DataInput dataIn) throws IOException {
52 return dataIn.readUTF();
53 }
54
55
56 /***
57 * ;
58 *
59 * @param dataIn
60 * @return object
61 * @throws IOException
62 */
63 protected Object readObject(DataInput dataIn) throws IOException {
64 int dataLength = dataIn.readInt();
65 if (dataLength > 0) {
66 byte[] data = new byte[dataLength];
67 dataIn.readFully(data);
68 ByteArrayInputStream bytesIn = new ByteArrayInputStream(data);
69 ObjectInputStream objIn = new ObjectInputStream(bytesIn);
70 try {
71 return objIn.readObject();
72 }
73 catch (ClassNotFoundException ex) {
74 throw new IOException(ex.getMessage());
75 }
76 }
77 return null;
78 }
79
80 /***
81 * build a Packet instance from the data input stream
82 *
83 * @param p A Packet object
84 * @param dataIn the data input stream to build the packet from
85 * @throws IOException
86 */
87 public void buildPacket(Packet p, DataInput dataIn) throws IOException {
88 AbstractPacket packet = (AbstractPacket)p;
89 packet.setId(readUTF(dataIn));
90 BitArray ba = packet.getBitArray();
91 ba.readFromStream(dataIn);
92 packet.setReceiptRequired(ba.get(AbstractPacket.RECEIPT_REQUIRED_INDEX));
93 if (ba.get(AbstractPacket.BROKERS_VISITED_INDEX)){
94 int visitedLen = dataIn.readShort();
95 for (int i =0; i < visitedLen; i++){
96 packet.addBrokerVisited(dataIn.readUTF());
97 }
98 }
99 }
100
101 /***
102 * Deserailizes a Packet from a byte array
103 *
104 * @param data
105 * @return the deserialized Packet
106 * @throws IOException
107 */
108 public Packet readPacketFromByteArray(byte[] data) throws IOException {
109 ByteArrayInputStream bytesIn = new ByteArrayInputStream(data);
110 DataInputStream dataIn = new DataInputStream(bytesIn);
111 Packet packet = createPacket();
112 buildPacket(packet, dataIn);
113 return packet;
114 }
115 }