Skip to content

Commit a5d815a

Browse files
hansonrhansonr
authored andcommitted
javajs/util upgrade to math SwingJS-Jmol improvements
1 parent 0b8b02b commit a5d815a

File tree

12 files changed

+705
-580
lines changed

12 files changed

+705
-580
lines changed
826 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20201127044806
1+
20201129201754
826 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20201127044806
1+
20201129201754

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

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
import java.util.function.Consumer;
2222
import java.util.function.Function;
2323

24-
import javajs.http.HttpClient.HttpRequest;
25-
2624

2725
/**
2826
* SwingJS implementation of javajs.http.HttpClient and associated classes.
@@ -114,6 +112,7 @@ protected AjaxURLConnection(URL u) {
114112

115113
/**
116114
* JavaScript SwingJS-only method, not available in Java, only JavaScript
115+
* @param whenDone
117116
*/
118117
// not @Override, because that method is only in SwingJS, and this class is portable
119118
public abstract void getBytesAsync(Function<byte[], Void> whenDone);
@@ -156,12 +155,12 @@ String getFileName() {
156155
/**
157156
* the source URI
158157
*/
159-
private URI uri;
158+
URI uri;
160159

161160
/**
162161
* GET, HEAD, POST, PUT, or DELETE
163162
*/
164-
private String method;
163+
String method;
165164

166165
/**
167166
* headers, mostly ignored in SwingJS and AJAX
@@ -183,7 +182,7 @@ String getFileName() {
183182
* when TRUE, all parameters and files are transmitted as binary data in
184183
* multipart/form-data format
185184
*/
186-
private boolean hasFormBody = false;
185+
boolean hasFormBody = false;
187186

188187
Request(URI uri, String method) {
189188
this.uri = uri;
@@ -338,7 +337,7 @@ public void run() {
338337
return r;
339338
}
340339

341-
private Response fulfillGet(Response r) throws IOException {
340+
Response fulfillGet(Response r) throws IOException {
342341
return r.getResponse(getConnection(createURL()), this);
343342
}
344343

@@ -370,7 +369,7 @@ private String encodeURI(String value) {
370369
}
371370
}
372371

373-
private Response fulfillPost(Response r) throws IOException {
372+
Response fulfillPost(Response r) throws IOException {
374373
HttpURLConnection conn = getConnection(createURL());
375374
sendFormData(conn, formData);
376375
return r.getResponse(conn, this);
@@ -399,7 +398,8 @@ private void sendFormData(HttpURLConnection conn, List<FormData> formData) throw
399398
*
400399
* Create a connection that will be a javajs.util.AjaxURLConnection.
401400
*
402-
* @param uri
401+
* @param url
402+
* @return connection
403403
* @throws IOException
404404
*/
405405
private HttpURLConnection getConnection(URL url) throws IOException {
@@ -424,12 +424,12 @@ private HttpURLConnection getConnection(URL url) throws IOException {
424424

425425
class Response implements HttpResponse {
426426

427-
private int state = 0;
427+
int state = 0;
428428

429429
/**
430430
* set TRUE whenever at least one of succeed, fail, or always, is non-null
431431
*/
432-
private boolean isAsync;
432+
boolean isAsync;
433433

434434
private InputStream inputStream;
435435

@@ -450,7 +450,7 @@ class Response implements HttpResponse {
450450
private BiConsumer<HttpResponse, ? super IOException> always;
451451

452452

453-
private String method;
453+
String method;
454454

455455
private URI uri;
456456

@@ -473,6 +473,8 @@ class Response implements HttpResponse {
473473
* getResponseCode() is called.)
474474
*
475475
* @param conn
476+
* @param request
477+
* @return Response
476478
*
477479
* @throws IOException
478480
*/
@@ -502,15 +504,17 @@ public void run() {
502504
}
503505
doCallback(state == 0 || state >= 400, exception);
504506
} else {
505-
conn.getBytesAsync(new Function<byte[], Void>() {
507+
@SuppressWarnings("unused")
508+
Function<byte[], Void> f = new Function<byte[], Void>() {
506509

507-
@Override
508-
public Void apply(byte[] t) {
509-
doCallback(t != null, null);
510-
return null;
511-
}
510+
@Override
511+
public Void apply(byte[] t) {
512+
doCallback(t != null, null);
513+
return null;
514+
}
512515

513-
});
516+
};
517+
/** @j2sNative conn.getBytesAsync$(); */
514518
}
515519
}
516520
}).start();
@@ -521,6 +525,7 @@ public Void apply(byte[] t) {
521525
* Make the proper callback, depending upon response code and exception state.
522526
*
523527
* @param ok
528+
* @param e
524529
*/
525530
protected void doCallback(boolean ok, IOException e) {
526531
ok &= (e == null);
@@ -594,7 +599,7 @@ public String getText() throws IOException {
594599
*/
595600
@Override
596601
public InputStream getContent() throws IOException {
597-
return (inputStream == null ? (inputStream = (InputStream) conn.getInputStream())
602+
return (inputStream == null ? (inputStream = conn.getInputStream())
598603
: inputStream);
599604
}
600605

sources/net.sf.j2s.java.core/src/javajs/util/AjaxURLConnection.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.util.function.Function;
2020

2121
import javajs.api.js.J2SObjectInterface;
22-
import swingjs.JSUtil;
2322
import swingjs.api.JSUtilI;
2423

2524
/**
@@ -384,8 +383,10 @@ public OutputStream getOutputStream() throws IOException {
384383
return streamOut = new ByteArrayOutputStream();
385384
}
386385

387-
@Override
386+
@SuppressWarnings({ "null", "unused" })
387+
@Override
388388
public InputStream getInputStream() throws FileNotFoundException {
389+
InputStream is = /** @j2sNative this.is || */null;
389390
if (is != null)
390391
return is;
391392
responseCode = -1;
@@ -395,18 +396,19 @@ public InputStream getInputStream() throws FileNotFoundException {
395396
return is;
396397
}
397398

398-
@Override
399+
// dont @Override
399400
public void getBytesAsync(Function<byte[], Void> whenDone) {
400401
getInputStreamAsync(new Function<InputStream, Void>() {
401402

402403
@Override
403404
public Void apply(InputStream is) {
404405
try {
405406
if (is != null) {
406-
whenDone.apply(is.readAllBytes());
407+
byte[] bytes = /** @j2sNative is.readAllBytes$() || */null;
408+
whenDone.apply(bytes);
407409
return null;
408410
}
409-
} catch (IOException e) {
411+
} catch (Exception e) {
410412
}
411413
whenDone.apply(null);
412414
return null;
@@ -416,7 +418,9 @@ public Void apply(InputStream is) {
416418

417419
}
418420

419-
private void getInputStreamAsync(Function<InputStream, Void> whenDone) {
421+
@SuppressWarnings({ "null", "unused" })
422+
private void getInputStreamAsync(Function<InputStream, Void> whenDone) {
423+
InputStream is = /** @j2sNative is = this.is || */null;
420424
if (is != null) {
421425
whenDone.apply(is);
422426
return;
@@ -488,8 +492,9 @@ private BufferedInputStream getCachedStream(boolean allowNWError) {
488492
return null;
489493
@SuppressWarnings("unused")
490494
URL url = this.url;
491-
if (data instanceof byte[])
492-
url._streamData = data;
495+
if (data instanceof byte[]) {
496+
/** @j2sNative url._streamData = data */;
497+
}
493498
boolean isAjax = /** @j2sNative url.ajax || */
494499
false;
495500
BufferedInputStream bis = getBIS(data, isAjax);

sources/net.sf.j2s.java.core/src/javajs/util/JSJarURLConnection.java

Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,47 +13,51 @@
1313
import java.util.jar.JarFile;
1414
import java.util.jar.Manifest;
1515

16-
import swingjs.JSUtil;
17-
1816
public class JSJarURLConnection extends JarURLConnection {
1917

20-
boolean connected;
18+
boolean connected;
2119

22-
protected JSJarURLConnection(URL url) throws MalformedURLException {
23-
super(url);
24-
}
20+
protected JSJarURLConnection(URL url) throws MalformedURLException {
21+
super(url);
22+
}
2523

26-
@Override
27-
public JarFile getJarFile() throws IOException {
28-
JSUtil.notImplemented("JSJarURLConnection does not (yet) implement getJarFile()");
29-
return null;
30-
}
24+
@Override
25+
public JarFile getJarFile() throws IOException {
26+
System.err.println("JSJarURLConnection does not (yet) implement getJarFile()");
27+
return null;
28+
}
3129

32-
@Override
33-
public void connect() throws IOException {
34-
if (url._streamData != null)
35-
return;
36-
BufferedInputStream bis = (BufferedInputStream) getJarFileURL().openStream();
37-
byte[] bytes = ZipTools.getZipFileContentsAsBytes(bis, new String[] { getEntryName() }, 0);
38-
if (bytes == null)
39-
throw new JarException("Jar entry " + getEntryName() + " was not found in " + getJarFileURL());
40-
url._streamData = bytes;
41-
return;
42-
}
43-
@Override
44-
public InputStream getInputStream() {
45-
if (!connected && url._streamData == null)
46-
try {
47-
connect();
48-
} catch (IOException e) {
49-
return null;
50-
}
51-
return new ByteArrayInputStream((byte[]) url._streamData);
52-
}
30+
@SuppressWarnings("unused")
31+
@Override
32+
public void connect() throws IOException {
33+
Object data = /** @j2sNative this.url._streamData ||*/null;
34+
if (data != null)
35+
return;
36+
BufferedInputStream bis = (BufferedInputStream) getJarFileURL().openStream();
37+
byte[] bytes = ZipTools.getZipFileContentsAsBytes(bis, new String[] { getEntryName() }, 0);
38+
if (bytes == null)
39+
throw new JarException("Jar entry " + getEntryName() + " was not found in " + getJarFileURL());
40+
/**
41+
* @j2sNative this.url._streamData = bytes
42+
*/
43+
return;
44+
}
45+
46+
@Override
47+
public InputStream getInputStream() {
48+
Object data = /** @j2sNative this.url._streamData ||*/null;
49+
if (!connected && data == null)
50+
try {
51+
connect();
52+
} catch (IOException e) {
53+
return null;
54+
}
55+
return new ByteArrayInputStream(/** j2sNative this.url._streamData || */null);
56+
}
5357

5458
public Manifest getManifest() throws IOException {
55-
JSUtil.notImplemented(null);
56-
return null;
59+
System.err.println("JSJarURLConnection.getManifest is not implemented");
60+
return null;
5761
}
5862

5963
/**
@@ -70,18 +74,22 @@ public Manifest getManifest() throws IOException {
7074
* @see #getJarFile
7175
* @see #getJarEntry
7276
*/
77+
@Override
7378
public JarEntry getJarEntry() throws IOException {
74-
JSUtil.notImplemented(null);
75-
return null;
79+
System.err.println("JSJarURLConnection.getJarEntry is not implemented");
80+
return null;
7681
}
7782

78-
@Override
79-
public void getBytesAsync(Function<byte[], Void> whenDone) {
80-
try {
81-
whenDone.apply(getInputStream().readAllBytes());
82-
} catch (IOException e) {
83-
whenDone.apply(null);
84-
}
85-
}
83+
// dont use @Override
84+
@SuppressWarnings({ "unused", "null" })
85+
public void getBytesAsync(Function<byte[], Void> whenDone) {
86+
byte[] bytes = null;
87+
try {
88+
InputStream is = getInputStream();
89+
bytes = /** @j2sNative is.readAllBytes$() || */null;
90+
} catch (Exception e) {
91+
}
92+
whenDone.apply(bytes);
93+
}
8694

8795
}

sources/net.sf.j2s.java.core/src/javajs/util/PT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,7 +1144,7 @@ private static String formatString(String strFormat, String key, String strT,
11441144
if (strFormat.charAt(ich) == '.') {
11451145
++ich;
11461146
if ((ch = strFormat.charAt(ich)) == '-') {
1147-
isExponential = (strT == null);
1147+
isExponential = true;//(strT == null);
11481148
++ich;
11491149
}
11501150
if ((ch = strFormat.charAt(ich)) >= '0' && ch <= '9') {
@@ -1165,7 +1165,7 @@ private static String formatString(String strFormat, String key, String strT,
11651165
strLabel += formatF(floatT, width, precision, alignLeft,
11661166
zeroPad);
11671167
else if (strT != null) // 'd' 'i' or 's'
1168-
strLabel += formatS(strT, width, precision, alignLeft,
1168+
strLabel += formatS(strT, width, precision < 0 ? precision - 1 : precision, alignLeft,
11691169
zeroPad);
11701170
else if (!Double.isNaN(doubleT)) // 'e'
11711171
strLabel += formatD(doubleT, width, precision - 1, alignLeft,

0 commit comments

Comments
 (0)