View Javadoc

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