1 16 package org.apache.commons.net.tftp; 17 18 import java.net.DatagramPacket ; 19 import java.net.InetAddress ; 20 21 42 43 public final class TFTPAckPacket extends TFTPPacket 44 { 45 46 int _blockNumber; 47 48 56 public TFTPAckPacket(InetAddress destination, int port, int blockNumber) 57 { 58 super(TFTPPacket.ACKNOWLEDGEMENT, destination, port); 59 _blockNumber = blockNumber; 60 } 61 62 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 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 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 142 public int getBlockNumber() 143 { 144 return _blockNumber; 145 } 146 147 148 149 public void setBlockNumber(int blockNumber) 150 { 151 _blockNumber = blockNumber; 152 } 153 } 154 155 | Popular Tags |