View Javadoc
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 org.apache.commons.net.tftp;
19  
20  import java.net.DatagramPacket;
21  import java.net.InetAddress;
22  
23  /**
24   * A final class derived from TFTPPacket defining the TFTP Acknowledgement packet type.
25   * <p>
26   * 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
27   * 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
28   * should only be concerned with the {@link org.apache.commons.net.tftp.TFTPClient} class {@link org.apache.commons.net.tftp.TFTPClient#receiveFile
29   * receiveFile()} and {@link org.apache.commons.net.tftp.TFTPClient#sendFile sendFile()} methods.
30   *
31   *
32   * @see TFTPPacket
33   * @see TFTPPacketException
34   * @see TFTP
35   */
36  
37  public final class TFTPAckPacket extends TFTPPacket {
38      /** The block number being acknowledged by the packet. */
39      int blockNumber;
40  
41      /**
42       * Creates an acknowledgement packet based from a received datagram. Assumes the datagram is at least length 4, else an ArrayIndexOutOfBoundsException may
43       * be thrown.
44       *
45       * @param datagram The datagram containing the received acknowledgement.
46       * @throws TFTPPacketException If the datagram isn't a valid TFTP acknowledgement packet.
47       */
48      TFTPAckPacket(final DatagramPacket datagram) throws TFTPPacketException {
49          super(TFTPPacket.ACKNOWLEDGEMENT, datagram.getAddress(), datagram.getPort());
50          final byte[] data;
51  
52          data = datagram.getData();
53  
54          if (getType() != data[1]) {
55              throw new TFTPPacketException("TFTP operator code does not match type.");
56          }
57  
58          this.blockNumber = (data[2] & 0xff) << 8 | data[3] & 0xff;
59      }
60  
61      /**
62       * Creates an acknowledgment packet to be sent to a host at a given port acknowledging receipt of a block.
63       *
64       * @param destination The host to which the packet is going to be sent.
65       * @param port        The port to which the packet is going to be sent.
66       * @param blockNumber The block number being acknowledged.
67       */
68      public TFTPAckPacket(final InetAddress destination, final int port, final int blockNumber) {
69          super(TFTPPacket.ACKNOWLEDGEMENT, destination, port);
70          this.blockNumber = blockNumber;
71      }
72  
73      /**
74       * Returns the block number of the acknowledgement.
75       *
76       * @return The block number of the acknowledgement.
77       */
78      public int getBlockNumber() {
79          return blockNumber;
80      }
81  
82      /**
83       * 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
84       * wants to implement his own TFTP client instead of using the {@link org.apache.commons.net.tftp.TFTPClient} class. Under normal circumstances, you should
85       * not have a need to call this method.
86       *
87       * @return A UDP datagram containing the TFTP acknowledgement packet.
88       */
89      @Override
90      public DatagramPacket newDatagram() {
91          final byte[] data;
92  
93          data = new byte[4];
94          data[0] = 0;
95          data[1] = (byte) type;
96          data[2] = (byte) ((blockNumber & 0xffff) >> 8);
97          data[3] = (byte) (blockNumber & 0xff);
98  
99          return new DatagramPacket(data, data.length, address, port);
100     }
101 
102     /**
103      * This is a method only available within the package for implementing efficient datagram transport by eliminating buffering. It takes a datagram as an
104      * 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.
105      *
106      * @param datagram The datagram to create.
107      * @param data     The buffer to store the packet and to use in the datagram.
108      * @return The datagram argument.
109      */
110     @Override
111     DatagramPacket newDatagram(final DatagramPacket datagram, final byte[] data) {
112         data[0] = 0;
113         data[1] = (byte) type;
114         data[2] = (byte) ((blockNumber & 0xffff) >> 8);
115         data[3] = (byte) (blockNumber & 0xff);
116 
117         datagram.setAddress(address);
118         datagram.setPort(port);
119         datagram.setData(data);
120         datagram.setLength(4);
121 
122         return datagram;
123     }
124 
125     /**
126      * Sets the block number of the acknowledgement.
127      *
128      * @param blockNumber the number to set
129      */
130     public void setBlockNumber(final int blockNumber) {
131         this.blockNumber = blockNumber;
132     }
133 
134     /**
135      * For debugging
136      *
137      * @since 3.6
138      */
139     @Override
140     public String toString() {
141         return super.toString() + " ACK " + blockNumber;
142     }
143 }