/** * For debugging * @since 3.6 */ @Override public String toString() { return super.toString() + " WRQ " + getFilename() + " " + TFTP.getModeName(getMode()); } }
/** * For debugging * @since 3.6 */ @Override public String toString() { return super.toString() + " RRQ " + getFilename() + " " + TFTP.getModeName(getMode()); } }
/** * For debugging * @since 3.6 */ @Override public String toString() { return super.toString() + " DATA " + _blockNumber + " " + _length; } }
throw new TFTPPacketException( "Bad packet. Datagram data length is too short."); packet = new TFTPReadRequestPacket(datagram); break; case WRITE_REQUEST: packet = new TFTPWriteRequestPacket(datagram); break; case DATA: packet = new TFTPDataPacket(datagram); break; case ACKNOWLEDGEMENT: packet = new TFTPAckPacket(datagram); break; case ERROR: packet = new TFTPErrorPacket(datagram); break; default: throw new TFTPPacketException( "Bad packet. Invalid TFTP operator code.");
/*** * Creates an acknowledgement packet based from a received * datagram. Assumes the datagram is at least length 4, else an * ArrayIndexOutOfBoundsException may be thrown. * * @param datagram The datagram containing the received acknowledgement. * @throws TFTPPacketException If the datagram isn't a valid TFTP * acknowledgement packet. ***/ TFTPAckPacket(DatagramPacket datagram) throws TFTPPacketException { super(TFTPPacket.ACKNOWLEDGEMENT, datagram.getAddress(), datagram.getPort()); byte[] data; data = datagram.getData(); if (getType() != data[1]) { throw new TFTPPacketException("TFTP operator code does not match type."); } _blockNumber = (((data[2] & 0xff) << 8) | (data[3] & 0xff)); }
/*** * Creates a data packet based from a received * datagram. Assumes the datagram is at least length 4, else an * ArrayIndexOutOfBoundsException may be thrown. * * @param datagram The datagram containing the received data. * @throws TFTPPacketException If the datagram isn't a valid TFTP * data packet. ***/ TFTPDataPacket(DatagramPacket datagram) throws TFTPPacketException { super(TFTPPacket.DATA, datagram.getAddress(), datagram.getPort()); _data = datagram.getData(); _offset = 4; if (getType() != _data[1]) { throw new TFTPPacketException("TFTP operator code does not match type."); } _blockNumber = (((_data[2] & 0xff) << 8) | (_data[3] & 0xff)); _length = datagram.getLength() - 4; if (_length > MAX_DATA_LENGTH) { _length = MAX_DATA_LENGTH; } }
/*** * Same as calling sendFile(filename, mode, input, host, TFTP.DEFAULT_PORT). * * @param filename The name the remote server should use when creating * the file on its file system. * @param mode The TFTP mode of the transfer (one of the MODE constants). * @param input the input stream containing the data to be sent * @param host The name of the remote host receiving the file. * @throws IOException If an I/O error occurs. The nature of the * error will be reported in the message. * @throws UnknownHostException If the hostname cannot be resolved. ***/ public void sendFile(String filename, int mode, InputStream input, InetAddress host) throws IOException { sendFile(filename, mode, input, host, DEFAULT_PORT); }
length = datagram.getLength(); if (getType() != data[1]) { throw new TFTPPacketException("TFTP operator code does not match type."); throw new TFTPPacketException("Bad error packet. No message.");
/*** * Creates a TFTP instance with a default timeout of DEFAULT_TIMEOUT, * a null socket, and buffered operations disabled. ***/ public TFTP() { setDefaultTimeout(DEFAULT_TIMEOUT); __receiveBuffer = null; __receiveDatagram = null; }
/*** * Receives a TFTPPacket. * * @return The TFTPPacket received. * @throws InterruptedIOException If a socket timeout occurs. The * Java documentation claims an InterruptedIOException is thrown * on a DatagramSocket timeout, but in practice we find a * SocketException is thrown. You should catch both to be safe. * @throws SocketException If a socket timeout occurs. The * Java documentation claims an InterruptedIOException is thrown * on a DatagramSocket timeout, but in practice we find a * SocketException is thrown. You should catch both to be safe. * @throws IOException If some other I/O error occurs. * @throws TFTPPacketException If an invalid TFTP packet is received. ***/ public final TFTPPacket receive() throws IOException, InterruptedIOException, SocketException, TFTPPacketException { DatagramPacket packet; packet = new DatagramPacket(new byte[PACKET_SIZE], PACKET_SIZE); _socket_.receive(packet); TFTPPacket newTFTPPacket = TFTPPacket.newTFTPPacket(packet); trace("<", newTFTPPacket); return newTFTPPacket; }
/*** * Sends a TFTP packet to its destination. * * @param packet The TFTP packet to send. * @throws IOException If some I/O error occurs. ***/ public final void send(TFTPPacket packet) throws IOException { trace(">", packet); _socket_.send(packet.newDatagram()); }
/*** * Same as calling receiveFile(filename, mode, output, host, TFTP.DEFAULT_PORT). * * @param filename The name of the file to receive. * @param mode The TFTP mode of the transfer (one of the MODE constants). * @param output The OutputStream to which the file should be written. * @param host The remote host serving the file. * @return number of bytes read * @throws IOException If an I/O error occurs. The nature of the * error will be reported in the message. ***/ public int receiveFile(String filename, int mode, OutputStream output, InetAddress host) throws IOException { return receiveFile(filename, mode, output, host, DEFAULT_PORT); }
/*** * This is a special method to perform a more efficient packet send. * It should only be used after calling * {@link #beginBufferedOps beginBufferedOps() }. beginBufferedOps() * initializes a set of buffers used internally that prevent the new * allocation of a DatagramPacket and byte array for each send and receive. * To use these buffers you must call the bufferedReceive() and * bufferedSend() methods instead of send() and receive(). You must * also be certain that you don't manipulate the resulting packet in * such a way that it interferes with future buffered operations. * For example, a TFTPDataPacket received with bufferedReceive() will * have a reference to the internal byte buffer. You must finish using * this data before calling bufferedReceive() again, or else the data * will be overwritten by the the call. * * @param packet The TFTP packet to send. * @throws IOException If some I/O error occurs. ***/ public final void bufferedSend(TFTPPacket packet) throws IOException { trace(">", packet); _socket_.send(packet._newDatagram(__sendDatagram, _sendBuffer)); }
/*** * Same as calling sendFile(filename, mode, input, hostname, TFTP.DEFAULT_PORT). * * @param filename The name the remote server should use when creating * the file on its file system. * @param mode The TFTP mode of the transfer (one of the MODE constants). * @param input the input stream containing the data to be sent * @param hostname The name of the remote host receiving the file. * @throws IOException If an I/O error occurs. The nature of the * error will be reported in the message. * @throws UnknownHostException If the hostname cannot be resolved. ***/ public void sendFile(String filename, int mode, InputStream input, String hostname) throws UnknownHostException, IOException { sendFile(filename, mode, input, InetAddress.getByName(hostname), DEFAULT_PORT); } }
_socket_.receive(__receiveDatagram); TFTPPacket newTFTPPacket = TFTPPacket.newTFTPPacket(__receiveDatagram); trace("<", newTFTPPacket); return newTFTPPacket;
/*** * Same as calling receiveFile(filename, mode, output, hostname, TFTP.DEFAULT_PORT). * * @param filename The name of the file to receive. * @param mode The TFTP mode of the transfer (one of the MODE constants). * @param output The OutputStream to which the file should be written. * @param hostname The name of the remote host serving the file. * @return number of bytes read * @throws IOException If an I/O error occurs. The nature of the * error will be reported in the message. * @throws UnknownHostException If the hostname cannot be resolved. ***/ public int receiveFile(String filename, int mode, OutputStream output, String hostname) throws UnknownHostException, IOException { return receiveFile(filename, mode, output, InetAddress.getByName(hostname), DEFAULT_PORT); }
/** * For debugging * @since 3.6 */ @Override public String toString() { return super.toString() + " ACK " + _blockNumber; } }
/*** * Requests to send a file to a remote host, reads the file from an * InputStream, sends the file to the remote host, and closes the * connection. A local UDP socket must first be created by * {@link org.apache.commons.net.DatagramSocketClient#open open()} before * invoking this method. This method will not close the InputStream * containing the file; you must close it after the method invocation. * * @param filename The name the remote server should use when creating * the file on its file system. * @param mode The TFTP mode of the transfer (one of the MODE constants). * @param input the input stream containing the data to be sent * @param hostname The name of the remote host receiving the file. * @param port The port number of the remote TFTP server. * @throws IOException If an I/O error occurs. The nature of the * error will be reported in the message. * @throws UnknownHostException If the hostname cannot be resolved. ***/ public void sendFile(String filename, int mode, InputStream input, String hostname, int port) throws UnknownHostException, IOException { sendFile(filename, mode, input, InetAddress.getByName(hostname), port); }
/*** * Requests a named file from a remote host, writes the * file to an OutputStream, closes the connection, and returns the number * of bytes read. A local UDP socket must first be created by * {@link org.apache.commons.net.DatagramSocketClient#open open()} before * invoking this method. This method will not close the OutputStream * containing the file; you must close it after the method invocation. * * @param filename The name of the file to receive. * @param mode The TFTP mode of the transfer (one of the MODE constants). * @param output The OutputStream to which the file should be written. * @param hostname The name of the remote host serving the file. * @param port The port number of the remote TFTP server. * @return number of bytes read * @throws IOException If an I/O error occurs. The nature of the * error will be reported in the message. * @throws UnknownHostException If the hostname cannot be resolved. ***/ public int receiveFile(String filename, int mode, OutputStream output, String hostname, int port) throws UnknownHostException, IOException { return receiveFile(filename, mode, output, InetAddress.getByName(hostname), port); }
/** * For debugging * @since 3.6 */ @Override public String toString() { return super.toString() + " ERR " + _error + " " + _message; } }