1 /* 2 * $HeadURL: /cvsroot/insight/Insight/remote-protocol/src/com/mindtree/insight/remoteprotocol/PacketDataSizeOverLimitsException.java,v $ 3 * $Date: 2005/08/09 18:39:17 $ 4 * $Revision: 1.1 $ 5 * $Author: m1001025 $ 6 * 7 * Copyright (c) 2005 MindTree Consulting Ltd. 8 * 9 * This file is part of Insight Remote Protocol Library. 10 * 11 * Insight Remote Protocol Library is free software: you can redistribute it 12 * and/or modify it under the terms of the GNU General Public License as 13 * published by the Free Software Foundation, either version 3 of the License, 14 * or (at your option) any later version. 15 * 16 * Insight Remote Protocol Library is distributed in the hope that it will be 17 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 19 * Public License for more details. 20 * 21 * You should have received a copy of the GNU General Public License along with 22 * Insight Remote Protocol Library. If not, see <http://www.gnu.org/licenses/>. 23 */ 24 25 package com.mindtree.techworks.insight.remoteprotocol.spi; 26 27 import java.io.Serializable; 28 29 import com.mindtree.techworks.insight.remoteprotocol.PacketDataSizeOverLimitsException; 30 import com.mindtree.techworks.insight.remoteprotocol.RemoteProtocolException; 31 32 33 /** 34 * Abstract class representing the data content in the 35 * {@link com.mindtree.techworks.insight.remoteprotocol.spi.DataPacket DataPacket}. This 36 * class contains the <code>byte</code> representation of the data. Specific 37 * subclasses may provide methods to access the data in a friendlier way. Unless 38 * an object of this class is an instance of <code>NullMessageDataContent</code>, 39 * setting <code>null</code> data is not allowed. 40 * 41 * @see com.mindtree.techworks.insight.remoteprotocol.spi.DataPacket 42 * @see com.mindtree.techworks.insight.remoteprotocol.spi.PacketHeader 43 * @author <a href="mailto:bindul_bhowmik@mindtree.com">Bindul Bhowmik</a> 44 * @version $Revision: 1.2 $ $Date: 2005/08/09 18:39:16 $ 45 */ 46 public abstract class PacketDataContent implements Serializable { 47 48 /** 49 * SVUID for Object Serialization 50 */ 51 private static final long serialVersionUID = 5823310926240802099L; 52 53 /** 54 * The maximum number of bytes a data packet can have. It is enforced by the 55 */ 56 public static final int MAX_PACKET_DATA_SIZE = 5000; 57 58 // ------------------------------------------------------------------------- 59 // Instance variables 60 // ------------------------------------------------------------------------- 61 62 /** 63 * Packet Data without the header 64 */ 65 protected byte [] packetData; 66 67 /** 68 * Identifies the type of this message packet. 69 */ 70 protected MessageType messageType; 71 72 // ------------------------------------------------------------------------- 73 // Constructors 74 // ------------------------------------------------------------------------- 75 76 /** 77 * Creates an instance of PacketDataContent object 78 */ 79 public PacketDataContent () { 80 81 // No Args constructor. 82 } 83 84 /** 85 * Creates an instance of PacketDataContent object. 86 * <p> 87 * The <code>byte []</code> passed in should be complete data for the 88 * packet including the header. The constructor will start reading data for 89 * this packet leaving the bytes required for the Header. 90 * </p> 91 * <p> 92 * <b>Note:</b> Using this constructor requires that certain fields 93 * regarding this packet be already set in the header. In particular the 94 * following fields in the header are used: 95 * <ul> 96 * <li>{@link PacketHeader#getDataLength() dataLength}</li> 97 * <li>{@link PacketHeader#getMessageType() messageType}</li> 98 * </ul> 99 * </p> 100 * 101 * @param completePacketData 102 * @param packetHeader 103 */ 104 public PacketDataContent (byte [] completePacketData, 105 PacketHeader packetHeader) { 106 107 this.messageType = packetHeader.getMessageType (); 108 if (packetHeader.getDataLength () != 0) { 109 this.packetData = new byte [packetHeader.getDataLength ()]; 110 System.arraycopy (completePacketData, packetHeader 111 .getPacketHeaderLength (), this.packetData, 0, packetHeader 112 .getDataLength ()); 113 } 114 } 115 116 // ------------------------------------------------------------------------- 117 // Accessors 118 // ------------------------------------------------------------------------- 119 120 /** 121 * Returns the length of the data in the packet. 122 * 123 * @return the number of bytes in the data. 124 */ 125 public int getLength () { 126 127 return null == this.packetData ? 0 : this.packetData.length; 128 } 129 130 131 /** 132 * Returns the messageType 133 * 134 * @return Returns the messageType. 135 */ 136 public MessageType getMessageType () { 137 138 return messageType; 139 } 140 141 142 /** 143 * Sets the messageType field 144 * 145 * @param messageType 146 * The messageType to set. 147 */ 148 public void setMessageType (MessageType messageType) { 149 150 this.messageType = messageType; 151 } 152 153 154 /** 155 * Returns the packetData. Although it would be more appropriate to use the 156 * specific methods in the subtype of this class to get the data in the 157 * correct format. 158 * 159 * @return Returns the packetData. 160 */ 161 public byte [] getPacketData () { 162 163 return packetData; 164 } 165 166 167 /** 168 * Sets the packetData field 169 * 170 * @param packetData 171 * The packetData to set. 172 * @throws PacketDataSizeOverLimitsException 173 * This may be thrown if the packet data is over limit 174 * @throws RemoteProtocolException 175 * This may be thrown by a subclass overriding this method to 176 * indicate some invalid data. 177 */ 178 public void setPacketData (byte [] packetData) 179 throws RemoteProtocolException { 180 181 if (null == packetData) { 182 throw new RemoteProtocolException ("Null data!"); 183 } 184 checkPacketDataSize (packetData); 185 this.packetData = packetData; 186 } 187 188 // ------------------------------------------------------------------------- 189 // Utility methods 190 // ------------------------------------------------------------------------- 191 192 /** 193 * Checks the packet data size and ensures that this is less than 194 * {@link #MAX_PACKET_DATA_SIZE MAX_PACKET_DATA_SIZE}. This method is 195 * called before setting packet data. If subclasses override the 196 * <code>#setPacketData(byte[])</code> method, then they should call this 197 * method before setting the data. 198 * 199 * @param dataToSet 200 * Checks the data to set 201 * @throws PacketDataSizeOverLimitsException 202 * If the data size is over the allowed limit 203 */ 204 protected void checkPacketDataSize (byte [] dataToSet) 205 throws PacketDataSizeOverLimitsException { 206 207 if (dataToSet.length > MAX_PACKET_DATA_SIZE) { 208 throw new PacketDataSizeOverLimitsException ("Allowed length: " 209 + MAX_PACKET_DATA_SIZE + ". Size of data sent in: " 210 + dataToSet.length); 211 } 212 } 213 214 }