Skip to content

Commit c70181f

Browse files
hansonrhansonr
authored andcommitted
3.2.4.05 falstad apps and inner class issue
1 parent 014801d commit c70181f

File tree

14 files changed

+71
-42
lines changed

14 files changed

+71
-42
lines changed
977 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20181213113352
1+
20181213165944
977 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20181213113352
1+
20181213165944
977 Bytes
Binary file not shown.

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

Lines changed: 59 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,19 @@ private Object doAjax(boolean isBinary) {
5050
J2SObjectInterface J2S = /** @j2sNative self.J2S || */ null;
5151
Object info = (/** @j2sNative {isBinary: isBinary } || */null);
5252
Object result = J2S.doAjax(url.toString(), postOut, bytesOut, info);
53-
responseCode = /** @j2sNative info.xhr.status || */0;
53+
boolean isEmpty = false;
54+
// the problem is that jsmol.php is still returning crlf even if output is 0 bytes
55+
// and it is not passing through the not-found state, just 200
56+
/**
57+
* @j2sNative
58+
*
59+
* isEmpty = (!result || result.length == 2 && result[0] == 13 && result[1] == 10);
60+
* if (isEmpty)
61+
* result = new Int8Array;
62+
*
63+
*
64+
*/
65+
responseCode = isEmpty ? HTTP_NOT_FOUND : /** @j2sNative info.xhr.status || */0;
5466
return result;
5567
}
5668

@@ -71,21 +83,29 @@ public void outputString(String post) {
7183

7284
@Override
7385
public InputStream getInputStream() {
74-
responseCode = 200;
86+
responseCode = -1;
87+
return getInputStreamAndResponse(url, false);
88+
}
89+
90+
private InputStream getInputStreamAndResponse(URL url, boolean allowNWError) {
7591
BufferedInputStream is = getAttachedStreamData(url, false);
76-
if (is != null || getUseCaches() && (is = getCachedStream(url)) != null)
92+
if (is != null || getUseCaches() && (is = getCachedStream(url, allowNWError)) != null)
7793
return is;
7894
is = attachStreamData(url, doAjax(true));
7995
if (getUseCaches() && is != null)
8096
setCachedStream(url);
97+
isNetworkError(is);
8198
return is;
8299
}
83100

84101
static Map<String, Object> urlCache = new Hashtable<String, Object>();
85102

86-
private BufferedInputStream getCachedStream(URL url) {
103+
private BufferedInputStream getCachedStream(URL url, boolean allowNWError) {
87104
Object data = urlCache.get(url.toString());
88-
return (data == null ? null : Rdr.toBIS(data));
105+
if (data == null)
106+
return null;
107+
BufferedInputStream bis = Rdr.toBIS(data);
108+
return (allowNWError || !isNetworkError(bis) ? bis : null);
89109
}
90110

91111
private void setCachedStream(URL url) {
@@ -94,6 +114,23 @@ private void setCachedStream(URL url) {
94114
urlCache.put(url.toString(), data);
95115
}
96116

117+
private boolean isNetworkError(BufferedInputStream is) {
118+
is.mark(15);
119+
byte[] bytes = new byte[13];
120+
try {
121+
is.read(bytes);
122+
is.reset();
123+
for (int i = NETWORK_ERROR.length; --i >= 0;)
124+
if (bytes[i] != NETWORK_ERROR[i])
125+
return false;
126+
} catch (IOException e) {
127+
}
128+
responseCode = HTTP_NOT_FOUND;
129+
return true;
130+
}
131+
132+
final private static int[] NETWORK_ERROR = new int[] { 78, 101, 116, 119, 111, 114, 107, 69, 114, 114, 111, 114 };
133+
97134
/**
98135
* J2S will attach the data (String, SB, or byte[]) to any URL that is
99136
* retrieved using a ClassLoader. This improves performance by
@@ -133,39 +170,23 @@ public Object getContents() {
133170
return doAjax(false);
134171
}
135172

136-
@Override
137-
public int getResponseCode() throws IOException {
138-
/*
139-
* We have the response code already
140-
*/
141-
if (responseCode != -1) {
142-
return responseCode;
143-
}
144-
145-
/*
146-
* Ensure that we have connected to the server. Record
147-
* exception as we need to re-throw it if there isn't
148-
* a status line.
149-
*/
150-
Exception exc = null;
151-
try {
152-
BufferedInputStream is = (BufferedInputStream) getInputStream();
153-
if (responseCode != HTTP_OK)
154-
return responseCode;
155-
if (is.available() > 40)
156-
return responseCode = HTTP_OK;
157-
is.mark(15);
158-
byte[] bytes = new byte[13];
159-
is.read(bytes);
160-
is.reset();
161-
String s = new String(bytes);
162-
if (s.startsWith("Network Error"))
163-
return responseCode = HTTP_NOT_FOUND;
164-
} catch (Exception e) {
165-
exc = e;
166-
}
167-
return responseCode = HTTP_OK;
168-
}
173+
@Override
174+
public int getResponseCode() throws IOException {
175+
/*
176+
* Check to see if have the response code already
177+
*/
178+
if (responseCode == -1) {
179+
/*
180+
* Ensure that we have connected to the server. Record exception as we need to
181+
* re-throw it if there isn't a status line.
182+
*/
183+
try {
184+
getInputStreamAndResponse(url, true);
185+
} catch (Exception e) {
186+
}
187+
}
188+
return responseCode;
189+
}
169190
@Override
170191
public void disconnect() {
171192
// TODO Auto-generated method stub

sources/net.sf.j2s.java.core/src/swingjs/plaf/JSComponentUI.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2041,7 +2041,6 @@ protected void setHorizontalButtonAlignments(JComponent b, int pos, int align) {
20412041
if (w == 0 && (w = this.jc.getWidth()) == 0) {
20422042
w = wText + wIcon;
20432043
}
2044-
System.out.println("JCUI " + w);
20452044
off = (w - wText - wIcon) / 2;
20462045
if (text0) {
20472046
DOMNode.setStyles(textNode, "left", off + "px");

sources/net.sf.j2s.java.core/src/swingjs/plaf/JSLabelUI.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ Dimension getMaximumSize(JComponent jc) {
111111
public void paint(Graphics g, JComponent c) {
112112
super.paint(g, c);
113113
if (icon != null) {
114-
System.out.println("JSLabelUI " + currentIcon.getIconWidth() + " " + currentIcon.getIconHeight());
114+
//System.out.println("JSLabelUI " + currentIcon.getIconWidth() + " " + currentIcon.getIconHeight());
115115
debugDump(iconNode);
116116
int w = icon.getIconWidth();
117117
int h = icon.getIconHeight();

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.io.InputStream;
77
import java.io.InputStreamReader;
88
import java.net.HttpURLConnection;
9+
import java.net.MalformedURLException;
910
import java.net.ProtocolException;
1011
import java.net.URL;
1112
import java.util.List;
@@ -183,6 +184,14 @@ protected static BufferedReader getHttpResponse(URL url, List<String> ids,
183184
public static void main(String[] args) {
184185

185186
try {
187+
URL url = new URL("https://asfadlkfj");
188+
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
189+
assert (connection.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND);
190+
} catch (IOException e1) {
191+
assert(false);
192+
}
193+
194+
try {
186195

187196

188197
// String path = "https://rest.ensemblgenomes.org/info/ping?content-type=application/json";

sources/net.sf.j2s.java.core/src2/test/baeldung/doublecolon/Computer.java renamed to sources/net.sf.j2s.java.core/src/test/baeldung/doublecolon/Computer.java

File renamed without changes.

0 commit comments

Comments
 (0)