1 16 package org.apache.commons.net.tftp; 17 18 import java.io.IOException ; 19 import java.io.InterruptedIOException ; 20 import java.net.DatagramPacket ; 21 import java.net.SocketException ; 22 import org.apache.commons.net.DatagramSocketClient; 23 24 45 46 public class TFTP extends DatagramSocketClient 47 { 48 51 public static final int ASCII_MODE = 0; 52 53 56 public static final int NETASCII_MODE = 0; 57 58 61 public static final int BINARY_MODE = 1; 62 63 66 public static final int IMAGE_MODE = 1; 67 68 71 public static final int OCTET_MODE = 1; 72 73 77 public static final int DEFAULT_TIMEOUT = 5000; 78 79 82 public static final int DEFAULT_PORT = 69; 83 84 88 static final int PACKET_SIZE = TFTPPacket.SEGMENT_SIZE + 4; 89 90 91 private byte[] __receiveBuffer; 92 93 94 private DatagramPacket __receiveDatagram; 95 96 97 private DatagramPacket __sendDatagram; 98 99 105 byte[] _sendBuffer; 106 107 108 116 public static final String getModeName(int mode) 117 { 118 return TFTPRequestPacket._modeStrings[mode]; 119 } 120 121 125 public TFTP() 126 { 127 setDefaultTimeout(DEFAULT_TIMEOUT); 128 __receiveBuffer = null; 129 __receiveDatagram = null; 130 } 131 132 139 public final void discardPackets() throws IOException 140 { 141 int to; 142 DatagramPacket datagram; 143 144 datagram = new DatagramPacket (new byte[PACKET_SIZE], PACKET_SIZE); 145 146 to = getSoTimeout(); 147 setSoTimeout(1); 148 149 try 150 { 151 while (true) 152 _socket_.receive(datagram); 153 } 154 catch (SocketException e) 155 { 156 } 158 catch (InterruptedIOException e) 159 { 160 } 162 163 setSoTimeout(to); 164 } 165 166 167 194 public final TFTPPacket bufferedReceive() throws IOException , 195 InterruptedIOException , SocketException , TFTPPacketException 196 { 197 __receiveDatagram.setData(__receiveBuffer); 198 __receiveDatagram.setLength(__receiveBuffer.length); 199 _socket_.receive(__receiveDatagram); 200 201 return TFTPPacket.newTFTPPacket(__receiveDatagram); 202 } 203 204 222 public final void bufferedSend(TFTPPacket packet) throws IOException 223 { 224 _socket_.send(packet._newDatagram(__sendDatagram, _sendBuffer)); 225 } 226 227 228 236 public final void beginBufferedOps() 237 { 238 __receiveBuffer = new byte[PACKET_SIZE]; 239 __receiveDatagram = 240 new DatagramPacket (__receiveBuffer, __receiveBuffer.length); 241 _sendBuffer = new byte[PACKET_SIZE]; 242 __sendDatagram = 243 new DatagramPacket (_sendBuffer, _sendBuffer.length); 244 } 245 246 249 public final void endBufferedOps() 250 { 251 __receiveBuffer = null; 252 __receiveDatagram = null; 253 _sendBuffer = null; 254 __sendDatagram = null; 255 } 256 257 258 264 public final void send(TFTPPacket packet) throws IOException 265 { 266 _socket_.send(packet.newDatagram()); 267 } 268 269 270 285 public final TFTPPacket receive() throws IOException , InterruptedIOException , 286 SocketException , TFTPPacketException 287 { 288 DatagramPacket packet; 289 290 packet = new DatagramPacket (new byte[PACKET_SIZE], PACKET_SIZE); 291 292 _socket_.receive(packet); 293 294 return TFTPPacket.newTFTPPacket(packet); 295 } 296 297 298 } 299 | Popular Tags |