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 definiing the TFTP Acknowledgement
25   * packet type.
26   * <p>
27   * Details regarding the TFTP protocol and the format of TFTP packets can
28   * be found in RFC 783.  But the point of these classes is to keep you
29   * from having to worry about the internals.  Additionally, only very
30   * few people should have to care about any of the TFTPPacket classes
31   * or derived classes.  Almost all users should only be concerned with the
32   * {@link org.apache.commons.net.tftp.TFTPClient} class
33   * {@link org.apache.commons.net.tftp.TFTPClient#receiveFile receiveFile()}
34   * and
35   * {@link org.apache.commons.net.tftp.TFTPClient#sendFile sendFile()}
36   * methods.
37   * <p>
38   * <p>
39   * @author Daniel F. Savarese
40   * @see TFTPPacket
41   * @see TFTPPacketException
42   * @see TFTP
43   ***/
44  
45  public final class TFTPAckPacket extends TFTPPacket
46  {
47      /*** The block number being acknowledged by the packet. ***/
48      int _blockNumber;
49  
50      /***
51       * Creates an acknowledgment packet to be sent to a host at a given port
52       * acknowledging receipt of a block.
53       * <p>
54       * @param destination  The host to which the packet is going to be sent.
55       * @param port  The port to which the packet is going to be sent.
56       * @param blockNumber  The block number being acknowledged.
57       ***/
58      public TFTPAckPacket(InetAddress destination, int port, int blockNumber)
59      {
60          super(TFTPPacket.ACKNOWLEDGEMENT, destination, port);
61          _blockNumber = blockNumber;
62      }
63  
64      /***
65       * Creates an acknowledgement packet based from a received
66       * datagram.  Assumes the datagram is at least length 4, else an
67       * ArrayIndexOutOfBoundsException may be thrown.
68       * <p>
69       * @param datagram  The datagram containing the received acknowledgement.
70       * @throws TFTPPacketException  If the datagram isn't a valid TFTP
71       *         acknowledgement packet.
72       ***/
73      TFTPAckPacket(DatagramPacket datagram) throws TFTPPacketException
74      {
75          super(TFTPPacket.ACKNOWLEDGEMENT, datagram.getAddress(),
76                datagram.getPort());
77          byte[] data;
78  
79          data = datagram.getData();
80  
81          if (getType() != data[1])
82              throw new TFTPPacketException("TFTP operator code does not match type.");
83  
84          _blockNumber = (((data[2] & 0xff) << 8) | (data[3] & 0xff));
85      }
86  
87      /***
88       * This is a method only available within the package for
89       * implementing efficient datagram transport by elminating buffering.
90       * It takes a datagram as an argument, and a byte buffer in which
91       * to store the raw datagram data.  Inside the method, the data
92       * is set as the datagram's data and the datagram returned.
93       * <p>
94       * @param datagram  The datagram to create.
95       * @param data The buffer to store the packet and to use in the datagram.
96       * @return The datagram argument.
97       ***/
98      @Override
99      DatagramPacket _newDatagram(DatagramPacket datagram, byte[] data)
100     {
101         data[0] = 0;
102         data[1] = (byte)_type;
103         data[2] = (byte)((_blockNumber & 0xffff) >> 8);
104         data[3] = (byte)(_blockNumber & 0xff);
105 
106         datagram.setAddress(_address);
107         datagram.setPort(_port);
108         datagram.setData(data);
109         datagram.setLength(4);
110 
111         return datagram;
112     }
113 
114 
115     /***
116      * Creates a UDP datagram containing all the TFTP
117      * acknowledgement packet data in the proper format.
118      * This is a method exposed to the programmer in case he
119      * wants to implement his own TFTP client instead of using
120      * the {@link org.apache.commons.net.tftp.TFTPClient}
121      * class.  Under normal circumstances, you should not have a need to call this
122      * method.
123      * <p>
124      * @return A UDP datagram containing the TFTP acknowledgement packet.
125      ***/
126     @Override
127     public DatagramPacket newDatagram()
128     {
129         byte[] data;
130 
131         data = new byte[4];
132         data[0] = 0;
133         data[1] = (byte)_type;
134         data[2] = (byte)((_blockNumber & 0xffff) >> 8);
135         data[3] = (byte)(_blockNumber & 0xff);
136 
137         return new DatagramPacket(data, data.length, _address, _port);
138     }
139 
140 
141     /***
142      * Returns the block number of the acknowledgement.
143      * <p>
144      * @return The block number of the acknowledgement.
145      ***/
146     public int getBlockNumber()
147     {
148         return _blockNumber;
149     }
150 
151 
152     /*** Sets the block number of the acknowledgement.  ***/
153     public void setBlockNumber(int blockNumber)
154     {
155         _blockNumber = blockNumber;
156     }
157 }
158