Advertisement
Guest User

Untitled

a guest
May 7th, 2016
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.51 KB | None | 0 0
  1.  
  2.  
  3. import java.io.*;
  4. import java.net.*;
  5. import java.util.*;
  6.  
  7. public class FTPClient extends Object {
  8.  
  9. /**
  10. * If this flag is on, we print out debugging information to stdout during
  11. * execution. Useful for debugging the FTP class and seeing the server's
  12. * responses directly.
  13. */
  14. private static boolean PRINT_DEBUG_INFO = false;
  15.  
  16. /**
  17. * The socket through which we are connected to the FTP server.
  18. */
  19. private Socket connectionSocket = null;
  20. /**
  21. * The socket output stream.
  22. */
  23. private PrintStream outputStream = null;
  24. /**
  25. * The socket input stream.
  26. */
  27. private BufferedReader inputStream = null;
  28.  
  29. /**
  30. * The offset at which we resume a file transfer.
  31. */
  32. private long restartPoint = 0L;
  33.  
  34. /**
  35. * Added by Julian: If this flag is on, we're currently logged in to something.
  36. */
  37. private boolean loggedIn = false;
  38.  
  39. /**
  40. * Added by Julian: This is the line terminator to use for multi-line responses.
  41. */
  42. public String lineTerm = "\n";
  43.  
  44. /**
  45. * Added by Julian: This is the size of the data blocks we use for transferring
  46. * files.
  47. */
  48. private static int BLOCK_SIZE = 4096;
  49.  
  50.  
  51. /**
  52. * Added by Julian: After you create an FTPConnection object, you will call the
  53. * connect() and login() methods to access your server. Please don't forget to
  54. * logout() and disconnect() when you're done (it's only polite...).
  55. */
  56. public FTPClient ()
  57. {
  58. // default constructor (obviously) -- this is just good to have...
  59. }
  60.  
  61.  
  62. /**
  63. * Added by Julian: Allows you to specify if you want to send debug output to
  64. * the console (true if you do, false if you don't).
  65. */
  66. public FTPClient (boolean debugOut)
  67. {
  68. PRINT_DEBUG_INFO = debugOut;
  69. }
  70.  
  71.  
  72. /**
  73. * Prints debugging information to stdout if the private flag
  74. * <code>PRINT_DEBUG_INFO</code> is turned on.
  75. */
  76. private void debugPrint(String message) {
  77. if (PRINT_DEBUG_INFO) System.err.println(message);
  78. }
  79.  
  80.  
  81. /**
  82. * Connects to the given FTP host on port 21, the default FTP port.
  83. */
  84. public boolean connect(String host)
  85. throws UnknownHostException, IOException
  86. {
  87. return connect(host, 21);
  88. }
  89.  
  90.  
  91. /**
  92. * Connects to the given FTP host on the given port.
  93. */
  94. public boolean connect(String host, int port)
  95. throws UnknownHostException, IOException
  96. {
  97. connectionSocket = new Socket(host, port);
  98. outputStream = new PrintStream(connectionSocket.getOutputStream());
  99. inputStream = new BufferedReader(new
  100. InputStreamReader(connectionSocket.getInputStream()));
  101.  
  102. if (!isPositiveCompleteResponse(getServerReply())){
  103. disconnect();
  104. return false;
  105. }
  106.  
  107. return true;
  108. }
  109.  
  110.  
  111. /**
  112. * Disconnects from the host to which we are currently connected.
  113. */
  114. public void disconnect()
  115. {
  116. if (outputStream != null) {
  117. try {
  118. if (loggedIn) { logout(); };
  119. outputStream.close();
  120. inputStream.close();
  121. connectionSocket.close();
  122. } catch (IOException e) {}
  123.  
  124. outputStream = null;
  125. inputStream = null;
  126. connectionSocket = null;
  127. }
  128. }
  129.  
  130.  
  131. /**
  132. * Wrapper for the commands <code>user [username]</code> and <code>pass
  133. * [password]</code>.
  134. */
  135. public boolean login(String username, String password)
  136. throws IOException
  137. {
  138. int response = executeCommand("user " + username);
  139. if (!isPositiveIntermediateResponse(response)) return false;
  140. response = executeCommand("pass " + password);
  141. loggedIn = isPositiveCompleteResponse(response);
  142. return loggedIn;
  143. }
  144.  
  145.  
  146. /**
  147. * Added by Julian: Logout before you disconnect (this is good form).
  148. */
  149. public boolean logout()
  150. throws IOException
  151. {
  152. int response = executeCommand("quit");
  153. loggedIn = !isPositiveCompleteResponse(response);
  154. return !loggedIn;
  155. }
  156.  
  157.  
  158. /**
  159. * Wrapper for the command <code>cwd [directory]</code>.
  160. */
  161. public boolean changeDirectory(String directory)
  162. throws IOException
  163. {
  164. int response = executeCommand("cwd " + directory);
  165. return isPositiveCompleteResponse(response);
  166. }
  167.  
  168.  
  169. /**
  170. * Wrapper for the commands <code>rnfr [oldName]</code> and <code>rnto
  171. * [newName]</code>.
  172. */
  173. public boolean renameFile(String oldName, String newName)
  174. throws IOException
  175. {
  176. int response = executeCommand("rnfr " + oldName);
  177. if (!isPositiveIntermediateResponse(response)) return false;
  178. response = executeCommand("rnto " + newName);
  179. return isPositiveCompleteResponse(response);
  180. }
  181.  
  182.  
  183. /**
  184. * Wrapper for the command <code>mkd [directory]</code>.
  185. */
  186. public boolean makeDirectory(String directory)
  187. throws IOException
  188. {
  189. int response = executeCommand("mkd " + directory);
  190. return isPositiveCompleteResponse(response);
  191. }
  192.  
  193.  
  194. /**
  195. * Wrapper for the command <code>rmd [directory]</code>.
  196. */
  197. public boolean removeDirectory(String directory)
  198. throws IOException
  199. {
  200. int response = executeCommand("rmd " + directory);
  201. return isPositiveCompleteResponse(response);
  202. }
  203.  
  204.  
  205. /**
  206. * Wrapper for the command <code>cdup</code>.
  207. */
  208. public boolean parentDirectory()
  209. throws IOException
  210. {
  211. int response = executeCommand("cdup");
  212. return isPositiveCompleteResponse(response);
  213. }
  214.  
  215.  
  216. /**
  217. * Wrapper for the command <code>dele [fileName]</code>.
  218. */
  219. public boolean deleteFile(String fileName)
  220. throws IOException
  221. {
  222. int response = executeCommand("dele " + fileName);
  223. return isPositiveCompleteResponse(response);
  224. }
  225.  
  226.  
  227. /**
  228. * Wrapper for the command <code>pwd</code>.
  229. */
  230. public String getCurrentDirectory()
  231. throws IOException
  232. {
  233. String response = getExecutionResponse("pwd");
  234. StringTokenizer strtok = new StringTokenizer(response);
  235.  
  236. // Get rid of the first token, which is the return code
  237. if (strtok.countTokens() < 2) return null;
  238. strtok.nextToken();
  239. String directoryName = strtok.nextToken();
  240.  
  241. // Most servers surround the directory name with quotation marks
  242. int strlen = directoryName.length();
  243. if (strlen == 0) return null;
  244. if (directoryName.charAt(0) == '\"') {
  245. directoryName = directoryName.substring(1);
  246. strlen--;
  247. }
  248. if (directoryName.charAt(strlen - 1) == '\"')
  249. return directoryName.substring(0, strlen - 1);
  250. return directoryName;
  251. }
  252.  
  253.  
  254. /**
  255. * Wrapper for the command <code>syst</code>.
  256. */
  257. public String getSystemType()
  258. throws IOException
  259. {
  260. return excludeCode(getExecutionResponse("syst"));
  261. }
  262.  
  263.  
  264. /**
  265. * Wrapper for the command <code>mdtm [fileName]</code>. If the file does
  266. * not exist, we return -1;
  267. */
  268. public long getModificationTime(String fileName)
  269. throws IOException
  270. {
  271. String response = excludeCode(getExecutionResponse("mdtm " + fileName));
  272. try {
  273. return Long.parseLong(response);
  274. } catch (Exception e) {
  275. return -1L;
  276. }
  277. }
  278.  
  279.  
  280. /**
  281. * Wrapper for the command <code>size [fileName]</code>. If the file does
  282. * not exist, we return -1;
  283. */
  284. public long getFileSize(String fileName)
  285. throws IOException
  286. {
  287. String response = excludeCode(getExecutionResponse("size " + fileName));
  288. try {
  289. return Long.parseLong(response);
  290. } catch (Exception e) {
  291. return -1L;
  292. }
  293. }
  294.  
  295.  
  296. /**
  297. * Wrapper for the command <code>retr [fileName]</code>.
  298. */
  299. public boolean downloadFile(String fileName)
  300. throws IOException
  301. {
  302. return readDataToFile("retr " + fileName, fileName);
  303. }
  304.  
  305.  
  306. /**
  307. * Wrapper for the command <code>retr [serverPath]</code>. The local file
  308. * path to which we will write is given by <code>localPath</code>.
  309. */
  310. public boolean downloadFile(String serverPath, String localPath)
  311. throws IOException
  312. {
  313. return readDataToFile("retr " + serverPath, localPath);
  314. }
  315.  
  316.  
  317. /**
  318. * Wrapper for the command <code>stor [fileName]</code>.
  319. */
  320. public boolean uploadFile(String fileName)
  321. throws IOException
  322. {
  323. return writeDataFromFile("stor " + fileName, fileName);
  324. }
  325.  
  326.  
  327. /**
  328. * Wrapper for the command <code>stor [localPath]</code>. The server file
  329. * path to which we will write is given by <code>serverPath</code>.
  330. */
  331. public boolean uploadFile(String serverPath, String localPath)
  332. throws IOException
  333. {
  334. return writeDataFromFile("stor " + serverPath, localPath);
  335. }
  336.  
  337.  
  338. /**
  339. * Set the restart point for the next download or upload operation. This
  340. * lets clients resume interrupted uploads or downloads.
  341. */
  342. public void setRestartPoint(int point)
  343. {
  344. restartPoint = point;
  345. debugPrint("Restart noted");
  346. }
  347.  
  348.  
  349. /**
  350. * Gets server reply code from the control port after an ftp command has
  351. * been executed. It knows the last line of the response because it begins
  352. * with a 3 digit number and a space, (a dash instead of a space would be a
  353. * continuation).
  354. */
  355. private int getServerReply()
  356. throws IOException
  357. {
  358. return Integer.parseInt(getFullServerReply().substring(0, 3));
  359. }
  360.  
  361.  
  362. /**
  363. * Gets server reply string from the control port after an ftp command has
  364. * been executed. This consists only of the last line of the response,
  365. * and only the part after the response code.
  366. */
  367. private String getFullServerReply()
  368. throws IOException
  369. {
  370. String reply;
  371.  
  372. do {
  373. reply = inputStream.readLine();
  374. debugPrint(reply);
  375. } while(!(Character.isDigit(reply.charAt(0)) &&
  376. Character.isDigit(reply.charAt(1)) &&
  377. Character.isDigit(reply.charAt(2)) &&
  378. reply.charAt(3) == ' '));
  379.  
  380. return reply;
  381. }
  382.  
  383.  
  384. /**
  385. * Added by Julian: Returns the last line of the server reply, but also
  386. * returns the full multi-line reply in a StringBuffer parameter.
  387. */
  388. private String getFullServerReply(StringBuffer fullReply)
  389. throws IOException
  390. {
  391. String reply;
  392. fullReply.setLength(0);
  393.  
  394. do {
  395. reply = inputStream.readLine();
  396. debugPrint(reply);
  397. fullReply.append(reply + lineTerm);
  398. } while(!(Character.isDigit(reply.charAt(0)) &&
  399. Character.isDigit(reply.charAt(1)) &&
  400. Character.isDigit(reply.charAt(2)) &&
  401. reply.charAt(3) == ' '));
  402.  
  403. // remove any trailing line terminators from the fullReply
  404. if (fullReply.length() > 0)
  405. {
  406. fullReply.setLength(fullReply.length() - lineTerm.length());
  407. }
  408.  
  409. return reply;
  410. }
  411.  
  412.  
  413. /**
  414. * Added by Julian: Gets a list of files in the current directory.
  415. */
  416. public String listFiles()
  417. throws IOException
  418. {
  419. return listFiles("");
  420. }
  421.  
  422.  
  423. /**
  424. * Added by Julian: Gets a list of files in either the current
  425. * directory, or one specified as a parameter. The 'params' parameter
  426. * can be either a directory name, a file mask, or both (such as
  427. * '/DirName/*.txt').
  428. */
  429. public String listFiles(String params)
  430. throws IOException
  431. {
  432. StringBuffer files = new StringBuffer();
  433. StringBuffer dirs = new StringBuffer();
  434. if (!getAndParseDirList(params, files, dirs))
  435. {
  436. debugPrint("Error getting file list");
  437. }
  438.  
  439. return files.toString();
  440. }
  441.  
  442.  
  443. /**
  444. * Added by Julian: Gets a list of subdirectories in the current directory.
  445. */
  446. public String listSubdirectories()
  447. throws IOException
  448. {
  449. return listSubdirectories("");
  450. }
  451.  
  452.  
  453. /**
  454. * Added by Julian: Gets a list of subdirectories in either the current
  455. * directory, or one specified as a parameter. The 'params' parameter
  456. * can be either a directory name, a name mask, or both (such as
  457. * '/DirName/Sub*').
  458. */
  459. public String listSubdirectories(String params)
  460. throws IOException
  461. {
  462. StringBuffer files = new StringBuffer();
  463. StringBuffer dirs = new StringBuffer();
  464. if (!getAndParseDirList(params, files, dirs))
  465. {
  466. debugPrint("Error getting dir list");
  467. }
  468.  
  469. return dirs.toString();
  470. }
  471.  
  472.  
  473. /**
  474. * Added by Julian: Sends and gets the results of a file list command,
  475. * like LIST or NLST.
  476. */
  477. private String processFileListCommand(String command)
  478. throws IOException
  479. {
  480. StringBuffer reply = new StringBuffer();
  481. String replyString;
  482.  
  483. // file listings require you to issue a PORT command,
  484. // like a file transfer
  485. boolean success = executeDataCommand(command, reply);
  486. if (!success)
  487. {
  488. return "";
  489. }
  490.  
  491. replyString = reply.toString();
  492. // strip the trailing line terminator from the reply
  493. if (reply.length() > 0)
  494. {
  495. return replyString.substring(0, reply.length() - 1);
  496. } else {
  497. return replyString;
  498. }
  499. }
  500.  
  501.  
  502. /**
  503. * Added by Julian: Gets a directory list from the server and parses
  504. * the elements into a list of files and a list of subdirectories.
  505. */
  506. private boolean getAndParseDirList(String params, StringBuffer files, StringBuffer dirs)
  507. throws IOException
  508. {
  509. // reset the return variables (we're using StringBuffers instead of
  510. // Strings because you can't change a String value and pass it back
  511. // to the calling routine -- changing a String creates a new object)
  512. files.setLength(0);
  513. dirs.setLength(0);
  514.  
  515. // get the NLST and the LIST -- don't worry if the commands
  516. // don't work, because we'll just end up sending nothing back
  517. // if that's the case
  518. String shortList = processFileListCommand("nlst " + params);
  519. String longList = processFileListCommand("list " + params);
  520.  
  521. // tokenize the lists we got, using a newline as a separator
  522. StringTokenizer sList = new StringTokenizer(shortList, "\n");
  523. StringTokenizer lList = new StringTokenizer(longList, "\n");
  524.  
  525. // other variables we'll need
  526. String sString;
  527. String lString;
  528.  
  529. // assume that both lists have the same number of elements
  530. while ((sList.hasMoreTokens()) && (lList.hasMoreTokens())) {
  531. sString = sList.nextToken();
  532. lString = lList.nextToken();
  533.  
  534. if (lString.length() > 0)
  535. {
  536. if (lString.startsWith("d"))
  537. {
  538. dirs.append(sString.trim() + lineTerm);
  539. debugPrint("Dir: " + sString);
  540. } else if (lString.startsWith("-")) {
  541. files.append(sString.trim() + lineTerm);
  542. debugPrint("File: " + sString);
  543. } else {
  544. // actually, symbolic links will start with an "l"
  545. // (lowercase L), but we're not going to mess with
  546. // those
  547. debugPrint("Unknown: " + lString);
  548. }
  549. }
  550. }
  551.  
  552. // strip off any trailing line terminators and return the values
  553. if (files.length() > 0) { files.setLength(files.length() - lineTerm.length()); }
  554. if (dirs.length() > 0) { dirs.setLength(dirs.length() - lineTerm.length()); }
  555.  
  556. return true;
  557. }
  558.  
  559.  
  560. /**
  561. * Executes the given FTP command on our current connection, returning the
  562. * three digit response code from the server. This method only works for
  563. * commands that do not require an additional data port.
  564. */
  565. public int executeCommand(String command)
  566. throws IOException
  567. {
  568. outputStream.println(command);
  569. return getServerReply();
  570. }
  571.  
  572.  
  573. /**
  574. * Executes the given FTP command on our current connection, returning the
  575. * last line of the server's response. Useful for commands that return
  576. * one line of information.
  577. */
  578. public String getExecutionResponse(String command)
  579. throws IOException
  580. {
  581. outputStream.println(command);
  582. return getFullServerReply();
  583. }
  584.  
  585.  
  586. /**
  587. * Executes the given ftpd command on the server and writes the results
  588. * returned on the data port to the file with the given name, returning true
  589. * if the server indicates that the operation was successful.
  590. */
  591. public boolean readDataToFile(String command, String fileName)
  592. throws IOException
  593. {
  594. // Open the local file
  595. RandomAccessFile outfile = new RandomAccessFile(fileName, "rw");
  596.  
  597. // Do restart if desired
  598. if (restartPoint != 0) {
  599. debugPrint("Seeking to " + restartPoint);
  600. outfile.seek(restartPoint);
  601. }
  602.  
  603. // Convert the RandomAccessFile to an OutputStream
  604. FileOutputStream fileStream = new FileOutputStream(outfile.getFD());
  605. boolean success = executeDataCommand(command, fileStream);
  606.  
  607. outfile.close();
  608.  
  609. return success;
  610. }
  611.  
  612.  
  613. /**
  614. * Executes the given ftpd command on the server and writes the contents
  615. * of the given file to the server on an opened data port, returning true
  616. * if the server indicates that the operation was successful.
  617. */
  618. public boolean writeDataFromFile(String command, String fileName)
  619. throws IOException
  620. {
  621. // Open the local file
  622. RandomAccessFile infile = new RandomAccessFile(fileName, "r");
  623.  
  624. // Do restart if desired
  625. if (restartPoint != 0) {
  626. debugPrint("Seeking to " + restartPoint);
  627. infile.seek(restartPoint);
  628. }
  629.  
  630. // Convert the RandomAccessFile to an InputStream
  631. FileInputStream fileStream = new FileInputStream(infile.getFD());
  632. boolean success = executeDataCommand(command, fileStream);
  633.  
  634. infile.close();
  635.  
  636. return success;
  637. }
  638.  
  639.  
  640. /**
  641. * Executes the given ftpd command on the server and writes the results
  642. * returned on the data port to the given OutputStream, returning true
  643. * if the server indicates that the operation was successful.
  644. */
  645. public boolean executeDataCommand(String command, OutputStream out)
  646. throws IOException
  647. {
  648. // Open a data socket on this computer
  649. ServerSocket serverSocket = new ServerSocket(0);
  650. if (!setupDataPort(command, serverSocket)) return false;
  651. Socket clientSocket = serverSocket.accept();
  652.  
  653. // Transfer the data
  654. InputStream in = clientSocket.getInputStream();
  655. transferData(in, out);
  656.  
  657. // Clean up the data structures
  658. in.close();
  659. clientSocket.close();
  660. serverSocket.close();
  661.  
  662. return isPositiveCompleteResponse(getServerReply());
  663. }
  664.  
  665.  
  666. /**
  667. * Executes the given ftpd command on the server and writes the contents
  668. * of the given InputStream to the server on an opened data port, returning
  669. * true if the server indicates that the operation was successful.
  670. */
  671. public boolean executeDataCommand(String command, InputStream in)
  672. throws IOException
  673. {
  674. // Open a data socket on this computer
  675. ServerSocket serverSocket = new ServerSocket(0);
  676. if (!setupDataPort(command, serverSocket)) return false;
  677. Socket clientSocket = serverSocket.accept();
  678.  
  679. // Transfer the data
  680. OutputStream out = clientSocket.getOutputStream();
  681. transferData(in, out);
  682.  
  683. // Clean up the data structures
  684. out.close();
  685. clientSocket.close();
  686. serverSocket.close();
  687.  
  688. return isPositiveCompleteResponse(getServerReply());
  689. }
  690.  
  691.  
  692. /**
  693. * Added by Julian: Executes the given ftpd command on the server
  694. * and writes the results returned on the data port to the given
  695. * StringBuffer, returning true if the server indicates that the
  696. * operation was successful.
  697. */
  698. public boolean executeDataCommand(String command, StringBuffer sb)
  699. throws IOException
  700. {
  701. // Open a data socket on this computer
  702. ServerSocket serverSocket = new ServerSocket(0);
  703. if (!setupDataPort(command, serverSocket)) return false;
  704. Socket clientSocket = serverSocket.accept();
  705.  
  706. // Transfer the data
  707. InputStream in = clientSocket.getInputStream();
  708. transferData(in, sb);
  709.  
  710. // Clean up the data structures
  711. in.close();
  712. clientSocket.close();
  713. serverSocket.close();
  714.  
  715. return isPositiveCompleteResponse(getServerReply());
  716. }
  717.  
  718.  
  719. /**
  720. * Transfers the data from the given input stream to the given output
  721. * stream until we reach the end of the stream.
  722. */
  723. private void transferData(InputStream in, OutputStream out)
  724. throws IOException
  725. {
  726. byte b[] = new byte[BLOCK_SIZE];
  727. int amount;
  728.  
  729. // Read the data into the file
  730. while ((amount = in.read(b)) > 0) {
  731. out.write(b, 0, amount);
  732. }
  733. }
  734.  
  735.  
  736. /**
  737. * Added by Julian: Transfers the data from the given input stream
  738. * to the given StringBuffer until we reach the end of the stream.
  739. */
  740. private void transferData(InputStream in, StringBuffer sb)
  741. throws IOException
  742. {
  743. byte b[] = new byte[BLOCK_SIZE];
  744. int amount;
  745.  
  746. // Read the data into the StringBuffer
  747. while ((amount = in.read(b)) > 0) {
  748. sb.append(new String(b, 0, amount));
  749. }
  750. }
  751.  
  752.  
  753. /**
  754. * Executes the given ftpd command on the server and writes the results
  755. * returned on the data port to the given FilterOutputStream, returning true
  756. * if the server indicates that the operation was successful.
  757. */
  758. private boolean setupDataPort(String command, ServerSocket serverSocket)
  759. throws IOException
  760. {
  761. // Send our local data port to the server
  762. if (!openPort(serverSocket)) return false;
  763.  
  764. // Set binary type transfer
  765. outputStream.println("type i");
  766. if (!isPositiveCompleteResponse(getServerReply())) {
  767. debugPrint("Could not set transfer type");
  768. return false;
  769. }
  770.  
  771. // If we have a restart point, send that information
  772. if (restartPoint != 0) {
  773. outputStream.println("rest " + restartPoint);
  774. restartPoint = 0;
  775. // TODO: Interpret server response here
  776. getServerReply();
  777. }
  778.  
  779. // Send the command
  780. outputStream.println(command);
  781.  
  782. return isPositivePreliminaryResponse(getServerReply());
  783. }
  784.  
  785.  
  786. /**
  787. * Get IP address and port number from serverSocket and send them via the
  788. * <code>port</code> command to the ftp server, returning true if we get a
  789. * valid response from the server, returning true if the server indicates
  790. * that the operation was successful.
  791. */
  792. private boolean openPort(ServerSocket serverSocket)
  793. throws IOException
  794. {
  795. int localport = serverSocket.getLocalPort();
  796.  
  797. // get local ip address
  798. InetAddress inetaddress = serverSocket.getInetAddress();
  799. InetAddress localip;
  800. try {
  801. localip = inetaddress.getLocalHost();
  802. } catch(UnknownHostException e) {
  803. debugPrint("Can't get local host");
  804. return false;
  805. }
  806.  
  807. // get ip address in high byte order
  808. byte[] addrbytes = localip.getAddress();
  809.  
  810. // tell server what port we are listening on
  811. short addrshorts[] = new short[4];
  812.  
  813. // problem: bytes greater than 127 are printed as negative numbers
  814. for(int i = 0; i <= 3; i++) {
  815. addrshorts[i] = addrbytes[i];
  816. if (addrshorts[i] < 0)
  817. addrshorts[i] += 256;
  818. }
  819.  
  820. outputStream.println("port " + addrshorts[0] + "," + addrshorts[1] +
  821. "," + addrshorts[2] + "," + addrshorts[3] + "," +
  822. ((localport & 0xff00) >> 8) + "," +
  823. (localport & 0x00ff));
  824.  
  825. return isPositiveCompleteResponse(getServerReply());
  826. }
  827.  
  828.  
  829. /**
  830. * True if the given response code is in the 100-199 range.
  831. */
  832. private boolean isPositivePreliminaryResponse(int response)
  833. {
  834. return (response >= 100 && response < 200);
  835. }
  836.  
  837.  
  838. /**
  839. * True if the given response code is in the 300-399 range.
  840. */
  841. private boolean isPositiveIntermediateResponse(int response)
  842. {
  843. return (response >= 300 && response < 400);
  844. }
  845.  
  846. /**
  847. * True if the given response code is in the 200-299 range.
  848. */
  849. private boolean isPositiveCompleteResponse(int response)
  850. {
  851. return (response >= 200 && response < 300);
  852. }
  853.  
  854.  
  855. /**
  856. * True if the given response code is in the 400-499 range.
  857. */
  858. private boolean isTransientNegativeResponse(int response)
  859. {
  860. return (response >= 400 && response < 500);
  861. }
  862.  
  863.  
  864. /**
  865. * True if the given response code is in the 500-599 range.
  866. */
  867. private boolean isPermanentNegativeResponse(int response)
  868. {
  869. return (response >= 500 && response < 600);
  870. }
  871.  
  872.  
  873. /**
  874. * Eliminates the response code at the beginning of the response string.
  875. */
  876. private String excludeCode(String response)
  877. {
  878. if (response.length() < 5) return response;
  879. return response.substring(4);
  880. }
  881.  
  882. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement