Skip to content

Commit 3ad46d4

Browse files
committed
SwingJS-site.zip and tests
1 parent bbd1633 commit 3ad46d4

File tree

11 files changed

+147
-133
lines changed

11 files changed

+147
-133
lines changed
553 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20210728102503
1+
20210728170251
553 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20210728102503
1+
20210728170251
553 Bytes
Binary file not shown.

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

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@ public class Test_Image extends Test_ {
3939

4040
public static void main(String[] args) {
4141

42-
// testSource();
43-
testPacked();
44-
// testGray();
45-
// testRead();
46-
// testWrite();
47-
48-
System.out.println("Test_Image OK");
42+
System.err.println("TODO: testSource and testRead are not working!");
43+
testPacked();
44+
testSource();
45+
testGray();
46+
testRead();
47+
testWrite();
48+
49+
// System.out.println("Test_Image OK");
4950
}
5051

5152
@SuppressWarnings("unused")
@@ -263,9 +264,11 @@ private static void testPacked() {
263264
packedData[i] = (byte) i;
264265
}
265266
DataBuffer databuffer = new DataBufferByte(packedData, len);
266-
WritableRaster raster = Raster.createPackedRaster(databuffer, nx, ny, 1, null);
267+
int bitsPerPixel = 1;
268+
WritableRaster raster = Raster.createPackedRaster(databuffer, nx, ny, bitsPerPixel, null);
267269
// default colors are red and blue
268-
ColorModel colorModel = new IndexColorModel(1, 2,
270+
int arrayLength = 2;
271+
ColorModel colorModel = new IndexColorModel(bitsPerPixel, arrayLength,
269272
new byte[] {(byte) 255, (byte) 0},
270273
new byte[] {(byte) 0, (byte) 0},
271274
new byte[] {(byte) 0, (byte) 255});
@@ -278,26 +281,30 @@ private static void testPacked() {
278281
Graphics2D g = image.createGraphics();
279282
g.setColor(new Color(0,0,255));
280283
g.fillRect(0, 0, 100, 100);
284+
System.out.println("after drawing, not updated " + Arrays.toString(data));
281285
g.dispose();
286+
System.out.println("after disposing, updated " + Arrays.toString(data));
282287
image.flush();
283-
System.out.println(Arrays.toString(data));
288+
System.out.println("after flush, updated " + Arrays.toString(data));
284289
dumpImage(image, nx, ny);
285290
}
286291

287292
private static void dumpImage(BufferedImage image, int nx, int ny) {
288293
System.out.println("----------------");
289294
int n = nx * ny;
290295
int[] pixels = new int[n * 4];
291-
for (int i = 0, pt = 0; i < n; i++, pt+=4) {
292-
293-
image.getColorModel().getComponents(i, pixels, pt);
294-
System.out.println(i + " " + nx + " " + ny + ": " + pixels[pt] + " " + pixels[pt+1] + " " + pixels[pt+2] + " " + pixels[pt+3]);
295-
}
296+
for (int i = 0, pt = 0; i < n; i++, pt += 4) {
297+
298+
image.getColorModel().getComponents(i, pixels, pt);
299+
System.out.println(i + " " + nx + " " + ny + ": " + pixels[pt] + " " + pixels[pt + 1] + " " + pixels[pt + 2]
300+
+ " " + pixels[pt + 3]);
301+
}
296302
System.out.println("===========");
297-
for (int i = 0; i < nx; i++) {
298-
for (int j = 0; j < ny; j++) {
299-
System.out.println(Integer.toHexString(image.getRGB(i, j)));
303+
for (int j = 0; j < ny; j++) {
304+
for (int i = 0; i < nx; i++) {
305+
System.out.print("\t" + Integer.toHexString(image.getRGB(i, j)));
300306
}
307+
System.out.println("");
301308
}
302309
}
303310

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ public static void main(String[] args) {
6464
long n;
6565
StringBuffer sb = new StringBuffer("test");
6666
System.out.println("below is 'test'? " + sb);
67-
sb.chars().mapToObj(i -> (char) i).forEach(System.out::print);
67+
sb.chars()
68+
.mapToObj(i -> (char) i)
69+
.forEach(System.out::print);
6870
System.out.println("\nabove is 'test'? " + sb);
6971
n = sb.chars().count();
7072
assert (n == 4);

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package test;
22

3-
import java.io.BufferedInputStream;
43
import java.io.IOException;
54
import java.io.InputStream;
65
import java.math.BigDecimal;
@@ -13,7 +12,6 @@
1312
import java.util.List;
1413
import java.util.Map;
1514

16-
import javajs.util.Rdr;
1715
import swingjs.JSUtil;
1816

1917
public class Test_JSON extends Test_ {
@@ -45,7 +43,7 @@ public static void main(String[] args) {
4543
* data = data["1cbs"][0].title;
4644
*/
4745
if (data instanceof InputStream) {
48-
data = Rdr.streamToUTF8String(new BufferedInputStream((InputStream) data));
46+
data = JSUtil.streamToString((InputStream) data);
4947
}
5048
System.out.println(data);
5149

@@ -54,7 +52,7 @@ public static void main(String[] args) {
5452
JSUtil.setAjax(url);
5553
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
5654
data = connection.getInputStream();
57-
data = Rdr.streamToUTF8String(new BufferedInputStream((InputStream) data));
55+
data = JSUtil.streamToString((InputStream) data);
5856
System.out.println(data);
5957

6058

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

Lines changed: 73 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,62 +3,50 @@
33
import java.io.UnsupportedEncodingException;
44
import java.nio.CharBuffer;
55

6+
import javajs.util.Rdr;
67
import javajs.util.SB;
78

89
public class Test_String extends Test_ {
910

1011
public static void main(String[] args) {
12+
String s, sb;
13+
byte[] b;
1114

12-
String sb = new String(new byte[] { 97, 98, 99 });
15+
sb = new String(new byte[] { 97, 98, 99 });
1316
System.out.println(sb);
1417
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);
19-
assert ("test".contentEquals(new StringBuffer("test")));
20-
int ii = "test\2ing".charAt(4);
21-
switch (ii | 'd') {
22-
case 'f':
23-
assert (true);
24-
break;
25-
case '3':
26-
case 3:
27-
default:
28-
assert (false);
29-
}
3018

31-
CharBuffer cb = CharBuffer.allocate(4);
32-
cb.mark();
33-
cb.put('a');
34-
cb.put('b');
35-
cb.put('c');
36-
cb.reset();
37-
StringBuffer sbb;
38-
sbb = new StringBuffer("testing");
39-
sbb.insert(0, cb);
40-
System.out.println(sbb);
41-
assert(sbb.toString().equals("abc\0testing"));
42-
System.out.println(">" + cb.toString() + "<");
43-
cb.position(2);
44-
System.out.println(">" + cb.toString() + "<");
45-
assert(cb.toString().equals("c\0"));
46-
sbb = new StringBuffer("testing");
47-
sbb.insert(3, cb, 0, 1);
48-
System.out.println(sbb);
49-
assert(sbb.toString().equals("tescting"));
50-
sb = "ab\u2520c";
51-
System.out.println(sb);
19+
5220
try {
53-
byte[] b = sb.getBytes("UTF-8");
54-
String s = "" + b[2] + b[3] + b[4];
21+
// Java will not ignore the BOM in the String constructor.
22+
// It will return 0xFEFF
23+
b = new byte[] {(byte) 0xEF,(byte) 0xBB,(byte) 0xBF, 'A', 'B', 'C' };
24+
sb = new String(b, "UTF-8"); // extra char
25+
System.out.println((int)sb.charAt(0));
26+
assert((int) sb.charAt(0) == 65279);
27+
b = sb.getBytes("UTF-16BE");
28+
s = "" + b[0] + b[1] +b[2];
29+
System.out.println(s);
30+
assert(s.equals("-2-10"));
31+
sb = sb.substring(1);
32+
System.out.println(sb);
33+
assert(sb.equals("ABC"));
34+
35+
sb = new String(new byte[] { 97, 98, 99 }, 1, 2);
36+
System.out.println(sb);
37+
assert (sb.equals("bc"));
38+
39+
sb = "ab\u2520c";
40+
System.out.println(sb);
41+
42+
b = sb.getBytes("UTF-8"); // no BOM
43+
s = "" + b[2] + b[3] + b[4];
5544
System.out.println(s);
5645
assert(s.equals("-30-108-96"));
5746
s = new String(b, "UTF-8");
5847
System.out.println(s);
5948
assert(s.equals(sb));
60-
b = sb.getBytes("UTF8"); // no BOM
61-
49+
6250
b = sb.getBytes("UTF-16BE"); // no BOM
6351
s = "" + b[2] + b[3] + b[4];
6452
System.out.println(s);
@@ -74,6 +62,7 @@ public static void main(String[] args) {
7462
s = new String(b, "UTF-16");
7563
System.out.println(s);
7664
assert(s.equals(sb));
65+
System.out.println(">" + Rdr.fixUTF(b) + "<");
7766

7867
b = sb.getBytes("UTF-16LE"); // no BOM
7968
s = "" + b[2] + b[3] + b[4];
@@ -87,6 +76,45 @@ public static void main(String[] args) {
8776
// TODO Auto-generated catch block
8877
e.printStackTrace();
8978
}
79+
80+
81+
82+
83+
assert("abcde".indexOf(99) == 2);
84+
assert ("test".contentEquals(new StringBuffer("test")));
85+
int ii = "test\2ing".charAt(4);
86+
switch (ii | 'd') {
87+
case 'f':
88+
assert (true);
89+
break;
90+
case '3':
91+
case 3:
92+
default:
93+
assert (false);
94+
}
95+
96+
CharBuffer cb = CharBuffer.allocate(4);
97+
cb.mark();
98+
cb.put('a');
99+
cb.put('b');
100+
cb.put('c');
101+
cb.reset();
102+
StringBuffer sbb;
103+
sbb = new StringBuffer("testing");
104+
sbb.insert(0, cb);
105+
System.out.println(sbb);
106+
assert(sbb.toString().equals("abc\0testing"));
107+
System.out.println(">" + cb.toString() + "<");
108+
cb.position(2);
109+
System.out.println(">" + cb.toString() + "<");
110+
assert(cb.toString().equals("c\0"));
111+
sbb = new StringBuffer("testing");
112+
sbb.insert(3, cb, 0, 1);
113+
System.out.println(sbb);
114+
assert(sbb.toString().equals("tescting"));
115+
sb = "ab\u2520c";
116+
System.out.println(sb);
117+
90118
assert ("test".compareToIgnoreCase("Test") == 0);
91119
assert ("test".compareToIgnoreCase("Testing") < 0);
92120
assert ("test".compareToIgnoreCase("Sest") > 0);
@@ -111,7 +139,7 @@ public static void main(String[] args) {
111139
long t0;
112140
int n = (/**@j2sNative 1 ? 100 : */10);
113141
t0 = System.currentTimeMillis();
114-
String s = "";
142+
s = "";
115143
for (int i = 0; i < 10000; i++) {
116144
for (int j = 0; j < n; j++)
117145
s += i;
@@ -151,12 +179,12 @@ public static void main(String[] args) {
151179
System.out.println("java : ms 76 many StringBuilder len=400");
152180

153181
t0 = System.currentTimeMillis();
154-
SB b = new SB();
182+
SB buf = new SB();
155183
for (int i = 0; i < 10000; i++) {
156184
for (int j = 0; j < 100; j++)
157-
b.appendI(i);
185+
buf.appendI(i);
158186
}
159-
s = b.toString();
187+
s = buf.toString();
160188
System.out.println("\nms " + (System.currentTimeMillis() - t0) + "\t javajs.util.SB len=" + s.length());
161189
System.out.println("java : ms 85 javajs.util.SB len=3889000");
162190

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,9 @@ public static void main(String[] args) {
156156
connection.setConnectTimeout(CONNECT_TIMEOUT_MS);
157157
connection.setReadTimeout(10000);
158158

159-
InputStream fis = url.openStream();
160-
BufferedInputStream bis = new BufferedInputStream(fis);
161-
String s = Rdr.streamToUTF8String(bis);
162-
bis.close();
159+
InputStream is = url.openStream();
160+
String s = JSUtil.streamToString(is);
161+
is.close();
163162
System.out.println(s);
164163
assert (s.equals("{\"ping\":1}\n"));
165164

0 commit comments

Comments
 (0)