TFTPDataPacket.java

  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. package org.apache.commons.net.tftp;

  18. import java.net.DatagramPacket;
  19. import java.net.InetAddress;

  20. /**
  21.  * A final class derived from TFTPPacket defining the TFTP Data packet type.
  22.  * <p>
  23.  * 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
  24.  * 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
  25.  * should only be concerned with the {@link org.apache.commons.net.tftp.TFTPClient} class {@link org.apache.commons.net.tftp.TFTPClient#receiveFile
  26.  * receiveFile()} and {@link org.apache.commons.net.tftp.TFTPClient#sendFile sendFile()} methods.
  27.  *
  28.  *
  29.  * @see TFTPPacket
  30.  * @see TFTPPacketException
  31.  * @see TFTP
  32.  */

  33. public final class TFTPDataPacket extends TFTPPacket {
  34.     /** The maximum number of bytes in a TFTP data packet (512) */
  35.     public static final int MAX_DATA_LENGTH = 512;

  36.     /** The minimum number of bytes in a TFTP data packet (0) */
  37.     public static final int MIN_DATA_LENGTH = 0;

  38.     /** The block number of the packet. */
  39.     int blockNumber;

  40.     /** The length of the data. */
  41.     private int length;

  42.     /** The offset into the _data array at which the data begins. */
  43.     private int offset;

  44.     /** The data stored in the packet. */
  45.     private byte[] data;

  46.     /**
  47.      * Creates a data packet based from a received datagram. Assumes the datagram is at least length 4, else an ArrayIndexOutOfBoundsException may be thrown.
  48.      *
  49.      * @param datagram The datagram containing the received data.
  50.      * @throws TFTPPacketException If the datagram isn't a valid TFTP data packet.
  51.      */
  52.     TFTPDataPacket(final DatagramPacket datagram) throws TFTPPacketException {
  53.         super(TFTPPacket.DATA, datagram.getAddress(), datagram.getPort());

  54.         this.data = datagram.getData();
  55.         this.offset = 4;

  56.         if (getType() != this.data[1]) {
  57.             throw new TFTPPacketException("TFTP operator code does not match type.");
  58.         }

  59.         this.blockNumber = (this.data[2] & 0xff) << 8 | this.data[3] & 0xff;

  60.         this.length = datagram.getLength() - 4;

  61.         if (this.length > MAX_DATA_LENGTH) {
  62.             this.length = MAX_DATA_LENGTH;
  63.         }
  64.     }

  65.     public TFTPDataPacket(final InetAddress destination, final int port, final int blockNumber, final byte[] data) {
  66.         this(destination, port, blockNumber, data, 0, data.length);
  67.     }

  68.     /**
  69.      * 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
  70.      * 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
  71.      * MAX_DATA_LENGTH, it is truncated.
  72.      *
  73.      * @param destination The host to which the packet is going to be sent.
  74.      * @param port        The port to which the packet is going to be sent.
  75.      * @param blockNumber The block number of the data.
  76.      * @param data        The byte array containing the data.
  77.      * @param offset      The offset into the array where the data starts.
  78.      * @param length      The length of the data.
  79.      */
  80.     public TFTPDataPacket(final InetAddress destination, final int port, final int blockNumber, final byte[] data, final int offset, final int length) {
  81.         super(TFTPPacket.DATA, destination, port);
  82.         this.blockNumber = blockNumber;
  83.         this.data = data;
  84.         this.offset = offset;
  85.         this.length = Math.min(length, MAX_DATA_LENGTH);
  86.     }

  87.     /**
  88.      * Returns the block number of the data packet.
  89.      *
  90.      * @return The block number of the data packet.
  91.      */
  92.     public int getBlockNumber() {
  93.         return blockNumber;
  94.     }

  95.     /**
  96.      * Returns the byte array containing the packet data.
  97.      *
  98.      * @return The byte array containing the packet data.
  99.      */
  100.     public byte[] getData() {
  101.         return data;
  102.     }

  103.     /**
  104.      * Returns the length of the data part of the data packet.
  105.      *
  106.      * @return The length of the data part of the data packet.
  107.      */
  108.     public int getDataLength() {
  109.         return length;
  110.     }

  111.     /**
  112.      * Returns the offset into the byte array where the packet data actually starts.
  113.      *
  114.      * @return The offset into the byte array where the packet data actually starts.
  115.      */
  116.     public int getDataOffset() {
  117.         return offset;
  118.     }

  119.     /**
  120.      * 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
  121.      * implement his own TFTP client instead of using the {@link org.apache.commons.net.tftp.TFTPClient} class. Under normal circumstances, you should not have
  122.      * a need to call this method.
  123.      *
  124.      * @return A UDP datagram containing the TFTP data packet.
  125.      */
  126.     @Override
  127.     public DatagramPacket newDatagram() {
  128.         final byte[] data;

  129.         data = new byte[length + 4];
  130.         data[0] = 0;
  131.         data[1] = (byte) type;
  132.         data[2] = (byte) ((blockNumber & 0xffff) >> 8);
  133.         data[3] = (byte) (blockNumber & 0xff);

  134.         System.arraycopy(this.data, offset, data, 4, length);

  135.         return new DatagramPacket(data, length + 4, address, port);
  136.     }

  137.     /**
  138.      * This is a method only available within the package for implementing efficient datagram transport by eliminating buffering. It takes a datagram as an
  139.      * 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.
  140.      *
  141.      * @param datagram The datagram to create.
  142.      * @param data     The buffer to store the packet and to use in the datagram.
  143.      * @return The datagram argument.
  144.      */
  145.     @Override
  146.     DatagramPacket newDatagram(final DatagramPacket datagram, final byte[] data) {
  147.         data[0] = 0;
  148.         data[1] = (byte) type;
  149.         data[2] = (byte) ((blockNumber & 0xffff) >> 8);
  150.         data[3] = (byte) (blockNumber & 0xff);

  151.         // Doublecheck we're not the same
  152.         if (data != this.data) {
  153.             System.arraycopy(this.data, offset, data, 4, length);
  154.         }

  155.         datagram.setAddress(address);
  156.         datagram.setPort(port);
  157.         datagram.setData(data);
  158.         datagram.setLength(length + 4);

  159.         return datagram;
  160.     }

  161.     /**
  162.      * Sets the block number of the data packet.
  163.      *
  164.      * @param blockNumber the number to set
  165.      */
  166.     public void setBlockNumber(final int blockNumber) {
  167.         this.blockNumber = blockNumber;
  168.     }

  169.     /**
  170.      * Sets the data for the data packet.
  171.      *
  172.      * @param data   The byte array containing the data.
  173.      * @param offset The offset into the array where the data starts.
  174.      * @param length The length of the data.
  175.      */
  176.     public void setData(final byte[] data, final int offset, final int length) {
  177.         this.data = data;
  178.         this.offset = offset;
  179.         this.length = length;

  180.         this.length = Math.min(length, MAX_DATA_LENGTH);
  181.     }

  182.     /**
  183.      * For debugging
  184.      *
  185.      * @since 3.6
  186.      */
  187.     @Override
  188.     public String toString() {
  189.         return super.toString() + " DATA " + blockNumber + " " + length;
  190.     }
  191. }