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