-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtils.java
125 lines (114 loc) · 3.68 KB
/
Utils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by linda on 2/3/2017.
*/
public class Utils {
static String ip_addr;
static int port_number;
static String response_code = "0";
/*
check if the current response is the last response
argv: server response
return true if it's not, false otherwise
*/
public static Boolean notlastline(String response){
Matcher matcher = getMatch("(\\d\\d\\d)", response);
if(!matcher.find())
return true;
else
response_code = matcher.group(0);
if(getMatch("(-)", response).find())
return true;
else
return false;
}
/*
argv: server response
return "142,103,6,49,227,166,227" part of the passive mode response
*/
public static String IPandPort(String response){
Matcher matcher = getMatch("\\((.*?)\\)", response);
if(matcher.find()){
return matcher.group(1);
}
else return null;
}
/*
regular expression helper function
*/
private static Matcher getMatch(String pattern, String response){
Pattern apattern = Pattern.compile(pattern);
return apattern.matcher(response);
}
/*
parse ip from pasv mode response
return ip address for the second data connection
*/
public static String getIP(String[] info){
String ip = "";
for(int i = 0; i < 3; i++){
ip += info[i] + ".";
}
ip_addr = ip + info[3];
return ip_addr;
}
/*
parse port from pasv mode response
return port number
*/
public static int getPort(String[] info){
port_number = Integer.parseInt(info[4])*256 + Integer.parseInt(info[5]);
return port_number;
}
/*
return true when the number of arguments provided by the user is correct, false otherwise
*/
public static boolean argumentChecker(String userInput){
String[] inputs = userInput.split(" ");
int length = inputs.length;
String command = inputs[0];
boolean result = false;
switch(command){
case "dir":
case "quit":
case "features":
result = (length == 1);
return result;
case "user":
case "pw":
case "get":
case "cd":
result = (length == 2);
return result;
}
return result;
}
public static void errorMessage(String code){
switch(code){
case "001":
System.out.println("0x001 Invalid command.");
break;
case "002":
System.out.println("0x002 Incorrect number of arguments.");
break;
case "FFFC":
System.out.println("0xFFFC Control connection to " + ip_addr + " on port" + port_number + " failed to open.");
case "FFFD ":
System.out.println("0xFFFD Control connection I/O error, closing control connection.");
break;
case "3A2 ":
System.out.println("0x3A2 Data transfer connection to " + ip_addr + " on port" + port_number + " failed to open.");
case "3A7 ":
System.out.println("0x3A7 Data transfer connection I/O error, closing data connection.");
break;
case "FFFE ":
System.out.println("0xFFFE Input error while reading commands, terminating.");
System.exit(0);
}
}
public static void setIPandPort(String ip, int port){
ip_addr = ip;
port_number = port;
}
}