home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / enxle1f6 / src / games / battle / shared / comm / countpacket.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  1.6 KB  |  75 lines

  1. /*
  2.  * @(#)CountPacket.java
  3.  */
  4.  
  5. package games.Battle.shared.comm;
  6.  
  7. /**
  8.  * A special packet that indicates hoa many packets are about to follow. This
  9.  * is used by the server before a series of WhoInfo, GameInfo, or chat packets.
  10.  * <P>
  11.  * The count is internally limited to 255.
  12.  *
  13.  * @version 1.00
  14.  * @author Alex Nicolaou
  15.  * @author Jay Steele
  16.  */
  17.  
  18. public class CountPacket extends BattlePacket {
  19.     byte count;
  20.  
  21.     /**
  22.      * convert to a string representation
  23.      */
  24.     public String toString() {
  25.         return getClass().getName() + " [count=" + count + "]";
  26.     }
  27.  
  28.     /**
  29.      * construct a CountPacket with a value of 0. Used before reading in
  30.      * an actual CountPacket from a stream.
  31.      */
  32.     public CountPacket() {
  33.         count = (byte)0;
  34.     }
  35.  
  36.     /**
  37.      * construct a CountPacket with a particular value. Used before writing 
  38.      * the CountPacket to a stream.
  39.      * @param count the count of items that will follow this packet
  40.      */
  41.     public CountPacket(int count) {
  42.         this.count = (byte)count;
  43.     }
  44.  
  45.     /**
  46.      * Dump a text version of this packet, for debugging.
  47.      */
  48.     public void asciiDump() {
  49.         System.out.println("CountPacket <" + count + ">");
  50.     }
  51.  
  52.     /**
  53.      * Return the count 
  54.      */
  55.     public final int getCount() { return (int)count & 0x00ff; }
  56.  
  57.     /**
  58.      * Convert this packet to an array of bytes for transmission.
  59.      */
  60.     protected byte[] toBytes() {
  61.         byte[] buffer = new byte[1];
  62.         buffer[0] = count;
  63.         return buffer;
  64.     }
  65.  
  66.     /**
  67.      * With the given array of bytes, fill this object
  68.      * with data.
  69.      * @param data the data to fill the CountPacket with.
  70.      */
  71.     protected void fromBytes(byte[] data) {
  72.         count = data[0];
  73.     }
  74. }
  75.