    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.net.tftp;
    019
    020import java.net.DatagramPacket;
    021import java.net.InetAddress;
    022
    023/**
    024 * A final class derived from TFTPPacket definiing the TFTP Data packet type.
    025 * <p>
    026 * Details regarding the TFTP protocol and the format of TFTP packets can be found in RFC 783. But the point of these classes is to keep you from having to
    027 * worry about the internals. Additionally, only very few people should have to care about any of the TFTPPacket classes or derived classes. Almost all users
    028 * should only be concerned with the {@link org.apache.commons.net.tftp.TFTPClient} class {@link org.apache.commons.net.tftp.TFTPClient#receiveFile
    029 * receiveFile()} and {@link org.apache.commons.net.tftp.TFTPClient#sendFile sendFile()} methods.
    030 *
    031 *
    032 * @see TFTPPacket
    033 * @see TFTPPacketException
    034 * @see TFTP
    035 */
    036
    037public final class TFTPDataPacket extends TFTPPacket {
    038    /** The maximum number of bytes in a TFTP data packet (512) */
    039    public static final int MAX_DATA_LENGTH = 512;
    040
    041    /** The minimum number of bytes in a TFTP data packet (0) */
    042    public static final int MIN_DATA_LENGTH = 0;
    043
    044    /** The block number of the packet. */
    045    int blockNumber;
    046
    047    /** The length of the data. */
    048    private int length;
    049
    050    /** The offset into the _data array at which the data begins. */
    051    private int offset;
    052
    053    /** The data stored in the packet. */
    054    private byte[] data;
    055
    056    /**
    057     * Creates a data packet based from a received datagram. Assumes the datagram is at least length 4, else an ArrayIndexOutOfBoundsException may be thrown.
    058     *
    059     * @param datagram The datagram containing the received data.
    060     * @throws TFTPPacketException If the datagram isn't a valid TFTP data packet.
    061     */
    062    TFTPDataPacket(final DatagramPacket datagram) throws TFTPPacketException {
    063        super(TFTPPacket.DATA, datagram.getAddress(), datagram.getPort());
    064
    065        this.data = datagram.getData();
    066        this.offset = 4;
    067
    068        if (getType() != this.data[1]) {
    069            throw new TFTPPacketException("TFTP operator code does not match type.");
    070        }
    071
    072        this.blockNumber = (((this.data[2] & 0xff) << 8) | (this.data[3] & 0xff));
    073
    074        this.length = datagram.getLength() - 4;
    075
    076        if (this.length > MAX_DATA_LENGTH) {
    077            this.length = MAX_DATA_LENGTH;
    078        }
    079    }
    080
    081    public TFTPDataPacket(final InetAddress destination, final int port, final int blockNumber, final byte[] data) {
    082        this(destination, port, blockNumber, data, 0, data.length);
    083    }
    084
    085    /**
    086     * Creates a data packet to be sent to a host at a given port with a given block number. The actual data to be sent is passed as an array, an offset, and a
    087     * length. The offset is the offset into the byte array where the data starts. The length is the length of the data. If the length is greater than
    088     * MAX_DATA_LENGTH, it is truncated.
    089     *
    090     * @param destination The host to which the packet is going to be sent.
    091     * @param port        The port to which the packet is going to be sent.
    092     * @param blockNumber The block number of the data.
    093     * @param data        The byte array containing the data.
    094     * @param offset      The offset into the array where the data starts.
    095     * @param length      The length of the data.
    096     */
    097    public TFTPDataPacket(final InetAddress destination, final int port, final int blockNumber, final byte[] data, final int offset, final int length) {
    098        super(TFTPPacket.DATA, destination, port);
    099
    100        this.blockNumber = blockNumber;
    101        this.data = data;
    102        this.offset = offset;
    103
    104        this.length = Math.min(length, MAX_DATA_LENGTH);
    105    }
    106
    107    /**
    108     * Returns the block number of the data packet.
    109     *
    110     * @return The block number of the data packet.
    111     */
    112    public int getBlockNumber() {
    113        return blockNumber;
    114    }
    115
    116    /**
    117     * Returns the byte array containing the packet data.
    118     *
    119     * @return The byte array containing the packet data.
    120     */
    121    public byte[] getData() {
    122        return data;
    123    }
    124
    125    /**
    126     * Returns the length of the data part of the data packet.
    127     *
    128     * @return The length of the data part of the data packet.
    129     */
    130    public int getDataLength() {
    131        return length;
    132    }
    133
    134    /**
    135     * Returns the offset into the byte array where the packet data actually starts.
    136     *
    137     * @return The offset into the byte array where the packet data actually starts.
    138     */
    139    public int getDataOffset() {
    140        return offset;
    141    }
    142
    143    /**
    144     * Creates a UDP datagram containing all the TFTP data packet data in the proper format. This is a method exposed to the programmer in case he wants to
    145     * implement his own TFTP client instead of using the {@link org.apache.commons.net.tftp.TFTPClient} class. Under normal circumstances, you should not have
    146     * a need to call this method.
    147     *
    148     * @return A UDP datagram containing the TFTP data packet.
    149     */
    150    @Override
    151    public DatagramPacket newDatagram() {
    152        final byte[] data;
    153
    154        data = new byte[length + 4];
    155        data[0] = 0;
    156        data[1] = (byte) type;
    157        data[2] = (byte) ((blockNumber & 0xffff) >> 8);
    158        data[3] = (byte) (blockNumber & 0xff);
    159
    160        System.arraycopy(this.data, offset, data, 4, length);
    161
    162        return new DatagramPacket(data, length + 4, address, port);
    163    }
    164
    165    /**
    166     * This is a method only available within the package for implementing efficient datagram transport by elminating buffering. It takes a datagram as an
    167     * argument, and a byte buffer in which to store the raw datagram data. Inside the method, the data is set as the datagram's data and the datagram returned.
    168     *
    169     * @param datagram The datagram to create.
    170     * @param data     The buffer to store the packet and to use in the datagram.
    171     * @return The datagram argument.
    172     */
    173    @Override
    174    DatagramPacket newDatagram(final DatagramPacket datagram, final byte[] data) {
    175        data[0] = 0;
    176        data[1] = (byte) type;
    177        data[2] = (byte) ((blockNumber & 0xffff) >> 8);
    178        data[3] = (byte) (blockNumber & 0xff);
    179
    180        // Doublecheck we're not the same
    181        if (data != this.data) {
    182            System.arraycopy(this.data, offset, data, 4, length);
    183        }
    184
    185        datagram.setAddress(address);
    186        datagram.setPort(port);
    187        datagram.setData(data);
    188        datagram.setLength(length + 4);
    189
    190        return datagram;
    191    }
    192
    193    /**
    194     * Sets the block number of the data packet.
    195     *
    196     * @param blockNumber the number to set
    197     */
    198    public void setBlockNumber(final int blockNumber) {
    199        this.blockNumber = blockNumber;
    200    }
    201
    202    /**
    203     * Sets the data for the data packet.
    204     *
    205     * @param data   The byte array containing the data.
    206     * @param offset The offset into the array where the data starts.
    207     * @param length The length of the data.
    208     */
    209    public void setData(final byte[] data, final int offset, final int length) {
    210        this.data = data;
    211        this.offset = offset;
    212        this.length = length;
    213
    214        this.length = Math.min(length, MAX_DATA_LENGTH);
    215    }
    216
    217    /**
    218     * For debugging
    219     *
    220     * @since 3.6
    221     */
    222    @Override
    223    public String toString() {
    224        return super.toString() + " DATA " + blockNumber + " " + length;
    225    }
    226}