Skip to content

Commit bbd1633

Browse files
committed
JSUtil.streamToBytes, streamToString
- should be faster - uses HTML5 TextDecoder
1 parent b09c436 commit bbd1633

File tree

4 files changed

+50
-21
lines changed

4 files changed

+50
-21
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public static String bytesToUTF8String(byte[] bytes) {
114114
* XMLReaders
115115
*
116116
* @param bis
117-
* @return a UTF-8 string
117+
* @return a UTF-8 string or null if there is an error
118118
*/
119119
public static String streamToUTF8String(BufferedInputStream bis) {
120120
String[] data = new String[1];
@@ -144,7 +144,8 @@ public static BufferedReader getBufferedReader(BufferedInputStream bis, String c
144144
}
145145

146146
/**
147-
* This method is specifically for strings that are marked for UTF 8 or 16.
147+
* This method is specifically for strings that might be marked for UTF 8 or 16.
148+
* In this case, Java would return a (0xFEFF) code point as the first character.
148149
*
149150
* @param bytes
150151
* @return UTF-decoded bytes
@@ -344,6 +345,8 @@ public static byte[] getBytesFromSB(SB sb) {
344345
* Read a an entire BufferedInputStream for its bytes, and either return them or
345346
* leave them in the designated output channel.
346347
*
348+
* Closes the stream.
349+
*
347350
* @param bis
348351
* @param out a destination output channel, or null
349352
* @return byte[] (if out is null) or a message indicating length (if not)

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ private static Object getFileContents(Object uriOrJSFile, boolean asBytes, JSFun
207207
try {
208208
URL url = new URL(uri);
209209
BufferedInputStream stream = (BufferedInputStream) url.getContent();
210-
return (asBytes ? Rdr.getStreamAsBytes(stream, null) : Rdr.streamToUTF8String(stream));
210+
return (asBytes ? streamToBytes(stream) : streamToString(stream));
211211
} catch (Exception e) {
212212
}
213213
}
@@ -248,11 +248,14 @@ static String ensureString(Object data) {
248248
if (data == null)
249249
return null;
250250
if (data instanceof byte[])
251-
return Rdr.bytesToUTF8String((byte[]) data);
251+
return new String((byte[]) data); // was Rdr.bytesToUTF8String
252252
if (data instanceof String || data instanceof SB)
253253
return data.toString();
254254
if (data instanceof InputStream)
255-
return Rdr.streamToUTF8String(new BufferedInputStream((InputStream) data));
255+
try {
256+
return streamToString((InputStream) data);
257+
} catch (IOException e) {
258+
}
256259
return null;
257260
}
258261

@@ -1267,6 +1270,29 @@ public String getJ2SPath() {
12671270
return (String) getAppletAttribute("_j2sFullPath");
12681271
}
12691272

1273+
/**
1274+
* Read an InputStream in its entirety as a string, closing the stream.
1275+
*
1276+
* @param is
1277+
* @return a String
1278+
* @throws IOException
1279+
*/
1280+
public static String streamToString(InputStream is) throws IOException {
1281+
return new String(streamToBytes(is));
1282+
}
1283+
1284+
/**
1285+
* Read an InputStream in its entirety as a byte array. Closes the stream.
1286+
*
1287+
* @param is
1288+
* @return a byte array
1289+
*/
1290+
public static byte[] streamToBytes(InputStream is) throws IOException {
1291+
byte[] bytes = Rdr.getLimitedStreamBytes(is, -1);
1292+
is.close();
1293+
return bytes;
1294+
}
1295+
12701296

12711297

12721298

sources/net.sf.j2s.java.core/src/swingjs/xml/JSJAXBClass.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package swingjs.xml;
22

3-
import java.io.BufferedInputStream;
3+
import java.io.IOException;
44
import java.io.InputStream;
55
import java.util.ArrayList;
66
import java.util.HashMap;
@@ -12,7 +12,7 @@
1212
import javax.xml.namespace.QName;
1313

1414
import javajs.util.PT;
15-
import javajs.util.Rdr;
15+
import swingjs.JSUtil;
1616
import swingjs.api.Interface;
1717

1818
class JSJAXBClass implements Cloneable {
@@ -111,11 +111,15 @@ private static void getPackageInfo(Class<?> javaClass) {
111111
// Keeping this simple for now.
112112
InputStream is = javaClass.getResourceAsStream("_$.js");
113113
if (is != null) {
114-
String data = Rdr.streamToUTF8String(new BufferedInputStream(is));
115-
packageAccessorType = parseAccessorType(data);
116-
data = PT.getQuotedAttribute(data, "namespace");
117-
if (data != null)
118-
packageNamespace = data;
114+
String data;
115+
try {
116+
data = JSUtil.streamToString(is);
117+
packageAccessorType = parseAccessorType(data);
118+
data = PT.getQuotedAttribute(data, "namespace");
119+
if (data != null)
120+
packageNamespace = data;
121+
} catch (IOException e) {
122+
}
119123
}
120124
}
121125
}

sources/net.sf.j2s.java.core/src/swingjs/xml/JSSAXParser.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,13 @@ public void parse(InputSource source, DefaultHandler handler) throws SAXExceptio
103103

104104
private String getString(InputSource source) throws IOException {
105105
Reader rdr = source.getCharacterStream();
106-
String[] data = new String[1];
107106
if (rdr == null) {
108-
InputStream bs = source.getByteStream();
109-
if (!(bs instanceof BufferedInputStream))
110-
bs = new BufferedInputStream(bs);
111-
data[0] = Rdr.fixUTF((byte[]) Rdr.getStreamAsBytes((BufferedInputStream) bs, null));
112-
} else {
113-
if (!(rdr instanceof BufferedReader))
114-
rdr = new BufferedReader(rdr);
115-
Rdr.readAllAsString((BufferedReader) rdr, -1, false, data, 0);
107+
return Rdr.fixUTF(JSUtil.streamToBytes(source.getByteStream()));
116108
}
109+
if (!(rdr instanceof BufferedReader))
110+
rdr = new BufferedReader(rdr);
111+
String[] data = new String[1];
112+
Rdr.readAllAsString((BufferedReader) rdr, -1, false, data, 0);
117113
return data[0];
118114
}
119115

0 commit comments

Comments
 (0)