Skip to content

Commit 8933628

Browse files
authored
Merge pull request #196 from BobHanson/master
StreamToBytes and StreamToString moved to javajs.util.reader
2 parents d66649c + 8429329 commit 8933628

File tree

11 files changed

+38
-37
lines changed

11 files changed

+38
-37
lines changed
15 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20210728170251
1+
20210728172208
15 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20210728170251
1+
20210728172208
15 Bytes
Binary file not shown.

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,32 @@ public String readNextLine() throws Exception {
9090
return reader.readLine();
9191
}
9292

93-
public static Map<String, Object> readCifData(GenericCifDataParser parser, BufferedReader br) {
94-
return parser.set(null, br, false).getAllCifData();
95-
}
93+
/**
94+
* Read an InputStream in its entirety as a byte array, closing the stream.
95+
*
96+
* @param is
97+
* @return a byte array
98+
*/
99+
public static byte[] streamToBytes(InputStream is) throws IOException {
100+
byte[] bytes = getLimitedStreamBytes(is, -1);
101+
is.close();
102+
return bytes;
103+
}
104+
105+
/**
106+
* Read an InputStream in its entirety as a string, closing the stream.
107+
*
108+
* @param is
109+
* @return a String
110+
* @throws IOException
111+
*/
112+
public static String streamToString(InputStream is) throws IOException {
113+
return new String(streamToBytes(is));
114+
}
115+
116+
public static Map<String, Object> readCifData(GenericCifDataParser parser, BufferedReader br) {
117+
return parser.set(null, br, false).getAllCifData();
118+
}
96119

97120
///////////
98121

@@ -327,7 +350,7 @@ public static BufferedReader getBR(String string) {
327350

328351
public static BufferedInputStream toBIS(Object o) {
329352
return (AU.isAB(o) ? getBIS((byte[]) o)
330-
: o instanceof SB ? getBIS(Rdr.getBytesFromSB((SB) o))
353+
: o instanceof SB ? getBIS(getBytesFromSB((SB) o))
331354
: o instanceof String ? getBIS(((String) o).getBytes()) : null);
332355
}
333356

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

Lines changed: 2 additions & 25 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 ? streamToBytes(stream) : streamToString(stream));
210+
return (asBytes ? Rdr.streamToBytes(stream) : Rdr.streamToString(stream));
211211
} catch (Exception e) {
212212
}
213213
}
@@ -253,7 +253,7 @@ static String ensureString(Object data) {
253253
return data.toString();
254254
if (data instanceof InputStream)
255255
try {
256-
return streamToString((InputStream) data);
256+
return Rdr.streamToString((InputStream) data);
257257
} catch (IOException e) {
258258
}
259259
return null;
@@ -1270,29 +1270,6 @@ public String getJ2SPath() {
12701270
return (String) getAppletAttribute("_j2sFullPath");
12711271
}
12721272

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-
12961273

12971274

12981275

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import javax.xml.namespace.QName;
1313

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

1818
class JSJAXBClass implements Cloneable {
@@ -113,7 +113,7 @@ private static void getPackageInfo(Class<?> javaClass) {
113113
if (is != null) {
114114
String data;
115115
try {
116-
data = JSUtil.streamToString(is);
116+
data = Rdr.streamToString(is);
117117
packageAccessorType = parseAccessorType(data);
118118
data = PT.getQuotedAttribute(data, "namespace");
119119
if (data != null)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public void parse(InputSource source, DefaultHandler handler) throws SAXExceptio
104104
private String getString(InputSource source) throws IOException {
105105
Reader rdr = source.getCharacterStream();
106106
if (rdr == null) {
107-
return Rdr.fixUTF(JSUtil.streamToBytes(source.getByteStream()));
107+
return Rdr.fixUTF(Rdr.streamToBytes(source.getByteStream()));
108108
}
109109
if (!(rdr instanceof BufferedReader))
110110
rdr = new BufferedReader(rdr);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.List;
1313
import java.util.Map;
1414

15+
import javajs.util.Rdr;
1516
import swingjs.JSUtil;
1617

1718
public class Test_JSON extends Test_ {
@@ -43,7 +44,7 @@ public static void main(String[] args) {
4344
* data = data["1cbs"][0].title;
4445
*/
4546
if (data instanceof InputStream) {
46-
data = JSUtil.streamToString((InputStream) data);
47+
data = Rdr.streamToString((InputStream) data);
4748
}
4849
System.out.println(data);
4950

@@ -52,7 +53,7 @@ public static void main(String[] args) {
5253
JSUtil.setAjax(url);
5354
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
5455
data = connection.getInputStream();
55-
data = JSUtil.streamToString((InputStream) data);
56+
data = Rdr.streamToString((InputStream) data);
5657
System.out.println(data);
5758

5859

0 commit comments

Comments
 (0)