1 package org.apache.commons.net.tftp;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Commons" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import java.net.DatagramPacket;
58 import java.net.InetAddress;
59
60 /****
61 * A final class derived from TFTPPacket definiing the TFTP Acknowledgement
62 * packet type.
63 * <p>
64 * Details regarding the TFTP protocol and the format of TFTP packets can
65 * be found in RFC 783. But the point of these classes is to keep you
66 * from having to worry about the internals. Additionally, only very
67 * few people should have to care about any of the TFTPPacket classes
68 * or derived classes. Almost all users should only be concerned with the
69 * <a href="org.apache.commons.net.tftp.TFTPClient.html#_top_">TFTPClient</a> class
70 * <a href="org.apache.commons.net.tftp.TFTPClient.html#receiveFile">receiveFile()</a>
71 * and
72 * <a href="org.apache.commons.net.tftp.TFTPClient.html#sendFile">sendFile()</a>
73 * methods.
74 * <p>
75 * <p>
76 * @author Daniel F. Savarese
77 * @see TFTPPacket
78 * @see TFTPPacketException
79 * @see TFTP
80 ***/
81
82 public final class TFTPAckPacket extends TFTPPacket
83 {
84 /**** The block number being acknowledged by the packet. ***/
85 int _blockNumber;
86
87 /****
88 * Creates an acknowledgment packet to be sent to a host at a given port
89 * acknowledging receipt of a block.
90 * <p>
91 * @param destination The host to which the packet is going to be sent.
92 * @param port The port to which the packet is going to be sent.
93 * @param blockNumber The block number being acknowledged.
94 ***/
95 public TFTPAckPacket(InetAddress destination, int port, int blockNumber)
96 {
97 super(TFTPPacket.ACKNOWLEDGEMENT, destination, port);
98 _blockNumber = blockNumber;
99 }
100
101 /****
102 * Creates an acknowledgement packet based from a received
103 * datagram. Assumes the datagram is at least length 4, else an
104 * ArrayIndexOutOfBoundsException may be thrown.
105 * <p>
106 * @param datagram The datagram containing the received acknowledgement.
107 * @throws TFTPPacketException If the datagram isn't a valid TFTP
108 * acknowledgement packet.
109 ***/
110 TFTPAckPacket(DatagramPacket datagram) throws TFTPPacketException
111 {
112 super(TFTPPacket.ACKNOWLEDGEMENT, datagram.getAddress(),
113 datagram.getPort());
114 byte[] data;
115
116 data = datagram.getData();
117
118 if (getType() != data[1])
119 throw new TFTPPacketException("TFTP operator code does not match type.");
120
121 _blockNumber = (((data[2] & 0xff) << 8) | (data[3] & 0xff));
122 }
123
124 /****
125 * This is a method only available within the package for
126 * implementing efficient datagram transport by elminating buffering.
127 * It takes a datagram as an argument, and a byte buffer in which
128 * to store the raw datagram data. Inside the method, the data
129 * is set as the datagram's data and the datagram returned.
130 * <p>
131 * @param datagram The datagram to create.
132 * @param data The buffer to store the packet and to use in the datagram.
133 * @return The datagram argument.
134 ***/
135 DatagramPacket _newDatagram(DatagramPacket datagram, byte[] data)
136 {
137 data[0] = 0;
138 data[1] = (byte)_type;
139 data[2] = (byte)((_blockNumber & 0xffff) >> 8);
140 data[3] = (byte)(_blockNumber & 0xff);
141
142 datagram.setAddress(_address);
143 datagram.setPort(_port);
144 datagram.setData(data);
145 datagram.setLength(4);
146
147 return datagram;
148 }
149
150
151 /****
152 * Creates a UDP datagram containing all the TFTP
153 * acknowledgement packet data in the proper format.
154 * This is a method exposed to the programmer in case he
155 * wants to implement his own TFTP client instead of using
156 * the <a href="org.apache.commons.net.tftp.TFTPClient.html#_top_">TFTPClient</a>
157 * class. Under normal circumstances, you should not have a need to call this
158 * method.
159 * <p>
160 * @return A UDP datagram containing the TFTP acknowledgement packet.
161 ***/
162 public DatagramPacket newDatagram()
163 {
164 byte[] data;
165
166 data = new byte[4];
167 data[0] = 0;
168 data[1] = (byte)_type;
169 data[2] = (byte)((_blockNumber & 0xffff) >> 8);
170 data[3] = (byte)(_blockNumber & 0xff);
171
172 return new DatagramPacket(data, data.length, _address, _port);
173 }
174
175
176 /****
177 * Returns the block number of the acknowledgement.
178 * <p>
179 * @return The block number of the acknowledgement.
180 ***/
181 public int getBlockNumber()
182 {
183 return _blockNumber;
184 }
185
186
187 /**** Sets the block number of the acknowledgement. ***/
188 public void setBlockNumber(int blockNumber)
189 {
190 _blockNumber = blockNumber;
191 }
192 }
193
This page was automatically generated by Maven