Skip to content

Commit 279a603

Browse files
authored
Merge pull request #194 from BobHanson/master
SimpleHTTPClient and j2sClazz new String TextDecoder
2 parents e84b8b2 + 9d814a9 commit 279a603

File tree

10 files changed

+174
-585
lines changed

10 files changed

+174
-585
lines changed
-5.83 KB
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20210720163506
1+
20210728102503
-5.83 KB
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20210720163506
1+
20210728102503
-5.83 KB
Binary file not shown.

sources/net.sf.j2s.java.core/src/javajs/http/SimpleHttpClient.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -287,22 +287,28 @@ public HttpRequest clearFormParts(String name) {
287287
}
288288

289289
private byte[] toBytes(Object data) {
290-
try {
291-
if (data == null || data instanceof byte[]) {
292-
} else if (data instanceof File) {
293-
FileInputStream fis = new FileInputStream((File) data);
294-
data = getBytes(fis);
295-
fis.close();
296-
} else if (data instanceof InputStream) {
297-
InputStream is = (InputStream) data;
298-
data = getBytes(is);
299-
is.close();
300-
} else {
301-
data = data.toString().getBytes();
290+
if (data == null || data instanceof byte[]) {
291+
return (byte[]) data;
292+
}
293+
if (data instanceof File) {
294+
try (FileInputStream fis = new FileInputStream((File) data)) {
295+
return getBytes(fis);
296+
}
297+
catch (IOException e) {
298+
e.printStackTrace();
299+
return null;
300+
}
301+
}
302+
if (data instanceof InputStream) {
303+
try (InputStream is = (InputStream) data) {
304+
return getBytes(is);
305+
}
306+
catch (IOException e) {
307+
e.printStackTrace();
308+
return null;
302309
}
303-
} catch (IOException e) {
304310
}
305-
return (byte[]) data;
311+
return data.toString().getBytes();
306312
}
307313

308314
@Override

sources/net.sf.j2s.java.core/src/test/Test_String.java

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ public class Test_String extends Test_ {
99

1010
public static void main(String[] args) {
1111

12-
System.out.println("abcde".indexOf(99));
12+
String sb = new String(new byte[] { 97, 98, 99 });
13+
System.out.println(sb);
14+
assert (sb.equals("abc"));
15+
sb = new String(new byte[] { 97, 98, 99 }, 1, 2);
16+
System.out.println(sb);
17+
assert (sb.equals("bc"));
18+
assert("abcde".indexOf(99) == 2);
1319
assert ("test".contentEquals(new StringBuffer("test")));
1420
int ii = "test\2ing".charAt(4);
1521
switch (ii | 'd') {
@@ -22,32 +28,61 @@ public static void main(String[] args) {
2228
assert (false);
2329
}
2430

25-
CharBuffer cb = CharBuffer.allocate(10);
31+
CharBuffer cb = CharBuffer.allocate(4);
2632
cb.mark();
2733
cb.put('a');
2834
cb.put('b');
2935
cb.put('c');
30-
31-
String sb = new String(new byte[] { 97, 98, 99 });
32-
System.out.println(sb);
33-
assert (sb.equals("abc"));
34-
36+
cb.reset();
3537
StringBuffer sbb;
3638
sbb = new StringBuffer("testing");
3739
sbb.insert(0, cb);
38-
3940
System.out.println(sbb);
41+
assert(sbb.toString().equals("abc\0testing"));
4042
System.out.println(">" + cb.toString() + "<");
41-
cb.reset();
43+
cb.position(2);
4244
System.out.println(">" + cb.toString() + "<");
45+
assert(cb.toString().equals("c\0"));
4346
sbb = new StringBuffer("testing");
44-
sbb.insert(0, cb);
45-
47+
sbb.insert(3, cb, 0, 1);
4648
System.out.println(sbb);
47-
49+
assert(sbb.toString().equals("tescting"));
4850
sb = "ab\u2520c";
51+
System.out.println(sb);
4952
try {
5053
byte[] b = sb.getBytes("UTF-8");
54+
String s = "" + b[2] + b[3] + b[4];
55+
System.out.println(s);
56+
assert(s.equals("-30-108-96"));
57+
s = new String(b, "UTF-8");
58+
System.out.println(s);
59+
assert(s.equals(sb));
60+
b = sb.getBytes("UTF8"); // no BOM
61+
62+
b = sb.getBytes("UTF-16BE"); // no BOM
63+
s = "" + b[2] + b[3] + b[4];
64+
System.out.println(s);
65+
assert(s.equals("09837"));
66+
s = new String(b, "UTF-16BE");
67+
System.out.println(s);
68+
assert(s.equals(sb));
69+
70+
b = sb.getBytes("UTF-16"); // includes BOM -2, -1
71+
s = "" + b[2] + b[3] + b[4];
72+
System.out.println(s);
73+
assert(s.equals("0970"));
74+
s = new String(b, "UTF-16");
75+
System.out.println(s);
76+
assert(s.equals(sb));
77+
78+
b = sb.getBytes("UTF-16LE"); // no BOM
79+
s = "" + b[2] + b[3] + b[4];
80+
System.out.println(s);
81+
assert(s.equals("98032"));
82+
s = new String(b, "UTF-16LE");
83+
System.out.println(s);
84+
assert(s.equals(sb));
85+
5186
} catch (UnsupportedEncodingException e) {
5287
// TODO Auto-generated catch block
5388
e.printStackTrace();
@@ -81,16 +116,18 @@ public static void main(String[] args) {
81116
for (int j = 0; j < n; j++)
82117
s += i;
83118
}
84-
System.out.println("ms " + (System.currentTimeMillis() - t0) + "\t s+= len=" + s.length());
85-
119+
System.out.println("\nms " + (System.currentTimeMillis() - t0) + "\t s+= len=" + s.length());
120+
System.out.println("java : ms 14825 s+= len=388900");
121+
86122
t0 = System.currentTimeMillis();
87123
StringBuffer Sb = new StringBuffer();
88124
for (int i = 0; i < 10000; i++) {
89125
for (int j = 0; j < 100; j++)
90126
Sb.append(i);
91127
}
92128
s = Sb.toString();
93-
System.out.println("ms " + (System.currentTimeMillis() - t0) + "\t one StringBuffer len=" + s.length());
129+
System.out.println("\nms " + (System.currentTimeMillis() - t0) + "\t one StringBuffer len=" + s.length());
130+
System.out.println("java : ms 91 one StringBuffer len=3889000");
94131

95132
t0 = System.currentTimeMillis();
96133
StringBuilder S = new StringBuilder();
@@ -99,7 +136,8 @@ public static void main(String[] args) {
99136
S.append(i);
100137
}
101138
s = S.toString();
102-
System.out.println("ms " + (System.currentTimeMillis() - t0) + "\t one StringBuilder len=" + s.length());
139+
System.out.println("\nms " + (System.currentTimeMillis() - t0) + "\t one StringBuilder len=" + s.length());
140+
System.out.println("java : ms 76 one StringBuilder len=3889000");
103141

104142
t0 = System.currentTimeMillis();
105143
for (int i = 0; i < 10000; i++) {
@@ -109,8 +147,8 @@ public static void main(String[] args) {
109147
}
110148
s = SB.toString();
111149
}
112-
System.out.println("ms " + (System.currentTimeMillis() - t0) + "\t many StringBuilder len=" + s.length());
113-
150+
System.out.println("\nms " + (System.currentTimeMillis() - t0) + "\t many StringBuilder len=" + s.length());
151+
System.out.println("java : ms 76 many StringBuilder len=400");
114152

115153
t0 = System.currentTimeMillis();
116154
SB b = new SB();
@@ -119,7 +157,8 @@ public static void main(String[] args) {
119157
b.appendI(i);
120158
}
121159
s = b.toString();
122-
System.out.println("ms " + (System.currentTimeMillis() - t0) + "\t javajs.util.SB len=" + s.length());
160+
System.out.println("\nms " + (System.currentTimeMillis() - t0) + "\t javajs.util.SB len=" + s.length());
161+
System.out.println("java : ms 85 javajs.util.SB len=3889000");
123162

124163
//Output prior to optimization of AbstractStringBuilder:
125164
//

sources/net.sf.j2s.java.core/srcjs/js/devnotes.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,30 @@ j2sClazz and j2sApplet older development notes
33
j2sClazz.js (formerly j2s.lib.js)
44
-----------
55

6+
// BH 2020.12.31 3.3.1-v1 full 64-bit long support; BigDecimal, BigInteger fully 64-bit
7+
8+
// BH 2020.12.19 3.2.10-v1 preliminary work aiming to back long with [r,m,s].
9+
// should be fully backward compatible;
10+
// supports the 3.2.10 transpiler.
11+
// BH 2020.12.11 fixing interface extended override of interface default
12+
// BH 2020.12.06 changing Long maxval to 0x1FFFFFFFFFFFFF from 0x20000000000000
13+
// BH 2020.12.06 better error checking for TYPE.parseTYPE(string)
14+
// BH 2020.07.27 fix for inner class array names
15+
// BH 2020.06.18 better test for instanceof Object[]
16+
// BH 2020.06.03 sets user.home and user.dir to /TEMP/swingjs, and user.name to "swingjs"
17+
// BH 2020.04.01 2.2.0-v1e fixes missing C$.superclazz when class loaded from core
18+
// BH 2020.03.19 3.2.9-v1c fixes new String("xxx") !== "xxx"
19+
// BH 2020.03.11 3.2.9-v1b fixes numerous subtle issues with boxed primitives Integer, Float, etc.
20+
// BH 2020.03.07 3.2.9-v1a fixes array.hashCode() to be System.identityHashCode(array).
21+
// BH 2020.02.18 3.2.8-v2 upgrades String, Integer, ClassLoader, Package, various Exceptions
22+
// BH 2020.02.12 3.2.8-v1 new Throwable().getStackTrace() should not include j2sClazz methods
23+
// BH 2020.02.02 3.2.7-v5 fixes array.getClass().getName() and getArrayClass() for short -- should be [S, not [H, for Java
24+
// BH 2019.12.29 3.2.6 fixes Float.parseFloat$S("NaN") [and Double]
25+
// BH 2019.12.23 3.2.6 update of System
26+
// BH 2019.12.19 3.2.6 revision of $clinit$
27+
// BH 2019.12.16 3.2.5-v4 adds ClassLoader static methods for system resources (just j2s/...)
28+
// BH 2019.12.15 3.2.5-v4 Character.prototype.valueOf() missing
29+
// BH 2019.12.14 3.2.5-v3 Clazz._4Name initialization should be full static initialization
630
// BH 2019.12.03 3.2.5.v2 Object.class instanceof java.lang.Class
731
// BH 2019.11.26 3.2.5.v1 errant if (args) in newInstance
832
// BH 2019.11.07 3.2.5.v0 full encapsulation

0 commit comments

Comments
 (0)