001 /* 002 * Copyright 2001-2005 The Apache Software Foundation 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.apache.commons.net.tftp; 017 018 import java.net.DatagramPacket; 019 import java.net.InetAddress; 020 021 /*** 022 * A final class derived from TFTPPacket definiing the TFTP Acknowledgement 023 * packet type. 024 * <p> 025 * Details regarding the TFTP protocol and the format of TFTP packets can 026 * be found in RFC 783. But the point of these classes is to keep you 027 * from having to worry about the internals. Additionally, only very 028 * few people should have to care about any of the TFTPPacket classes 029 * or derived classes. Almost all users should only be concerned with the 030 * {@link org.apache.commons.net.tftp.TFTPClient} class 031 * {@link org.apache.commons.net.tftp.TFTPClient#receiveFile receiveFile()} 032 * and 033 * {@link org.apache.commons.net.tftp.TFTPClient#sendFile sendFile()} 034 * methods. 035 * <p> 036 * <p> 037 * @author Daniel F. Savarese 038 * @see TFTPPacket 039 * @see TFTPPacketException 040 * @see TFTP 041 ***/ 042 043 public final class TFTPAckPacket extends TFTPPacket 044 { 045 /*** The block number being acknowledged by the packet. ***/ 046 int _blockNumber; 047 048 /*** 049 * Creates an acknowledgment packet to be sent to a host at a given port 050 * acknowledging receipt of a block. 051 * <p> 052 * @param destination The host to which the packet is going to be sent. 053 * @param port The port to which the packet is going to be sent. 054 * @param blockNumber The block number being acknowledged. 055 ***/ 056 public TFTPAckPacket(InetAddress destination, int port, int blockNumber) 057 { 058 super(TFTPPacket.ACKNOWLEDGEMENT, destination, port); 059 _blockNumber = blockNumber; 060 } 061 062 /*** 063 * Creates an acknowledgement packet based from a received 064 * datagram. Assumes the datagram is at least length 4, else an 065 * ArrayIndexOutOfBoundsException may be thrown. 066 * <p> 067 * @param datagram The datagram containing the received acknowledgement. 068 * @throws TFTPPacketException If the datagram isn't a valid TFTP 069 * acknowledgement packet. 070 ***/ 071 TFTPAckPacket(DatagramPacket datagram) throws TFTPPacketException 072 { 073 super(TFTPPacket.ACKNOWLEDGEMENT, datagram.getAddress(), 074 datagram.getPort()); 075 byte[] data; 076 077 data = datagram.getData(); 078 079 if (getType() != data[1]) 080 throw new TFTPPacketException("TFTP operator code does not match type."); 081 082 _blockNumber = (((data[2] & 0xff) << 8) | (data[3] & 0xff)); 083 } 084 085 /*** 086 * This is a method only available within the package for 087 * implementing efficient datagram transport by elminating buffering. 088 * It takes a datagram as an argument, and a byte buffer in which 089 * to store the raw datagram data. Inside the method, the data 090 * is set as the datagram's data and the datagram returned. 091 * <p> 092 * @param datagram The datagram to create. 093 * @param data The buffer to store the packet and to use in the datagram. 094 * @return The datagram argument. 095 ***/ 096 DatagramPacket _newDatagram(DatagramPacket datagram, byte[] data) 097 { 098 data[0] = 0; 099 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