今天看sun的HttpMessages.java文件,雖然文件很小,但是對我的啓發很大。
前面定義了大量的HTTP常量:
- ...
- ...
- private static final String STATUS_305 = "Use Proxy";
- private static final String STATUS_307 = "Temporary Redirect";
- private static final String STATUS_400 = "Bad Request";
- private static final String STATUS_401 = "Unauthorized";
- private static final String STATUS_402 = "Payment Required";
- private static final String STATUS_403 = "Forbidden";
- private static final String STATUS_404 = "Not Found";
- private static final String STATUS_405 = "Method Not Allowed";
- private static final String STATUS_406 = "Not Acceptable";
- private static final String STATUS_407 = "Proxy Authentication Required";
- private static final String STATUS_408 = "Request Timeout";
- ...
- ...
...
...
private static final String STATUS_305 = "Use Proxy";
private static final String STATUS_307 = "Temporary Redirect";
private static final String STATUS_400 = "Bad Request";
private static final String STATUS_401 = "Unauthorized";
private static final String STATUS_402 = "Payment Required";
private static final String STATUS_403 = "Forbidden";
private static final String STATUS_404 = "Not Found";
private static final String STATUS_405 = "Method Not Allowed";
private static final String STATUS_406 = "Not Acceptable";
private static final String STATUS_407 = "Proxy Authentication Required";
private static final String STATUS_408 = "Request Timeout";
...
...
也可以趁機從這裏查看HTTP返回值的意義^_^。然後定義了一個java.util.concurrent.ConcurrentHashMap的變量httpStatusCodeMappings,再往裏面添加剛纔定義的常量(這個操作放在了一個靜態塊裏),如:
- static {
- httpStatusCodeMappings.put("sc.100", STATUS_100);
- httpStatusCodeMappings.put("sc.101", STATUS_101);
- httpStatusCodeMappings.put("sc.200", STATUS_200);
- httpStatusCodeMappings.put("sc.201", STATUS_201);
- httpStatusCodeMappings.put("sc.202", STATUS_202);
- httpStatusCodeMappings.put("sc.203", STATUS_203);
- ...
- ...
static {
httpStatusCodeMappings.put("sc.100", STATUS_100);
httpStatusCodeMappings.put("sc.101", STATUS_101);
httpStatusCodeMappings.put("sc.200", STATUS_200);
httpStatusCodeMappings.put("sc.201", STATUS_201);
httpStatusCodeMappings.put("sc.202", STATUS_202);
httpStatusCodeMappings.put("sc.203", STATUS_203);
...
...
最重要的就是這個方法了,先看代碼:
- public static String getMessage(int status) {
- // Return the status message for the most frequently used status
- // codes directly, without any lookup
- switch (status) {
- case 200: return STATUS_200;
- case 302: return STATUS_302;
- case 400: return STATUS_400;
- case 404: return STATUS_404;
- }
- return httpStatusCodeMappings.get("sc."+ status);
- }
public static String getMessage(int status) {
// Return the status message for the most frequently used status
// codes directly, without any lookup
switch (status) {
case 200: return STATUS_200;
case 302: return STATUS_302;
case 400: return STATUS_400;
case 404: return STATUS_404;
}
return httpStatusCodeMappings.get("sc."+ status);
}
這段代碼的用途很簡單,就是要返回狀態碼對應的Message,而這些這些消息都已經存放在httpStatusCodeMappings變量裏了,爲了提高訪問常用的幾個返回碼的Message,它直接用了一個靜態塊,而不去lookup那個map了,這就是高手!