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 TFTPErrorPacket extends TFTPPacket 44 { 45 46 public static final int UNDEFINED = 0; 47 48 49 public static final int FILE_NOT_FOUND = 1; 50 51 52 public static final int ACCESS_VIOLATION = 2; 53 54 55 public static final int OUT_OF_SPACE = 3; 56 57 60 public static final int ILLEGAL_OPERATION = 4; 61 62 63 public static final int UNKNOWN_TID = 5; 64 65 66 public static final int FILE_EXISTS = 6; 67 68 69 public static final int NO_SUCH_USER = 7; 70 71 72 int _error; 73 74 75 String _message; 76 77 86 public TFTPErrorPacket(InetAddress destination, int port, 87 int error, String message) 88 { 89 super(TFTPPacket.ERROR, destination, port); 90 91 _error = error; 92 _message = message; 93 } 94 95 104 TFTPErrorPacket(DatagramPacket datagram) throws TFTPPacketException 105 { 106 super(TFTPPacket.ERROR, datagram.getAddress(), datagram.getPort()); 107 int index, length; 108 byte[] data; 109 StringBuffer buffer; 110 111 data = datagram.getData(); 112 length = datagram.getLength(); 113 114 if (getType() != data[1]) 115 throw new TFTPPacketException("TFTP operator code does not match type."); 116 117 _error = (((data[2] & 0xff) << 8) | (data[3] & 0xff)); 118 119 if (length < 5) 120 throw new TFTPPacketException("Bad error packet. No message."); 121 122 index = 4; 123 buffer = new StringBuffer (); 124 125 while (index < length && data[index] != 0) 126 { 127 buffer.append((char)data[index]); 128 ++index; 129 } 130 131 _message = buffer.toString(); 132 } 133 134 145 DatagramPacket _newDatagram(DatagramPacket datagram, byte[] data) 146 { 147 int length; 148 149 length = _message.length(); 150 151 data[0] = 0; 152 data[1] = (byte)_type; 153 data[2] = (byte)((_error & 0xffff) >> 8); 154 data[3] = (byte)(_error & 0xff); 155 156 System.arraycopy(_message.getBytes(), 0, data, 4, length); 157 158 data[length + 4] = 0; 159 160 datagram.setAddress(_address); 161 datagram.setPort(_port); 162 datagram.setData(data); 163 datagram.setLength(length + 4); 164 165 return datagram; 166 } 167 168 169 181 public DatagramPacket newDatagram() 182 { 183 byte[] data; 184 int length; 185 186 length = _message.length(); 187 188 data = new byte[length + 5]; 189 data[0] = 0; 190 data[1] = (byte)_type; 191 data[2] = (byte)((_error & 0xffff) >> 8); 192 data[3] = (byte)(_error & 0xff); 193 194 System.arraycopy(_message.getBytes(), 0, data, 4, length); 195 196 data[length + 4] = 0; 197 198 return new DatagramPacket (data, data.length, _address, _port); 199 } 200 201 202 207 public int getError() 208 { 209 return _error; 210 } 211 212 213 218 public String getMessage() 219 { 220 return _message; 221 } 222 } 223 | Popular Tags |