Skip to content

Commit 8ca4918

Browse files
author
zhourenjian@gmail.com
committed
Use Charset object instead of charset string and upper case UTF-8 to avoid a Java bottleneck
1 parent 78f29bc commit 8ca4918

File tree

6 files changed

+50
-37
lines changed

6 files changed

+50
-37
lines changed

sources/net.sf.j2s.ajax/ajaxcore/net/sf/j2s/ajax/HttpRequest.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.UnsupportedEncodingException;
2121
import java.net.HttpURLConnection;
2222
import java.net.URL;
23+
import java.nio.charset.Charset;
2324
import java.util.Iterator;
2425
import java.util.List;
2526
import java.util.Map;
@@ -137,6 +138,9 @@ public static interface IXHRReceiving {
137138

138139
public static String DEFAULT_USER_AGENT = "Java2Script/2.0.2";
139140

141+
protected static Charset UTF_8 = Charset.forName("UTF-8");
142+
protected static Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
143+
140144
protected int status;
141145
protected String statusText;
142146
protected int readyState;
@@ -220,18 +224,21 @@ public String getResponseText() {
220224
}
221225
}
222226
if (charset != null) {
223-
try {
224-
responseText = baos.toString(charset);
225-
} catch (UnsupportedEncodingException e) {
227+
if ("UTF-8".equalsIgnoreCase(charset)) {
228+
responseText = new String (baos.toByteArray(), UTF_8);
229+
} else if ("ISO-8859-1".equalsIgnoreCase(charset)) {
230+
responseText = new String (baos.toByteArray(), ISO_8859_1);
231+
} else {
232+
try {
233+
responseText = baos.toString(charset);
234+
} catch (UnsupportedEncodingException e) {
235+
responseText = baos.toString();
236+
}
226237
}
227238
}
228239
}
229240
if (responseText == null) {
230-
try {
231-
responseText = baos.toString("iso-8859-1");
232-
} catch (UnsupportedEncodingException e) {
233-
responseText = baos.toString();
234-
}
241+
responseText = new String (baos.toByteArray(), ISO_8859_1);
235242
}
236243
return responseText;
237244
}
@@ -264,7 +271,7 @@ public Document getResponseXML() {
264271
dbf.setAttribute("http://xml.org/sax/features/namespaces", Boolean.TRUE);
265272
try {
266273
DocumentBuilder db = dbf.newDocumentBuilder();
267-
ByteArrayInputStream biStream = new ByteArrayInputStream(responseContent.getBytes("utf-8"));
274+
ByteArrayInputStream biStream = new ByteArrayInputStream(responseContent.getBytes(UTF_8));
268275
responseXML = db.parse(biStream);
269276
} catch (Exception e) {
270277
e.printStackTrace();

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ protected void doPipe(final HttpServletResponse resp, String key, char type, Str
152152
* Client send in "notify" request to execute #notifyPipeStatus, see below comments
153153
*/
154154
boolean updated = SimplePipeHelper.notifyPipeStatus(key, true); // update it!
155-
resp.setContentType("text/javascript; charset=utf-8");
155+
resp.setContentType("text/javascript; charset=UTF-8");
156156
writer = resp.getWriter();
157157
writer.write("$p1p3b$ (\""); // $p1p3b$ = net.sf.j2s.ajax.SimplePipeRequest.pipeNotifyCallBack
158158
writer.write(key);
@@ -162,7 +162,7 @@ protected void doPipe(final HttpServletResponse resp, String key, char type, Str
162162
return;
163163
}
164164
if (SimplePipeRequest.PIPE_TYPE_SUBDOMAIN_QUERY == type) { // subdomain query
165-
resp.setContentType("text/html; charset=utf-8");
165+
resp.setContentType("text/html; charset=UTF-8");
166166
writer = resp.getWriter();
167167
StringBuilder builder = new StringBuilder();
168168
builder.append("<html><head><title></title></head><body>\r\n");
@@ -196,7 +196,7 @@ protected void doPipe(final HttpServletResponse resp, String key, char type, Str
196196
}
197197
boolean isScripting = SimplePipeRequest.PIPE_TYPE_SCRIPT == type;
198198
if (isScripting) { // iframe
199-
resp.setContentType("text/html; charset=utf-8");
199+
resp.setContentType("text/html; charset=UTF-8");
200200
writer = resp.getWriter();
201201
StringBuilder builder = new StringBuilder();
202202
builder.append("<html><head><title></title></head><body>\r\n");
@@ -213,9 +213,9 @@ protected void doPipe(final HttpServletResponse resp, String key, char type, Str
213213
writer.flush();
214214
} else {
215215
if (SimplePipeRequest.PIPE_TYPE_QUERY == type || isContinuum) {
216-
resp.setContentType("text/plain; charset=utf-8");
216+
resp.setContentType("text/plain; charset=UTF-8");
217217
} else {
218-
resp.setContentType("text/javascript; charset=utf-8");
218+
resp.setContentType("text/javascript; charset=UTF-8");
219219
}
220220
writer = resp.getWriter();
221221
}

sources/net.sf.j2s.ajax/ajaxrpc/net/sf/j2s/ajax/SimpleRPCHttpServlet.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,8 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp)
264264
resp.setHeader("Pragma", "no-cache");
265265
resp.setHeader("Cache-Control", "no-cache");
266266
resp.setDateHeader("Expires", 0);
267-
resp.setContentType("text/plain; charset=utf-8");
268-
//resp.setCharacterEncoding("utf-8");
267+
resp.setContentType("text/plain; charset=UTF-8");
268+
//resp.setCharacterEncoding("UTF-8");
269269
PrintWriter writer = resp.getWriter();
270270
SimpleRPCRunnable clonedRunnable = null;
271271
try {
@@ -370,8 +370,8 @@ public boolean ignoreDefaultFields() {
370370
resp.setDateHeader("Expires", 0);
371371

372372
if (isScriptReuest) { // cross site script response
373-
resp.setContentType("text/javascript; charset=utf-8");
374-
//resp.setCharacterEncoding("utf-8");
373+
resp.setContentType("text/javascript; charset=UTF-8");
374+
//resp.setCharacterEncoding("UTF-8");
375375
PrintWriter writer = resp.getWriter();
376376
writer.write("net.sf.j2s.ajax.SimpleRPCRequest.xssNotify(");
377377
writer.write("\"" + requestID + "\", \"");
@@ -385,8 +385,8 @@ public boolean ignoreDefaultFields() {
385385
}
386386

387387
// normal text response
388-
resp.setContentType("text/plain; charset=utf-8");
389-
//resp.setCharacterEncoding("utf-8");
388+
resp.setContentType("text/plain; charset=UTF-8");
389+
//resp.setCharacterEncoding("UTF-8");
390390
PrintWriter writer = resp.getWriter();
391391
writer.write(serialize);
392392
runnable.ajaxOut();
@@ -404,7 +404,7 @@ private String prepareScriptRequest(HttpServletRequest req, HttpServletResponse
404404
// make sure that servlet support cross site script request
405405
if (!supportXSSRequest()) {
406406
resp.setContentType("text/javascript");
407-
//resp.setCharacterEncoding("utf-8");
407+
//resp.setCharacterEncoding("UTF-8");
408408
resp.getWriter().write("net.sf.j2s.ajax.SimpleRPCRequest" +
409409
".xssNotify(\"" + scriptRequestID + "\", \"unsupported\");");
410410
return null;
@@ -436,7 +436,7 @@ private String prepareScriptRequest(HttpServletRequest req, HttpServletResponse
436436
// check whether servlet can deal the requests
437437
if (partsCount > maxXSSRequestParts()) {
438438
resp.setContentType("text/javascript");
439-
//resp.setCharacterEncoding("utf-8");
439+
//resp.setCharacterEncoding("UTF-8");
440440
resp.getWriter().write("net.sf.j2s.ajax.SimpleRPCRequest" +
441441
".xssNotify(\"" + scriptRequestID + "\", \"exceedrequestlimit\");");
442442
return null;
@@ -488,7 +488,7 @@ private String prepareScriptRequest(HttpServletRequest req, HttpServletResponse
488488
}
489489
if (toContinue) {
490490
resp.setContentType("text/javascript");
491-
//resp.setCharacterEncoding("utf-8");
491+
//resp.setCharacterEncoding("UTF-8");
492492
resp.getWriter().write("net.sf.j2s.ajax.SimpleRPCRequest" +
493493
".xssNotify(\"" + scriptRequestID + "\", \"continue\"" +
494494
((curPart == 1) ? ", \"" + session.getId() + "\");" : ");"));

sources/net.sf.j2s.ajax/ajaxrpc/net/sf/j2s/ajax/SimpleSerializable.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ public class SimpleSerializable implements Cloneable {
6868
private static Set<String> classMissed = new HashSet<String>();
6969

7070
@J2SIgnore
71-
protected static Charset UTF_8 = Charset.forName("utf-8");
71+
public static Charset UTF_8 = Charset.forName("UTF-8");
7272

7373
@J2SIgnore
74-
protected static Charset ISO_8859_1 = Charset.forName("iso-8859-1");
74+
public static Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
7575

7676
private int simpleVersion;
7777

sources/net.sf.j2s.ajax/generator/net/sf/j2s/ajax/SimpleSource4ObjectiveC.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class SimpleSource4ObjectiveC {
3535
static String author = "Author";
3636
static String company = "Company";
3737
static String constantPrefix = "C_";
38-
38+
static boolean supportsARC = false;
3939

4040
private static String wrapString(String s) {
4141
if (s == null) {
@@ -894,10 +894,15 @@ public static String generateImplementation(SimpleSerializable s) {
894894
@SuppressWarnings("deprecation")
895895
public static void main(String[] args) {
896896
if (args == null || args.length < 2 + 3) {
897-
System.out.println("Usage: " + SimpleSource4ObjectiveC.class.getName() + " <sources folder> <author> <orgization or company> <constant prefix> <factory name> <class> [class ...]");
897+
System.out.println("Usage: " + SimpleSource4ObjectiveC.class.getName() + "[--arc] <sources folder> <author> <orgization or company> <constant prefix> <factory name> <class> [class ...]");
898898
return;
899899
}
900-
String targetFolder = args[0];
900+
int indexStarting = 0;
901+
if ("--arc".equals(args[0])) {
902+
supportsARC = true;
903+
indexStarting = 1;
904+
}
905+
String targetFolder = args[indexStarting];
901906
File f = new File(targetFolder);
902907
if (f.exists()) {
903908
if (!f.isDirectory()) {
@@ -912,12 +917,13 @@ public static void main(String[] args) {
912917
}
913918
}
914919
folder = f.getName();
915-
author = args[1];
916-
company = args[2];
917-
constantPrefix = args[3];
920+
author = args[indexStarting + 1];
921+
company = args[indexStarting + 2];
922+
constantPrefix = args[indexStarting + 3];
918923

919-
String factoryClazz = args[4];
920-
for (int i = 1 + 4; i < args.length; i++) {
924+
String factoryClazz = args[indexStarting + 4];
925+
int classStartingIndex = indexStarting + 5;
926+
for (int i = classStartingIndex; i < args.length; i++) {
921927
String j2sSimpleClazz = args[i];
922928
try {
923929
Class<?> clazz = Class.forName(j2sSimpleClazz);
@@ -985,7 +991,7 @@ public static void main(String[] args) {
985991
source.append("#import \"SimpleFactory.h\"\r\n");
986992
source.append("\r\n");
987993

988-
for (int i = 1 + 4; i < args.length; i++) {
994+
for (int i = classStartingIndex; i < args.length; i++) {
989995
String j2sSimpleClazz = args[i];
990996
try {
991997
Class<?> clazz = Class.forName(j2sSimpleClazz);
@@ -1066,7 +1072,7 @@ public static void main(String[] args) {
10661072
source.append("- (id) createInstanceByClassName:(NSString *) className {\r\n");
10671073
SourceUtils.insertLineComment(source, "\t", index++, false);
10681074

1069-
for (int i = 1 + 4; i < args.length; i++) {
1075+
for (int i = classStartingIndex; i < args.length; i++) {
10701076
String j2sSimpleClazz = args[i];
10711077
try {
10721078
Class<?> clazz = Class.forName(j2sSimpleClazz);
@@ -1135,7 +1141,7 @@ public static void main(String[] args) {
11351141
source.append("- (id) getClassFullName:(NSString *) className {\r\n");
11361142
SourceUtils.insertLineComment(source, "\t", index++, false);
11371143

1138-
for (int i = 1 + 4; i < args.length; i++) {
1144+
for (int i = classStartingIndex; i < args.length; i++) {
11391145
String j2sSimpleClazz = args[i];
11401146
try {
11411147
Class<?> clazz = Class.forName(j2sSimpleClazz);

sources/net.sf.j2s.ajax/generator/net/sf/j2s/ajax/SourceUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public static void updateSourceContent(File file, String source) {
188188
FileOutputStream fos = null;
189189
try {
190190
fos = new FileOutputStream(file);
191-
fos.write(source.getBytes("utf-8"));
191+
fos.write(source.getBytes("UTF-8"));
192192
} catch (IOException e) {
193193
e.printStackTrace();
194194
} finally {

0 commit comments

Comments
 (0)