    View Javadoc
    1   package org.apache.commons.net.tftp;
    2
    3   /* ====================================================================
    4    * The Apache Software License, Version 1.1
    5    *
    6    * Copyright (c) 2001 The Apache Software Foundation.  All rights
    7    * reserved.
    8    *
    9    * Redistribution and use in source and binary forms, with or without
    10   * modification, are permitted provided that the following conditions
    11   * are met:
    12   *
    13   * 1. Redistributions of source code must retain the above copyright
    14   *    notice, this list of conditions and the following disclaimer.
    15   *
    16   * 2. Redistributions in binary form must reproduce the above copyright
    17   *    notice, this list of conditions and the following disclaimer in
    18   *    the documentation and/or other materials provided with the
    19   *    distribution.
    20   *
    21   * 3. The end-user documentation included with the redistribution,
    22   *    if any, must include the following acknowledgment:
    23   *       "This product includes software developed by the
    24   *        Apache Software Foundation (http://www.apache.org/)."
    25   *    Alternately, this acknowledgment may appear in the software itself,
    26   *    if and wherever such third-party acknowledgments normally appear.
    27   *
    28   * 4. The names "Apache" and "Apache Software Foundation" and
    29   *    "Apache Commons" must not be used to endorse or promote products
    30   *    derived from this software without prior written permission. For
    31   *    written permission, please contact apache@apache.org.
    32   *
    33   * 5. Products derived from this software may not be called "Apache",
    34   *    nor may "Apache" appear in their name, without
    35   *    prior written permission of the Apache Software Foundation.
    36   *
    37   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
    38   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    39   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    40   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
    41   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    42   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    43   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
    44   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    45   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    46   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
    47   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    48   * SUCH DAMAGE.
    49   * ====================================================================
    50   *
    51   * This software consists of voluntary contributions made by many
    52   * individuals on behalf of the Apache Software Foundation.  For more
    53   * information on the Apache Software Foundation, please see
    54   * <http://www.apache.org/>;.
    55   */
    56
    57  import java.net.DatagramPacket;
    58  import java.net.InetAddress;
    59
    60  /****
    61   * A final class derived from TFTPPacket definiing the TFTP Data
    62   * packet type.
    63   * <p>
    64   * Details regarding the TFTP protocol and the format of TFTP packets can
    65   * be found in RFC 783.  But the point of these classes is to keep you
    66   * from having to worry about the internals.  Additionally, only very
    67   * few people should have to care about any of the TFTPPacket classes
    68   * or derived classes.  Almost all users should only be concerned with the
    69   * <a href="org.apache.commons.net.tftp.TFTPClient.html#_top_">TFTPClient</a> class
    70   * <a href="org.apache.commons.net.tftp.TFTPClient.html#receiveFile">receiveFile()</a>
    71   * and
    72   * <a href="org.apache.commons.net.tftp.TFTPClient.html#sendFile">sendFile()</a>
    73   * methods.
    74   * <p>
    75   * <p>
    76   * @author Daniel F. Savarese
    77   * @see TFTPPacket
    78   * @see TFTPPacketException
    79   * @see TFTP
    80   ***/
    81
    82  public final class TFTPDataPacket extends TFTPPacket
    83  {
    84      /**** The maximum number of bytes in a TFTP data packet (512) ***/
    85      public static final int MAX_DATA_LENGTH = 512;
    86
    87      /**** The minimum number of bytes in a TFTP data packet (0) ***/
    88      public static final int MIN_DATA_LENGTH = 0;
    89
    90      /**** The block number of the packet. ***/
    91      int _blockNumber;
    92
    93      /**** The length of the data. ***/
    94      int _length;
    95
    96      /**** The offset into the _data array at which the data begins. ***/
    97      int _offset;
    98
    99      /**** The data stored in the packet. ***/
    100     byte[] _data;
    101
    102     /****
    103      * Creates a data packet to be sent to a host at a given port
    104      * with a given block number.  The actual data to be sent is passed as
    105      * an array, an offset, and a length.  The offset is the offset into
    106      * the byte array where the data starts.  The length is the length of
    107      * the data.  If the length is greater than MAX_DATA_LENGTH, it is
    108      * truncated.
    109      * <p>
    110      * @param destination  The host to which the packet is going to be sent.
    111      * @param port  The port to which the packet is going to be sent.
    112      * @param blockNumber The block number of the data.
    113      * @param data The byte array containing the data.
    114      * @param offset The offset into the array where the data starts.
    115      * @param length The length of the data.
    116      ***/
    117     public TFTPDataPacket(InetAddress destination, int port, int blockNumber,
    118                           byte[] data, int offset, int length)
    119     {
    120         super(TFTPPacket.DATA, destination, port);
    121
    122         _blockNumber = blockNumber;
    123         _data = data;
    124         _offset = offset;
    125
    126         if (length > MAX_DATA_LENGTH)
    127             _length = MAX_DATA_LENGTH;
    128         else
    129             _length = length;
    130     }
    131
    132     public TFTPDataPacket(InetAddress destination, int port, int blockNumber,
    133                           byte[] data)
    134     {
    135         this(destination, port, blockNumber, data, 0, data.length);
    136     }
    137
    138
    139     /****
    140      * Creates a data packet based from a received
    141      * datagram.  Assumes the datagram is at least length 4, else an
    142      * ArrayIndexOutOfBoundsException may be thrown.
    143      * <p>
    144      * @param datagram  The datagram containing the received data.
    145      * @throws TFTPPacketException  If the datagram isn't a valid TFTP
    146      *         data packet.
    147      ***/
    148     TFTPDataPacket(DatagramPacket datagram) throws TFTPPacketException
    149     {
    150         super(TFTPPacket.DATA, datagram.getAddress(), datagram.getPort());
    151
    152         _data = datagram.getData();
    153         _offset = 4;
    154
    155         if (getType() != _data[1])
    156             throw new TFTPPacketException("TFTP operator code does not match type.");
    157
    158         _blockNumber = (((_data[2] & 0xff) << 8) | (_data[3] & 0xff));
    159
    160         _length = datagram.getLength() - 4;
    161
    162         if (_length > MAX_DATA_LENGTH)
    163             _length = MAX_DATA_LENGTH;
    164     }
    165
    166     /****
    167      * This is a method only available within the package for
    168      * implementing efficient datagram transport by elminating buffering.
    169      * It takes a datagram as an argument, and a byte buffer in which
    170      * to store the raw datagram data.  Inside the method, the data
    171      * is set as the datagram's data and the datagram returned.
    172      * <p>
    173      * @param datagram  The datagram to create.
    174      * @param data The buffer to store the packet and to use in the datagram.
    175      * @return The datagram argument.
    176      ***/
    177     DatagramPacket _newDatagram(DatagramPacket datagram, byte[] data)
    178     {
    179         data[0] = 0;
    180         data[1] = (byte)_type;
    181         data[2] = (byte)((_blockNumber & 0xffff) >> 8);
    182         data[3] = (byte)(_blockNumber & 0xff);
    183
    184         // Doublecheck we're not the same
    185         if (data != _data)
    186             System.arraycopy(_data, _offset, data, 4, _length);
    187
    188         datagram.setAddress(_address);
    189         datagram.setPort(_port);
    190         datagram.setData(data);
    191         datagram.setLength(_length + 4);
    192
    193         return datagram;
    194     }
    195
    196     /****
    197      * Creates a UDP datagram containing all the TFTP
    198      * data packet data in the proper format.
    199      * This is a method exposed to the programmer in case he
    200      * wants to implement his own TFTP client instead of using
    201      * the <a href="org.apache.commons.net.tftp.TFTPClient.html#_top_">TFTPClient</a>
    202      * class.
    203      * Under normal circumstances, you should not have a need to call this
    204      * method.
    205      * <p>
    206      * @return A UDP datagram containing the TFTP data packet.
    207      ***/
    208     public DatagramPacket newDatagram()
    209     {
    210         byte[] data;
    211         DatagramPacket datagram;
    212
    213         data = new byte[_length + 4];
    214         data[0] = 0;
    215         data[1] = (byte)_type;
    216         data[2] = (byte)((_blockNumber & 0xffff) >> 8);
    217         data[3] = (byte)(_blockNumber & 0xff);
    218
    219         System.arraycopy(_data, _offset, data, 4, _length);
    220
    221         return new DatagramPacket(data, _length + 4, _address, _port);
    222     }
    223
    224     /****
    225      * Returns the block number of the data packet.
    226      * <p>
    227      * @return The block number of the data packet.
    228      ***/
    229     public int getBlockNumber()
    230     {
    231         return _blockNumber;
    232     }
    233
    234     /**** Sets the block number of the data packet.  ***/
    235     public void setBlockNumber(int blockNumber)
    236     {
    237         _blockNumber = blockNumber;
    238     }
    239
    240     /****
    241      * Sets the data for the data packet.
    242      * <p>
    243      * @param data The byte array containing the data.
    244      * @param offset The offset into the array where the data starts.
    245      * @param length The length of the data.
    246      ***/
    247     public void setData(byte[] data, int offset, int length)
    248     {
    249         _data = data;
    250         _offset = offset;
    251         _length = length;
    252
    253         if (length > MAX_DATA_LENGTH)
    254             _length = MAX_DATA_LENGTH;
    255         else
    256             _length = length;
    257     }
    258
    259     /****
    260      * Returns the length of the data part of the data packet.
    261      * <p>
    262      * @return The length of the data part of the data packet.
    263      ***/
    264     public int getDataLength()
    265     {
    266         return _length;
    267     }
    268
    269     /****
    270      * Returns the offset into the byte array where the packet data actually
    271      * starts.
    272      * <p>
    273      * @return The offset into the byte array where the packet data actually
    274      *         starts.
    275      ***/
    276     public int getDataOffset()
    277     {
    278         return _offset;
    279     }
    280
    281     /****
    282      * Returns the byte array containing the packet data.
    283      * <p>
    284      * @return The byte array containing the packet data.
    285      ***/
    286     public byte[] getData()
    287     {
    288         return _data;
    289     }
    290 }

This page was automatically generated by Maven