home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / DatagramPacket.java < prev    next >
Text File  |  1997-05-20  |  5KB  |  178 lines

  1. /*
  2.  * @(#)DatagramPacket.java    1.12 97/01/25
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22.  
  23. package java.net;
  24.  
  25. /**
  26.  * This class represents a datagram packet. 
  27.  * <p>
  28.  * Datagram packets are used to implement a connectionless packet 
  29.  * delivery service. Each message is routed from one machine to 
  30.  * another based solely on information contained within that packet. 
  31.  * Multiple packets sent from one machine to another might be routed 
  32.  * differently, and might arrive in any order. 
  33.  *
  34.  * @author  Pavani Diwanji
  35.  * @version 1.12, 01/25/97
  36.  * @since   JDK1.0
  37.  */
  38. public final 
  39. class DatagramPacket {
  40.     /*
  41.      * The fields of this class are package-private since DatagramSocketImpl 
  42.      * classes needs to access them.
  43.      */
  44.     byte[] buf;
  45.     int length;
  46.     InetAddress address;
  47.     int port;
  48.  
  49.     /**
  50.      * Constructs a <code>DatagramPacket</code> for receiving packets of 
  51.      * length <code>ilength</code>. 
  52.      * <p>
  53.      * The <code>length</code> argument must be less than or equal to 
  54.      * <code>ibuf.length</code>. 
  55.      *
  56.      * @param   ibuf      buffer for holding the incoming datagram.
  57.      * @param   ilength   the number of bytes to read.
  58.      * @since   JDK1.0
  59.      */
  60.     public DatagramPacket(byte ibuf[], int ilength) {
  61.     if (ilength > ibuf.length) {
  62.         throw new IllegalArgumentException("illegal length");
  63.     }
  64.     buf = ibuf;
  65.     length = ilength;
  66.     address = null;
  67.     port = -1;
  68.     }
  69.     
  70.     /**
  71.      * Constructs a datagram packet for sending packets of length 
  72.      * <code>ilength</code> to the specified port number on the specified 
  73.      * host. The <code>length</code> argument must be less than or equal 
  74.      * to <code>ibuf.length</code>. 
  75.      *
  76.      * @param   ibuf      the packet data.
  77.      * @param   ilength   the packet length.
  78.      * @param   iaddr     the destination address.
  79.      * @param   iport     the destination port number.
  80.      * @see     java.net.InetAddress
  81.      * @since   JDK1.0
  82.      */
  83.     public DatagramPacket(byte ibuf[], int ilength,
  84.               InetAddress iaddr, int iport) {
  85.     if (ilength > ibuf.length) {
  86.         throw new IllegalArgumentException("illegal length");
  87.     }
  88.     if (iport < 0 || iport > 0xFFFF) {
  89.         throw new IllegalArgumentException("Port out of range:"+ iport);
  90.     }
  91.     buf = ibuf;
  92.     length = ilength;
  93.     address = iaddr;
  94.     port = iport;
  95.     }
  96.     
  97.     /**
  98.      * Returns the IP address of the machine to which this datagram is being
  99.      * sent or from which the datagram was received.
  100.      *
  101.      * @return  the IP address of the machine to which this datagram is being
  102.      *          sent or from which the datagram was received.
  103.      * @see     java.net.InetAddress
  104.      * @since   JDK1.0
  105.      */
  106.     public synchronized InetAddress getAddress() {
  107.     return address;
  108.     }
  109.     
  110.     /**
  111.      * Returns the port number on the remote host to which this datagram is
  112.      * being sent or from which the datagram was received.
  113.      *
  114.      * @return  the port number on the remote host to which this datagram is
  115.      *          being sent or from which the datagram was received.
  116.      * @since   JDK1.0
  117.      */
  118.     public synchronized int getPort() {
  119.     return port;
  120.     }
  121.     
  122.     /**
  123.      * Returns the data received or the data to be sent.
  124.      *
  125.      * @return  the data received or the data to be sent.
  126.      * @since   JDK1.0
  127.      */
  128.     public synchronized byte[] getData() {
  129.     return buf;
  130.     }
  131.     
  132.     /**
  133.      * Returns the length of the data to be sent or the length of the
  134.      * data received.
  135.      *
  136.      * @return  the length of the data to be sent or the length of the
  137.      *          data received.
  138.      * @since   JDK1.0
  139.      */
  140.     public synchronized int getLength() {
  141.     return length;
  142.     }
  143.  
  144.     /**
  145.      * @since   JDK1.1
  146.      */
  147.     public synchronized void setAddress(InetAddress iaddr) {
  148.     address = iaddr;
  149.     }
  150.  
  151.     /**
  152.      * @since   JDK1.1
  153.      */
  154.     public synchronized void setPort(int iport) {
  155.     if (iport < 0 || iport > 0xFFFF) {
  156.         throw new IllegalArgumentException("Port out of range:"+ iport);
  157.     }
  158.     port = iport;
  159.     }
  160.  
  161.     /**
  162.      * @since   JDK1.1
  163.      */
  164.     public synchronized void setData(byte[] ibuf) {
  165.     buf = ibuf;
  166.     }
  167.  
  168.     /**
  169.      * @since   JDK1.1
  170.      */
  171.     public synchronized void setLength(int ilength) {
  172.     if (ilength > buf.length) {
  173.         throw new IllegalArgumentException("illegal length");
  174.     }
  175.     length = ilength;
  176.     }
  177. }
  178.