Home | History | Annotate | Download | only in net
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  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 package java.net;
     19 
     20 import org.apache.harmony.luni.util.Msg;
     21 
     22 /**
     23  * This class represents a datagram packet which contains data either to be sent
     24  * or received through a {@code DatagramSocket}. It holds additional information
     25  * such as its source or destination host.
     26  *
     27  * @see DatagramSocket
     28  */
     29 public final class DatagramPacket {
     30 
     31     byte[] data;
     32 
     33     /**
     34      * Length of the data to be sent or size of data that was received via
     35      * DatagramSocket#receive() method call.
     36      */
     37     int length;
     38 
     39     /**
     40      * Size of internal buffer that is used to store received data. Should be
     41      * greater or equal to "length" field.
     42      */
     43     int capacity;
     44 
     45     InetAddress address;
     46 
     47     int port = -1; // The default port number is -1
     48 
     49     int offset = 0;
     50 
     51 
     52     /**
     53      * Constructs a new {@code DatagramPacket} object to receive data up to
     54      * {@code length} bytes.
     55      *
     56      * @param data
     57      *            a byte array to store the read characters.
     58      * @param length
     59      *            the length of the data buffer.
     60      */
     61     public DatagramPacket(byte[] data, int length) {
     62         this(data, 0, length);
     63     }
     64 
     65     /**
     66      * Constructs a new {@code DatagramPacket} object to receive data up to
     67      * {@code length} bytes with a specified buffer offset.
     68      *
     69      * @param data
     70      *            a byte array to store the read characters.
     71      * @param offset
     72      *            the offset of the byte array where the bytes is written.
     73      * @param length
     74      *            the length of the data.
     75      */
     76     public DatagramPacket(byte[] data, int offset, int length) {
     77         super();
     78         setData(data, offset, length);
     79     }
     80 
     81     /**
     82      * Constructs a new {@code DatagramPacket} object to send data to the port
     83      * {@code aPort} of the address {@code host}. The {@code length} must be
     84      * lesser than or equal to the size of {@code data}. The first {@code
     85      * length} bytes from the byte array position {@code offset} are sent.
     86      *
     87      * @param data
     88      *            a byte array which stores the characters to be sent.
     89      * @param offset
     90      *            the offset of {@code data} where to read from.
     91      * @param length
     92      *            the length of data.
     93      * @param host
     94      *            the address of the target host.
     95      * @param aPort
     96      *            the port of the target host.
     97      */
     98     public DatagramPacket(byte[] data, int offset, int length,
     99             InetAddress host, int aPort) {
    100         this(data, offset, length);
    101         setPort(aPort);
    102         address = host;
    103     }
    104 
    105     /**
    106      * Constructs a new {@code DatagramPacket} object to send data to the port
    107      * {@code aPort} of the address {@code host}. The {@code length} must be
    108      * lesser than or equal to the size of {@code data}. The first {@code
    109      * length} bytes are sent.
    110      *
    111      * @param data
    112      *            a byte array which stores the characters to be sent.
    113      * @param length
    114      *            the length of data.
    115      * @param host
    116      *            the address of the target host.
    117      * @param port
    118      *            the port of the target host.
    119      */
    120     public DatagramPacket(byte[] data, int length, InetAddress host, int port) {
    121         this(data, 0, length, host, port);
    122     }
    123 
    124     /**
    125      * Gets the sender or destination IP address of this datagram packet.
    126      *
    127      * @return the address from where the datagram was received or to which it
    128      *         is sent.
    129      */
    130     public synchronized InetAddress getAddress() {
    131         return address;
    132     }
    133 
    134     /**
    135      * Gets the data of this datagram packet.
    136      *
    137      * @return the received data or the data to be sent.
    138      */
    139     public synchronized byte[] getData() {
    140         return data;
    141     }
    142 
    143     /**
    144      * Gets the length of the data stored in this datagram packet.
    145      *
    146      * @return the length of the received data or the data to be sent.
    147      */
    148     public synchronized int getLength() {
    149         return length;
    150     }
    151 
    152     /**
    153      * Gets the offset of the data stored in this datagram packet.
    154      *
    155      * @return the position of the received data or the data to be sent.
    156      */
    157     public synchronized int getOffset() {
    158         return offset;
    159     }
    160 
    161     /**
    162      * Gets the port number of the target or sender host of this datagram
    163      * packet.
    164      *
    165      * @return the port number of the origin or target host.
    166      */
    167     public synchronized int getPort() {
    168         return port;
    169     }
    170 
    171     /**
    172      * Sets the IP address of the target host.
    173      *
    174      * @param addr
    175      *            the target host address.
    176      */
    177     public synchronized void setAddress(InetAddress addr) {
    178         address = addr;
    179     }
    180 
    181     /**
    182      * Sets the data buffer for this datagram packet.
    183      *
    184      * @param buf
    185      *            the buffer to store the data.
    186      * @param anOffset
    187      *            the buffer offset where the data is stored.
    188      * @param aLength
    189      *            the length of the data to be sent or the length of buffer to
    190      *            store the received data.
    191      */
    192     public synchronized void setData(byte[] buf, int anOffset, int aLength) {
    193         if (0 > anOffset || anOffset > buf.length || 0 > aLength
    194                 || aLength > buf.length - anOffset) {
    195             throw new IllegalArgumentException(Msg.getString("K002f")); //$NON-NLS-1$
    196         }
    197         data = buf;
    198         offset = anOffset;
    199         length = aLength;
    200         capacity = aLength;
    201     }
    202 
    203     /**
    204      * Sets the data buffer for this datagram packet. The length of the datagram
    205      * packet is set to the buffer length.
    206      *
    207      * @param buf
    208      *            the buffer to store the data.
    209      */
    210     public synchronized void setData(byte[] buf) {
    211         length = buf.length; // This will check for null
    212         capacity = buf.length;
    213         data = buf;
    214         offset = 0;
    215     }
    216 
    217     /**
    218      * Gets the current capacity value.
    219      *
    220      * @return the current capacity value
    221      */
    222     synchronized int getCapacity() {
    223         return capacity;
    224     }
    225 
    226     /**
    227      * Sets the length of the datagram packet. This length plus the offset must
    228      * be lesser than or equal to the buffer size.
    229      *
    230      * @param len
    231      *            the length of this datagram packet.
    232      */
    233     public synchronized void setLength(int len) {
    234         if (0 > len || offset + len > data.length) {
    235             throw new IllegalArgumentException(Msg.getString("K002f")); //$NON-NLS-1$
    236         }
    237         length = len;
    238         capacity = len;
    239     }
    240 
    241     /**
    242      * An alternative to {@link #setLength(int)}, that doesn't reset the {@link #capacity}
    243      * field.
    244      *
    245      * @param len the length of this datagram packet
    246      */
    247     synchronized void setLengthOnly(int len) {
    248         if (0 > len || offset + len > data.length) {
    249             throw new IllegalArgumentException(Msg.getString("K002f")); //$NON-NLS-1$
    250         }
    251         length = len;
    252     }
    253 
    254     /**
    255      * Sets the port number of the target host of this datagram packet.
    256      *
    257      * @param aPort
    258      *            the target host port number.
    259      */
    260     public synchronized void setPort(int aPort) {
    261         if (aPort < 0 || aPort > 65535) {
    262             throw new IllegalArgumentException(Msg.getString("K0325", aPort)); //$NON-NLS-1$
    263         }
    264         port = aPort;
    265     }
    266 
    267     /**
    268      * Constructs a new {@code DatagramPacket} object to send data to the
    269      * address {@code sockAddr}. The {@code length} must be lesser than or equal
    270      * to the size of {@code data}. The first {@code length} bytes of the data
    271      * are sent.
    272      *
    273      * @param data
    274      *            the byte array to store the data.
    275      * @param length
    276      *            the length of the data.
    277      * @param sockAddr
    278      *            the target host address and port.
    279      * @throws SocketException
    280      *             if an error in the underlying protocol occurs.
    281      */
    282     public DatagramPacket(byte[] data, int length, SocketAddress sockAddr)
    283             throws SocketException {
    284         this(data, 0, length);
    285         setSocketAddress(sockAddr);
    286     }
    287 
    288     /**
    289      * Constructs a new {@code DatagramPacket} object to send data to the
    290      * address {@code sockAddr}. The {@code length} must be lesser than or equal
    291      * to the size of {@code data}. The first {@code length} bytes of the data
    292      * are sent.
    293      *
    294      * @param data
    295      *            the byte array to store the data.
    296      * @param offset
    297      *            the offset of the data.
    298      * @param length
    299      *            the length of the data.
    300      * @param sockAddr
    301      *            the target host address and port.
    302      * @throws SocketException
    303      *             if an error in the underlying protocol occurs.
    304      */
    305     public DatagramPacket(byte[] data, int offset, int length,
    306             SocketAddress sockAddr) throws SocketException {
    307         this(data, offset, length);
    308         setSocketAddress(sockAddr);
    309     }
    310 
    311     /**
    312      * Gets the host address and the port to which this datagram packet is sent
    313      * as a {@code SocketAddress} object.
    314      *
    315      * @return the SocketAddress of the target host.
    316      */
    317     public synchronized SocketAddress getSocketAddress() {
    318         return new InetSocketAddress(getAddress(), getPort());
    319     }
    320 
    321     /**
    322      * Sets the {@code SocketAddress} for this datagram packet.
    323      *
    324      * @param sockAddr
    325      *            the SocketAddress of the target host.
    326      */
    327     public synchronized void setSocketAddress(SocketAddress sockAddr) {
    328         if (!(sockAddr instanceof InetSocketAddress)) {
    329             throw new IllegalArgumentException(Msg.getString(
    330                     "K0316", sockAddr == null ? null : sockAddr.getClass())); //$NON-NLS-1$
    331         }
    332         InetSocketAddress inetAddr = (InetSocketAddress) sockAddr;
    333         port = inetAddr.getPort();
    334         address = inetAddr.getAddress();
    335     }
    336 }
    337