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 Data 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 TFTPDataPacket extends TFTPPacket {
38      /** The maximum number of bytes in a TFTP data packet (512) */
39      public static final int MAX_DATA_LENGTH = 512;
40  
41      /** The minimum number of bytes in a TFTP data packet (0) */
42      public static final int MIN_DATA_LENGTH = 0;
43  
44      /** The block number of the packet. */
45      int blockNumber;
46  
47      /** The length of the data. */
48      private int length;
49  
50      /** The offset into the _data array at which the data begins. */
51      private int offset;
52  
53      /** The data stored in the packet. */
54      private byte[] data;
55  
56      /**
57       * Creates a data packet based from a received datagram. Assumes the datagram is at least length 4, else an ArrayIndexOutOfBoundsException may be thrown.
58       *
59       * @param datagram The datagram containing the received data.
60       * @throws TFTPPacketException If the datagram isn't a valid TFTP data packet.
61       */
62      TFTPDataPacket(final DatagramPacket datagram) throws TFTPPacketException {
63          super(TFTPPacket.DATA, datagram.getAddress(), datagram.getPort());
64  
65          this.data = datagram.getData();
66          this.offset = 4;
67  
68          if (getType() != this.data[1]) {
69              throw new TFTPPacketException("TFTP operator code does not match type.");
70          }
71  
72          this.blockNumber = (this.data[2] & 0xff) << 8 | this.data[3] & 0xff;
73  
74          this.length = datagram.getLength() - 4;
75  
76          if (this.length > MAX_DATA_LENGTH) {
77              this.length = MAX_DATA_LENGTH;
78          }
79      }
80  
81      public TFTPDataPacket(final InetAddress destination, final int port, final int blockNumber, final byte[] data) {
82          this(destination, port, blockNumber, data, 0, data.length);
83      }
84  
85      /**
86       * Creates a data packet to be sent to a host at a given port with a given block number. The actual data to be sent is passed as an array, an offset, and a
87       * length. The offset is the offset into the byte array where the data starts. The length is the length of the data. If the length is greater than
88       * MAX_DATA_LENGTH, it is truncated.
89       *
90       * @param destination The host to which the packet is going to be sent.
91       * @param port        The port to which the packet is going to be sent.
92       * @param blockNumber The block number of the data.
93       * @param data        The byte array containing the data.
94       * @param offset      The offset into the array where the data starts.
95       * @param length      The length of the data.
96       */
97      public TFTPDataPacket(final InetAddress destination, final int port, final int blockNumber, final byte[] data, final int offset, final int length) {
98          super(TFTPPacket.DATA, destination, port);
99          this.blockNumber = blockNumber;
100         this.data = data;
101         this.offset = offset;
102         this.length = Math.min(length, MAX_DATA_LENGTH);
103     }
104 
105     /**
106      * Returns the block number of the data packet.
107      *
108      * @return The block number of the data packet.
109      */
110     public int getBlockNumber() {
111         return blockNumber;
112     }
113 
114     /**
115      * Returns the byte array containing the packet data.
116      *
117      * @return The byte array containing the packet data.
118      */
119     public byte[] getData() {
120         return data;
121     }
122 
123     /**
124      * Returns the length of the data part of the data packet.
125      *
126      * @return The length of the data part of the data packet.
127      */
128     public int getDataLength() {
129         return length;
130     }
131 
132     /**
133      * Returns the offset into the byte array where the packet data actually starts.
134      *
135      * @return The offset into the byte array where the packet data actually starts.
136      */
137     public int getDataOffset() {
138         return offset;
139     }
140 
141     /**
142      * Creates a UDP datagram containing all the TFTP data packet data in the proper format. This is a method exposed to the programmer in case he wants to
143      * implement his own TFTP client instead of using the {@link org.apache.commons.net.tftp.TFTPClient} class. Under normal circumstances, you should not have
144      * a need to call this method.
145      *
146      * @return A UDP datagram containing the TFTP data packet.
147      */
148     @Override
149     public DatagramPacket newDatagram() {
150         final byte[] data;
151 
152         data = new byte[length + 4];
153         data[0] = 0;
154         data[1] = (byte) type;
155         data[2] = (byte) ((blockNumber & 0xffff) >> 8);
156         data[3] = (byte) (blockNumber & 0xff);
157 
158         System.arraycopy(this.data, offset, data, 4, length);
159 
160         return new DatagramPacket(data, length + 4, address, port);
161     }
162 
163     /**
164      * This is a method only available within the package for implementing efficient datagram transport by eliminating buffering. It takes a datagram as an
165      * 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.
166      *
167      * @param datagram The datagram to create.
168      * @param data     The buffer to store the packet and to use in the datagram.
169      * @return The datagram argument.
170      */
171     @Override
172     DatagramPacket newDatagram(final DatagramPacket datagram, final byte[] data) {
173         data[0] = 0;
174         data[1] = (byte) type;
175         data[2] = (byte) ((blockNumber & 0xffff) >> 8);
176         data[3] = (byte) (blockNumber & 0xff);
177 
178         // Doublecheck we're not the same
179         if (data != this.data) {
180             System.arraycopy(this.data, offset, data, 4, length);
181         }
182 
183         datagram.setAddress(address);
184         datagram.setPort(port);
185         datagram.setData(data);
186         datagram.setLength(length + 4);
187 
188         return datagram;
189     }
190 
191     /**
192      * Sets the block number of the data packet.
193      *
194      * @param blockNumber the number to set
195      */
196     public void setBlockNumber(final int blockNumber) {
197         this.blockNumber = blockNumber;
198     }
199 
200     /**
201      * Sets the data for the data packet.
202      *
203      * @param data   The byte array containing the data.
204      * @param offset The offset into the array where the data starts.
205      * @param length The length of the data.
206      */
207     public void setData(final byte[] data, final int offset, final int length) {
208         this.data = data;
209         this.offset = offset;
210         this.length = length;
211 
212         this.length = Math.min(length, MAX_DATA_LENGTH);
213     }
214 
215     /**
216      * For debugging
217      *
218      * @since 3.6
219      */
220     @Override
221     public String toString() {
222         return super.toString() + " DATA " + blockNumber + " " + length;
223     }
224 }