-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.java
More file actions
64 lines (56 loc) · 2.24 KB
/
Utils.java
File metadata and controls
64 lines (56 loc) · 2.24 KB
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
/**
* Utils Class
*
* A class containing constants and methods used by both the server and workers.
*/
import java.io.Closeable;
import java.io.IOException;
public class Utils {
// numbers follow standard Java convention
public static final int SUCCESSFUL_TERMINATION = 0;
public static final int UNSUCCESSFUL_TERMINATION = -1;
// I/O constants
public static final int BUFFER_SIZE = 4096;
public static final int EOF = -1;
public static final int NO_BYTE = -1;
public static final int OFFSET = 0;
// request constants
public static final String DEFAULT_PATH = "/";
public static final String DEFAULT_LOCATION = "/index.html";
// response constants
public static final String STRING_TO_BYTE_CHARSET = "US-ASCII";
public static final int TIMEOUT_CODE = 408;
public static final String TIMEOUT_PHRASE = "Request Timeout";
public static final int BAD_CODE = 400;
public static final String BAD_PHRASE = "Bad Request";
public static final int NOT_FOUND_CODE = 404;
public static final String NOT_FOUND_PHRASE = "Not Found";
public static final int OK_CODE = 200;
public static final String OK_PHRASE = "OK";
public static final String HTTP_VERSION = "HTTP/1.1";
public static final String HTTP_METHOD = "GET";
public static final String EOL = "\r\n";
public static final String END_OF_HEADERS = EOL;
/**
* Close all opened streams, sockets, and other resources before terminating the program.
*
* @param resources all resources which need to be closed
*/
public static void closeGracefully(Closeable... resources) {
/*
* We need to surround this with a try-catch block because the closing itself can raise
* an IOException. In this case, if closing fails, there is nothing else we can do. We must also
* ensure the resource is not null. This is because other parts of the program instantiate certain
* resources to null before reassignment.
*/
try {
for (Closeable resource : resources) {
if (resource != null) {
resource.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}