TFTPAckPacket.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 Acknowledgement 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 TFTPAckPacket extends TFTPPacket {
  34.     /** The block number being acknowledged by the packet. */
  35.     int blockNumber;

  36.     /**
  37.      * Creates an acknowledgement packet based from a received datagram. Assumes the datagram is at least length 4, else an ArrayIndexOutOfBoundsException may
  38.      * be thrown.
  39.      *
  40.      * @param datagram The datagram containing the received acknowledgement.
  41.      * @throws TFTPPacketException If the datagram isn't a valid TFTP acknowledgement packet.
  42.      */
  43.     TFTPAckPacket(final DatagramPacket datagram) throws TFTPPacketException {
  44.         super(TFTPPacket.ACKNOWLEDGEMENT, datagram.getAddress(), datagram.getPort());
  45.         final byte[] data;

  46.         data = datagram.getData();

  47.         if (getType() != data[1]) {
  48.             throw new TFTPPacketException("TFTP operator code does not match type.");
  49.         }

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

  52.     /**
  53.      * Creates an acknowledgment packet to be sent to a host at a given port acknowledging receipt of a block.
  54.      *
  55.      * @param destination The host to which the packet is going to be sent.
  56.      * @param port        The port to which the packet is going to be sent.
  57.      * @param blockNumber The block number being acknowledged.
  58.      */
  59.     public TFTPAckPacket(final InetAddress destination, final int port, final int blockNumber) {
  60.         super(TFTPPacket.ACKNOWLEDGEMENT, destination, port);
  61.         this.blockNumber = blockNumber;
  62.     }

  63.     /**
  64.      * Returns the block number of the acknowledgement.
  65.      *
  66.      * @return The block number of the acknowledgement.
  67.      */
  68.     public int getBlockNumber() {
  69.         return blockNumber;
  70.     }

  71.     /**
  72.      * Creates a UDP datagram containing all the TFTP acknowledgement packet data in the proper format. This is a method exposed to the programmer in case he
  73.      * wants to implement his own TFTP client instead of using the {@link org.apache.commons.net.tftp.TFTPClient} class. Under normal circumstances, you should
  74.      * not have a need to call this method.
  75.      *
  76.      * @return A UDP datagram containing the TFTP acknowledgement packet.
  77.      */
  78.     @Override
  79.     public DatagramPacket newDatagram() {
  80.         final byte[] data;

  81.         data = new byte[4];
  82.         data[0] = 0;
  83.         data[1] = (byte) type;
  84.         data[2] = (byte) ((blockNumber & 0xffff) >> 8);
  85.         data[3] = (byte) (blockNumber & 0xff);

  86.         return new DatagramPacket(data, data.length, address, port);
  87.     }

  88.     /**
  89.      * This is a method only available within the package for implementing efficient datagram transport by eliminating buffering. It takes a datagram as an
  90.      * 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.
  91.      *
  92.      * @param datagram The datagram to create.
  93.      * @param data     The buffer to store the packet and to use in the datagram.
  94.      * @return The datagram argument.
  95.      */
  96.     @Override
  97.     DatagramPacket newDatagram(final DatagramPacket datagram, final byte[] data) {
  98.         data[0] = 0;
  99.         data[1] = (byte) type;
  100.         data[2] = (byte) ((blockNumber & 0xffff) >> 8);
  101.         data[3] = (byte) (blockNumber & 0xff);

  102.         datagram.setAddress(address);
  103.         datagram.setPort(port);
  104.         datagram.setData(data);
  105.         datagram.setLength(4);

  106.         return datagram;
  107.     }

  108.     /**
  109.      * Sets the block number of the acknowledgement.
  110.      *
  111.      * @param blockNumber the number to set
  112.      */
  113.     public void setBlockNumber(final int blockNumber) {
  114.         this.blockNumber = blockNumber;
  115.     }

  116.     /**
  117.      * For debugging
  118.      *
  119.      * @since 3.6
  120.      */
  121.     @Override
  122.     public String toString() {
  123.         return super.toString() + " ACK " + blockNumber;
  124.     }
  125. }