Skip to content

Commit 474863b

Browse files
committed
OC isRemote fixes
1 parent 7701d4c commit 474863b

File tree

5 files changed

+95
-94
lines changed

5 files changed

+95
-94
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20211027044255
1+
20211213135401
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20210208070817
1+
20211028131132
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20211027044255
1+
20211213135401

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

Lines changed: 92 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ public void writeByteAsInt(int b) {
235235
public void write(byte[] buf, int i, int len) {
236236
if (os == null)
237237
initOS();
238+
if (len < 0)
239+
len = buf.length - i;
238240
try {
239241
os.write(buf, i, len);
240242
} catch (IOException e) {
@@ -294,91 +296,91 @@ public void cancel() {
294296
closeChannel();
295297
}
296298

297-
@Override
298-
@SuppressWarnings({ "unused", "null" })
299-
public String closeChannel() {
300-
if (closed)
301-
return null;
302-
// can't cancel file writers
303-
try {
304-
if (bw != null) {
305-
bw.flush();
306-
bw.close();
307-
} else if (os != null) {
308-
os.flush();
309-
os.close();
310-
}
311-
if (os0 != null && isCanceled) {
312-
os0.flush();
313-
os0.close();
314-
}
315-
} catch (Exception e) {
316-
// ignore closing issues
317-
}
318-
if (isCanceled) {
319-
closed = true;
320-
return null;
321-
}
322-
if (fileName == null) {
323-
if (isBase64) {
324-
String s = getBase64();
325-
if (os0 != null) {
326-
os = os0;
327-
append(s);
328-
}
329-
sb = new SB();
330-
sb.append(s);
331-
isBase64 = false;
332-
return closeChannel();
333-
}
334-
return (sb == null ? null : sb.toString());
335-
}
336-
closed = true;
337-
if (!isLocalFile) {
338-
String ret = postByteArray(); // unsigned applet could do this
339-
if (ret.startsWith("java.net"))
340-
byteCount = -1;
341-
return ret;
342-
}
343-
J2SObjectInterface J2S = null;
344-
Object _function = null;
345-
boolean isSwingJS = false;
346-
/**
347-
* @j2sNative isSwingJS = !!self.J2S; J2S = self.J2S || self.Jmol; _function
348-
* = (typeof this.fileName == "function" ? this.fileName : null);
349-
*
350-
*/
351-
if (J2S != null && !isTemp) {
352-
353-
// action here generally will be for the browser to display a download message
354-
// temp files will not be sent this way.
355-
Object data = (sb == null ? toByteArray() : sb.toString());
356-
if (_function != null) {
357-
J2S.applyFunc(_function, data);
358-
} else if (isSwingJS) {
359-
if (os == null && bw == null && sb != null && fileName != null) {
360-
try {
361-
os = new FileOutputStream(fileName);
362-
os.write(sb.toBytes(0, -1));
299+
@Override
300+
@SuppressWarnings({ "unused", "null" })
301+
public String closeChannel() {
302+
if (closed)
303+
return null;
304+
// can't cancel file writers
305+
try {
306+
if (bw != null) {
307+
bw.flush();
308+
bw.close();
309+
} else if (os != null) {
310+
os.flush();
363311
os.close();
364-
} catch (FileNotFoundException e) {
365-
e.printStackTrace();
366-
} catch (IOException e) {
367-
e.printStackTrace();
368312
}
369-
}
370-
} else {
371-
Object info = /** @j2sNative { isBinary : (this.sb == null) } || */
372-
null;
373-
String mimetype = null;
374-
if (bytes != null && Rdr.isZipB(bytes)) {
375-
mimetype = "application/zip";
376-
}
377-
J2S.doAjax(fileName, mimetype, data, info);
378-
}
379-
}
380-
return null;
381-
}
313+
if (os0 != null && isCanceled) {
314+
os0.flush();
315+
os0.close();
316+
}
317+
} catch (Exception e) {
318+
// ignore closing issues
319+
}
320+
if (isCanceled) {
321+
closed = true;
322+
return null;
323+
}
324+
if (fileName == null) {
325+
if (isBase64) {
326+
String s = getBase64();
327+
if (os0 != null) {
328+
os = os0;
329+
append(s);
330+
}
331+
sb = new SB();
332+
sb.append(s);
333+
isBase64 = false;
334+
return closeChannel();
335+
}
336+
return (sb == null ? null : sb.toString());
337+
}
338+
closed = true;
339+
if (!isLocalFile) {
340+
String ret = postByteArray(); // unsigned applet could do this
341+
if (ret == null || ret.startsWith("java.net"))
342+
byteCount = -1;
343+
return ret;
344+
}
345+
J2SObjectInterface J2S = null;
346+
Object _function = null;
347+
boolean isSwingJS = false;
348+
/**
349+
* @j2sNative isSwingJS = !!self.J2S; J2S = self.J2S || self.Jmol; _function =
350+
* (typeof this.fileName == "function" ? this.fileName : null);
351+
*
352+
*/
353+
if (J2S != null && !isTemp) {
354+
355+
// action here generally will be for the browser to display a download message
356+
// temp files will not be sent this way.
357+
Object data = (sb == null ? toByteArray() : sb.toString());
358+
if (_function != null) {
359+
J2S.applyFunc(_function, data);
360+
} else if (isSwingJS) {
361+
if (os == null && bw == null && sb != null && fileName != null) {
362+
try {
363+
os = new FileOutputStream(fileName);
364+
os.write(sb.toBytes(0, -1));
365+
os.close();
366+
} catch (FileNotFoundException e) {
367+
e.printStackTrace();
368+
} catch (IOException e) {
369+
e.printStackTrace();
370+
}
371+
}
372+
} else {
373+
Object info = /** @j2sNative { isBinary : (this.sb == null) } || */
374+
null;
375+
String mimetype = null;
376+
if (bytes != null && Rdr.isZipB(bytes)) {
377+
mimetype = "application/zip";
378+
}
379+
J2S.doAjax(fileName, mimetype, data, info);
380+
}
381+
}
382+
return null;
383+
}
382384

383385
public boolean isBase64() {
384386
return isBase64;
@@ -423,22 +425,21 @@ private String postByteArray() {
423425
return bytePoster == null ? null : bytePoster.postByteArray(fileName, toByteArray());
424426
}
425427

426-
public final static String[] urlPrefixes = { "http:", "https:", "sftp:", "ftp:", "file:" };
428+
public final static String[] urlPrefixes = { "http:", "https:", "sftp:", "ftp:", "file:", "cache:" };
427429
// note that SFTP is not supported
428-
public final static int URL_LOCAL = 4;
430+
public final static int URL_LOCAL = 4, URL_CACHE = 5;
429431

430432
public static boolean isRemote(String fileName) {
431-
if (fileName == null)
433+
if (fileName == null || fileName.equals(";base64,"))
432434
return false;
433435
int itype = urlTypeIndex(fileName);
434-
return (itype >= 0 && itype != URL_LOCAL);
436+
// was !=, but I think cache counts as local
437+
// this is only for Jmol.
438+
return (itype >= 0 && itype < URL_LOCAL);
435439
}
436440

437441
public static boolean isLocal(String fileName) {
438-
if (fileName == null)
439-
return false;
440-
int itype = urlTypeIndex(fileName);
441-
return (itype < 0 || itype == URL_LOCAL);
442+
return (fileName != null && !isRemote(fileName));
442443
}
443444

444445
public static int urlTypeIndex(String name) {

0 commit comments

Comments
 (0)