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  
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  
27  /***
28   * Allows instances implementing Packet interface to be deserailized
29   * 
30   * @version $Revision: 1.6 $
31   */
32  public abstract class AbstractPacketReader implements PacketReader {
33  
34      /***
35       * @param packetType
36       * @return true if this PacketReader can a Packet of this type
37       */
38      public boolean canRead(int packetType) {
39          return this.getPacketType() == packetType;
40      }
41  
42      /***
43       * pointless method - but mirrors writer
44       *
45       * @param dataIn
46       * @return the String
47       * @throws IOException
48       */
49      protected String readUTF(DataInput dataIn) throws IOException {
50          return dataIn.readUTF();
51      }
52  
53  
54      /***
55       * ;
56       *
57       * @param dataIn
58       * @return object
59       * @throws IOException
60       */
61      protected Object readObject(DataInput dataIn) throws IOException {
62          int dataLength = dataIn.readInt();
63          if (dataLength > 0) {
64              byte[] data = new byte[dataLength];
65              dataIn.readFully(data);
66              ByteArrayInputStream bytesIn = new ByteArrayInputStream(data);
67              ObjectInputStream objIn = new ObjectInputStream(bytesIn);
68              try {
69                  return objIn.readObject();
70              }
71              catch (ClassNotFoundException ex) {
72                  throw new IOException(ex.getMessage());
73              }
74          }
75          return null;
76      }
77  
78      /***
79       * build a Packet instance from the data input stream
80       *
81       * @param packet A Packet object
82       * @param dataIn the data input stream to build the packet from
83       * @throws IOException
84       */
85      public void buildPacket(Packet packet, DataInput dataIn) throws IOException {
86          packet.setId(readUTF(dataIn));
87          packet.setReceiptRequired(dataIn.readBoolean());
88      }
89  
90      /***
91       * Deserailizes a Packet from a byte array
92       *
93       * @param data
94       * @return the deserialized Packet
95       * @throws IOException
96       */
97      public Packet readPacketFromByteArray(byte[] data) throws IOException {
98          ByteArrayInputStream bytesIn = new ByteArrayInputStream(data);
99          DataInputStream dataIn = new DataInputStream(bytesIn);
100         Packet packet = createPacket();
101         buildPacket(packet, dataIn);
102         return packet;
103     }
104 }