Skip to content

Commit eeec49e

Browse files
committed
Minor performance improvements
This reduces duplicate calls to things like .trim() and .indexOf. It also improves condition checking slightly and includes a threadly version bump.
1 parent e5e49a6 commit eeec49e

File tree

6 files changed

+25
-25
lines changed

6 files changed

+25
-25
lines changed

gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
group = org.threadly
2-
version = 0.7
3-
threadlyVersion = 5.1
2+
version = 0.8
3+
threadlyVersion = 5.3
44
litesocketsVersion = 4.0
55
org.gradle.parallel=true

protocol/src/main/java/org/threadly/litesockets/protocols/http/request/HTTPRequest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,4 @@ public HTTPRequestBuilder makeBuilder() {
6969
HTTPRequestBuilder hrb = new HTTPRequestBuilder().replaceHTTPHeaders(headers).setHTTPRequestHeader(request);
7070
return hrb;
7171
}
72-
7372
}

protocol/src/main/java/org/threadly/litesockets/protocols/http/request/HTTPRequestBuilder.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,3 @@ public HTTPRequest build() {
148148
return new HTTPRequest(request, new HTTPHeaders(headers));
149149
}
150150
}
151-

protocol/src/main/java/org/threadly/litesockets/protocols/http/request/HTTPRequestHeader.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@ public class HTTPRequestHeader {
2222

2323
public HTTPRequestHeader(final String requestHeader) {
2424
this.rawRequest = requestHeader.trim();
25-
String[] tmp = requestHeader.trim().split(" ");
25+
String[] tmp = rawRequest.split(" ");
2626
if(tmp.length != REQUIRED_REQUEST_ITEMS) {
2727
throw new IllegalArgumentException("HTTPRequestHeader can only have 3 arguments! :"+requestHeader);
2828
}
2929
requestType = tmp[0].trim().toUpperCase();
3030
String ptmp = tmp[1].trim();
31-
if(ptmp.indexOf("?") >= 0) {
32-
int pos = tmp[1].indexOf("?");
33-
requestPath = ptmp.substring(0, pos);
34-
requestQuery = HTTPUtils.queryToMap(ptmp.substring(pos+1));
31+
int queryParamPos = ptmp.indexOf('?');
32+
if(queryParamPos >= 0) {
33+
requestPath = ptmp.substring(0, queryParamPos);
34+
requestQuery = HTTPUtils.queryToMap(ptmp.substring(queryParamPos+1));
3535
} else {
3636
requestPath = ptmp;
37-
requestQuery = HTTPUtils.queryToMap("");
37+
requestQuery = Collections.emptyMap();
3838
}
3939

4040
httpVersion = tmp[2].trim().toUpperCase();
@@ -50,31 +50,31 @@ public HTTPRequestHeader(HTTPRequestType requestType, String requestPath, Map<St
5050
public HTTPRequestHeader(String requestType, String requestPath, Map<String, String> requestQuery, String httpVersion){
5151
this.requestType = requestType;
5252
final HashMap<String, String> rqm = new HashMap<>();
53-
if(requestPath.contains("?")) {
54-
int pos = requestPath.indexOf("?");
55-
this.requestPath = requestPath.substring(0, pos);
56-
rqm.putAll(HTTPUtils.queryToMap(requestPath.substring(pos+1)));
53+
int queryParamPos = requestPath.indexOf("?");
54+
if(queryParamPos >= 0) {
55+
this.requestPath = requestPath.substring(0, queryParamPos);
56+
rqm.putAll(HTTPUtils.queryToMap(requestPath.substring(queryParamPos+1)));
5757
} else {
5858
this.requestPath = requestPath;
5959
}
6060
if(requestQuery != null) {
6161
rqm.putAll(requestQuery);
6262
}
6363
this.requestQuery = Collections.unmodifiableMap(rqm);
64+
if(!HTTPConstants.HTTP_VERSION_1_1.equals(httpVersion) && !HTTPConstants.HTTP_VERSION_1_0.equals(httpVersion)) {
65+
throw new UnsupportedOperationException("Unknown HTTP Version!:"+httpVersion);
66+
}
6467
this.httpVersion = httpVersion.trim().toUpperCase();
6568
StringBuilder sb = new StringBuilder();
6669
sb.append(requestType.toString());
6770
sb.append(HTTPConstants.SPACE);
6871
sb.append(requestPath);
69-
if(requestQuery != null && requestQuery.size() > 0) {
72+
if(requestQuery != null && ! requestQuery.isEmpty()) {
7073
sb.append(HTTPUtils.queryToString(requestQuery));
7174
}
7275
sb.append(HTTPConstants.SPACE);
7376
sb.append(this.httpVersion);
7477
rawRequest = sb.toString();
75-
if(!httpVersion.equals(HTTPConstants.HTTP_VERSION_1_1) && !httpVersion.equals(HTTPConstants.HTTP_VERSION_1_0)) {
76-
throw new UnsupportedOperationException("Unknown HTTP Version!:"+httpVersion);
77-
}
7878
}
7979

8080
public String getRequestType() {
@@ -113,13 +113,14 @@ public int hashCode() {
113113

114114
@Override
115115
public boolean equals(Object o) {
116-
if(o instanceof HTTPRequestHeader) {
116+
if(o == this) {
117+
return true;
118+
} else if(o instanceof HTTPRequestHeader) {
117119
HTTPRequestHeader hrh = (HTTPRequestHeader)o;
118120
if(hrh.toString().equals(this.toString())) {
119121
return true;
120122
}
121123
}
122124
return false;
123125
}
124-
125126
}

protocol/src/main/java/org/threadly/litesockets/protocols/http/response/HTTPResponseHeader.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class HTTPResponseHeader {
1616

1717
public HTTPResponseHeader(String stringResponse) {
1818
this.rawResponse = stringResponse.trim();
19-
String[] tmp = stringResponse.trim().split(" ", MAX_RESPONSE_ITEMS);
19+
String[] tmp = rawResponse.split(" ", MAX_RESPONSE_ITEMS);
2020
try {
2121
httpVersion = tmp[0].trim();
2222
if(!httpVersion.equalsIgnoreCase(HTTPConstants.HTTP_VERSION_1_1) && !httpVersion.equalsIgnoreCase(HTTPConstants.HTTP_VERSION_1_0)) {
@@ -72,6 +72,4 @@ public boolean equals(Object o) {
7272
public String toString() {
7373
return rawResponse;
7474
}
75-
76-
7775
}

protocol/src/main/java/org/threadly/litesockets/protocols/http/shared/HTTPHeaders.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@ public HTTPHeaders(String headerString) {
2626
this.rawHeaders = headerString;
2727
}
2828

29-
String[] rows = headerString.trim().split(HTTPConstants.HTTP_NEWLINE_DELIMINATOR);
29+
String[] rows = headerString.split(HTTPConstants.HTTP_NEWLINE_DELIMINATOR);
3030
for(String h: rows) {
3131
int delim = h.indexOf(':');
32-
map.put(h.substring(0, delim), h.substring(delim + 1).trim());
32+
if (delim < 0) {
33+
throw new IllegalArgumentException("Header is missing key value delim: " + h);
34+
}
35+
map.put(h.substring(0, delim).trim(), h.substring(delim + 1).trim());
3336
}
3437

3538
headers = Collections.unmodifiableMap(map);

0 commit comments

Comments
 (0)