Skip to content

Commit c14cb37

Browse files
committed
Adds Java 9 InputStream.transferTo, readAllBytes
1 parent 1b33c08 commit c14cb37

File tree

6 files changed

+30
-17
lines changed

6 files changed

+30
-17
lines changed

sources/net.sf.j2s.java.core/src/java/io/ByteArrayInputStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public synchronized long skip(long n) {
232232
* @throws IOException
233233
*/
234234
@Override
235-
public long transferTo(OutputStream out)
235+
public long transferTo(OutputStream out)
236236
throws IOException {
237237
byte[] b = (pos == 0 ? buf : readAllBytes());
238238
out.write(b);

sources/net.sf.j2s.java.core/src/java/io/FileInputStream.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -426,10 +426,9 @@ protected void finalize() throws IOException {
426426
* @throws IOException
427427
*/
428428
@Override
429-
public long transferTo​(OutputStream out)
430-
throws IOException {
429+
public long transferTo(OutputStream out) throws IOException {
431430
if (channel != null) {
432-
return super.transferTo(out);
431+
return super.transferTo(out);
433432
}
434433
byte[] b = (is.pos == 0 ? is.buf : is.readAllBytes());
435434
out.write(b);

sources/net.sf.j2s.java.core/src/java/io/FileOutputStream.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,7 @@ public void close() throws IOException {
370370
if (channel == null) {
371371
bos.close();
372372
fd._file.秘bytes = 秘bytes = bos.toByteArray();
373-
if (!fd._isTempFile())
374-
JSUtil.saveFile(path, 秘bytes, null, null);
373+
JSUtil.saveFile(path, 秘bytes, null, null);
375374
} else {
376375
channel.close();
377376
}

sources/net.sf.j2s.java.core/src/java/io/InputStream.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,7 @@ public long skip(long n) throws IOException {
218218
* @return
219219
* @throws IOException
220220
*/
221-
public long transferTo​(OutputStream out)
222-
throws IOException {
221+
public long transferTo(OutputStream out) throws IOException {
223222
long n = available();
224223
out.write(readAllBytes());
225224
return n;

sources/net.sf.j2s.java.core/src/swingjs/JSUtil.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ public static Object removeCachedFileData(String path) {
119119
*/
120120
@SuppressWarnings("unused")
121121
private static Object getFileContents(Object uriOrJSFile, boolean asBytes) {
122-
if (uriOrJSFile instanceof File) {
122+
boolean isFile = (uriOrJSFile instanceof File);
123+
if (isFile) {
123124
byte[] bytes = /** @j2sNative uriOrJSFile.秘bytes || */
124125
null;
125126
if (bytes != null)
@@ -142,15 +143,18 @@ private static Object getFileContents(Object uriOrJSFile, boolean asBytes) {
142143
}
143144
if (data == null && !uri.startsWith("./")) {
144145
// Java applications may use "./" here
145-
try {
146-
BufferedInputStream stream = (BufferedInputStream) new URL(uri).getContent();
147-
data = (asBytes ? Rdr.getStreamAsBytes(stream, null) : Rdr.streamToUTF8String(stream));
148-
} catch (Exception e) {
149-
// bypasses AjaxURLConnection
150-
data = J2S.getFileData(uri, null, false, asBytes);
151-
if (data == null)
152-
removeCachedFileData(uri);
146+
if (!isFile) {
147+
try {
148+
BufferedInputStream stream = (BufferedInputStream) new URL(uri).getContent();
149+
return (asBytes ? Rdr.getStreamAsBytes(stream, null) : Rdr.streamToUTF8String(stream));
150+
} catch (Exception e) {
151+
}
153152
}
153+
// bypasses AjaxURLConnection
154+
data = J2S.getFileData(uri, null, false, asBytes);
155+
if (data == null)
156+
removeCachedFileData(uri);
157+
154158
}
155159
return data;
156160
}
@@ -596,6 +600,14 @@ public static BufferedInputStream getURLInputStream(URL url, boolean andDelete)
596600
return null;
597601
}
598602

603+
/**
604+
* Display the web page in the give target, such as "_blank"
605+
*/
606+
@Override
607+
public void displayURL(String url, String target) {
608+
showWebPage((URL)(Object)url, target);
609+
}
610+
599611
public static void showWebPage(URL url, Object target) {
600612
/**
601613
* @j2sNative
@@ -604,6 +616,8 @@ public static void showWebPage(URL url, Object target) {
604616
* window.open(url.toString());
605617
*/
606618
}
619+
620+
607621

608622
/**
609623
* important warnings for TODO list

sources/net.sf.j2s.java.core/src/swingjs/api/JSUtilI.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,6 @@ public interface JSUtilI {
192192
*/
193193
byte[] addJSCachedBytes(Object URLorURIorFile);
194194

195+
void displayURL(String url, String target);
196+
195197
}

0 commit comments

Comments
 (0)