Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 682dbf5

Browse files
Use char instead of String for PIPE_TYPE_* constants, so we can make optimization for heavy servers
1 parent 85377f9 commit 682dbf5

File tree

3 files changed

+32
-28
lines changed

3 files changed

+32
-28
lines changed

sources/net.sf.j2s.ajax/ajaxpipe/net/sf/j2s/ajax/SimplePipeHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ synchronized static String registerPipe(SimplePipeRunnable pipe) {
118118
*/
119119
@J2SIgnore
120120
static String nextPipeKey() {
121-
StringBuffer buf = new StringBuffer();
121+
StringBuffer buf = new StringBuffer(SimplePipeRequest.PIPE_KEY_LENGTH);
122122
for (int i = 0; i < SimplePipeRequest.PIPE_KEY_LENGTH; i++) {
123123
int r = (int) Math.round((float) Math.random() * 61); // 0..61, total 62 numbers
124124
if (r < 10) {

sources/net.sf.j2s.ajax/ajaxpipe/net/sf/j2s/ajax/SimplePipeHttpServlet.java

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,10 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
112112
resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
113113
return;
114114
}
115-
String type = req.getParameter(SimplePipeRequest.FORM_PIPE_TYPE);
116-
if (type == null) {
117-
type = SimplePipeRequest.PIPE_TYPE_CONTINUUM;
115+
char type = SimplePipeRequest.PIPE_TYPE_CONTINUUM;
116+
String typeStr = req.getParameter(SimplePipeRequest.FORM_PIPE_TYPE);
117+
if (typeStr != null && typeStr.length() > 0) {
118+
type = typeStr.charAt(0);
118119
}
119120
String domain = req.getParameter(SimplePipeRequest.FORM_PIPE_DOMAIN);
120121
doPipe(resp, key, type, domain);
@@ -139,14 +140,14 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
139140
* type = notify
140141
* Notify that client (browser) still keeps the pipe connection.
141142
*/
142-
protected void doPipe(final HttpServletResponse resp, String key, String type, String domain)
143+
protected void doPipe(final HttpServletResponse resp, String key, char type, String domain)
143144
throws IOException {
144145
PrintWriter writer = null;
145146
resp.setHeader("Pragma", "no-cache");
146147
resp.setHeader("Cache-Control", "no-cache");
147148
resp.setDateHeader("Expires", 0);
148149

149-
if (SimplePipeRequest.PIPE_TYPE_NOTIFY.equals(type)) {
150+
if (SimplePipeRequest.PIPE_TYPE_NOTIFY == type) {
150151
/*
151152
* Client send in "notify" request to execute #notifyPipeStatus, see below comments
152153
*/
@@ -160,7 +161,7 @@ protected void doPipe(final HttpServletResponse resp, String key, String type, S
160161
writer.write("\");");
161162
return;
162163
}
163-
if (SimplePipeRequest.PIPE_TYPE_SUBDOMAIN_QUERY.equals(type)) { // subdomain query
164+
if (SimplePipeRequest.PIPE_TYPE_SUBDOMAIN_QUERY == type) { // subdomain query
164165
resp.setContentType("text/html; charset=utf-8");
165166
writer = resp.getWriter();
166167
StringBuffer buffer = new StringBuffer();
@@ -189,11 +190,11 @@ protected void doPipe(final HttpServletResponse resp, String key, String type, S
189190
writer.write(buffer.toString());
190191
return;
191192
}
192-
boolean isContinuum = SimplePipeRequest.PIPE_TYPE_CONTINUUM.equals(type);
193+
boolean isContinuum = SimplePipeRequest.PIPE_TYPE_CONTINUUM == type;
193194
if (isContinuum) {
194195
resp.setHeader("Transfer-Encoding", "chunked");
195196
}
196-
boolean isScripting = SimplePipeRequest.PIPE_TYPE_SCRIPT.equals(type);
197+
boolean isScripting = SimplePipeRequest.PIPE_TYPE_SCRIPT == type;
197198
if (isScripting) { // iframe
198199
resp.setContentType("text/html; charset=utf-8");
199200
writer = resp.getWriter();
@@ -211,8 +212,7 @@ protected void doPipe(final HttpServletResponse resp, String key, String type, S
211212
writer.write(buffer.toString());
212213
writer.flush();
213214
} else {
214-
if (SimplePipeRequest.PIPE_TYPE_QUERY.equals(type)
215-
|| isContinuum) {
215+
if (SimplePipeRequest.PIPE_TYPE_QUERY == type || isContinuum) {
216216
resp.setContentType("text/plain; charset=utf-8");
217217
} else {
218218
resp.setContentType("text/javascript; charset=utf-8");
@@ -345,28 +345,27 @@ protected void doPipe(final HttpServletResponse resp, String key, String type, S
345345
}
346346
}
347347

348-
protected static String output(String type, String key,
349-
String str) {
348+
protected static String output(char type, String key, String str) {
350349
StringBuffer buffer = new StringBuffer();
351-
if (SimplePipeRequest.PIPE_TYPE_SCRIPT.equals(type)) {
350+
if (SimplePipeRequest.PIPE_TYPE_SCRIPT == type) {
352351
// iframe, so $ is a safe method identifier
353352
buffer.append("<script type=\"text/javascript\">$ (\"");
354-
} else if (SimplePipeRequest.PIPE_TYPE_XSS.equals(type)) {
353+
} else if (SimplePipeRequest.PIPE_TYPE_XSS == type) {
355354
buffer.append("$p1p3p$ (\""); // $p1p3p$
356355
}
357356
buffer.append(key);
358-
if (SimplePipeRequest.PIPE_TYPE_SCRIPT.equals(type)
359-
|| SimplePipeRequest.PIPE_TYPE_XSS.equals(type)) {
357+
if (SimplePipeRequest.PIPE_TYPE_SCRIPT == type
358+
|| SimplePipeRequest.PIPE_TYPE_XSS == type) {
360359
str = str.replaceAll("\\\\", "\\\\\\\\").replaceAll("\r", "\\\\r")
361360
.replaceAll("\n", "\\\\n").replaceAll("\"", "\\\\\"");
362-
if (SimplePipeRequest.PIPE_TYPE_SCRIPT.equals(type)) {
361+
if (SimplePipeRequest.PIPE_TYPE_SCRIPT == type) {
363362
str = str.replaceAll("<\\/script>", "<\\/scr\" + \"ipt>");
364363
}
365364
}
366365
buffer.append(str);
367-
if (SimplePipeRequest.PIPE_TYPE_SCRIPT.equals(type)) { // iframe
366+
if (SimplePipeRequest.PIPE_TYPE_SCRIPT == type) { // iframe
368367
buffer.append("\");</script>\r\n");
369-
} else if (SimplePipeRequest.PIPE_TYPE_XSS.equals(type)) {
368+
} else if (SimplePipeRequest.PIPE_TYPE_XSS == type) {
370369
buffer.append("\");\r\n");
371370
}
372371
return buffer.toString();

sources/net.sf.j2s.ajax/ajaxpipe/net/sf/j2s/ajax/SimplePipeRequest.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,32 +79,32 @@ public static interface IHttpPipeRequestFactory extends IHttpRequestFactory {
7979
/**
8080
* Type of pipe request: query
8181
*/
82-
public static final String PIPE_TYPE_QUERY = "q"; // "query";
82+
public static final char PIPE_TYPE_QUERY = 'q';
8383

8484
/**
8585
* Type of pipe request: subdomain-query
8686
*/
87-
public static final String PIPE_TYPE_SUBDOMAIN_QUERY = "u"; // "subdomain-query";
87+
public static final char PIPE_TYPE_SUBDOMAIN_QUERY = 'u';
8888

8989
/**
9090
* Type of pipe request: notify
9191
*/
92-
public static final String PIPE_TYPE_NOTIFY = "n"; // "notify";
92+
public static final char PIPE_TYPE_NOTIFY = 'n';
9393

9494
/**
9595
* Type of pipe request: script
9696
*/
97-
public static final String PIPE_TYPE_SCRIPT = "s"; // "script";
97+
public static final char PIPE_TYPE_SCRIPT = 's';
9898

9999
/**
100100
* Type of pipe request: xss
101101
*/
102-
public static final String PIPE_TYPE_XSS = "x"; // "xss";
102+
public static final char PIPE_TYPE_XSS = 'x';
103103

104104
/**
105105
* Type of pipe request: continuum
106106
*/
107-
public static final String PIPE_TYPE_CONTINUUM = "c"; // "continuum";
107+
public static final char PIPE_TYPE_CONTINUUM = 'c';
108108

109109

110110
/**
@@ -191,12 +191,17 @@ public static void switchToContinuumMode(boolean queue) {
191191
* @param pipeRequestType
192192
* @return request data for both GET and POST request.
193193
*/
194-
protected static String constructRequest(String pipeKey, String pipeRequestType, long pipeSequence) {
194+
protected static String constructRequest(String pipeKey, char pipeRequestType, long pipeSequence) {
195195
reqCount++;
196-
return FORM_PIPE_KEY + "=" + pipeKey + "&"
196+
/**
197+
* @j2sNative
198+
* return"k="+pipeKey+"&t="+pipeRequestType+"&s="+pipeSequence+"&r="+net.sf.j2s.ajax.SimplePipeRequest.reqCount;
199+
*/ {
200+
return FORM_PIPE_KEY + "=" + pipeKey + "&"
197201
+ FORM_PIPE_TYPE + "=" + pipeRequestType + "&"
198202
+ FORM_PIPE_SEQUENCE + "=" + pipeSequence + "&"
199203
+ FORM_PIPE_RANDOM + "=" + reqCount;
204+
}
200205
}
201206

202207
protected static void sendRequest(HttpRequest request, String method, String url,

0 commit comments

Comments
 (0)