Skip to content

Commit 4efbd7b

Browse files
Add readAll method to readme
1 parent 2cf99b4 commit 4efbd7b

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@ Library like J2ME's Connector.open() but with TLS 1.2 automatically.
44
### Usage
55
HTTPS:
66
```java
7+
// Somewhere
8+
private static byte[] readAll(InputStream in, int max) throws IOException {
9+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
10+
byte[] buf = new byte[512];
11+
int total = 0;
12+
while (true) {
13+
int want = Math.min(buf.length, max - total);
14+
if (want <= 0) break;
15+
int n = in.read(buf, 0, want);
16+
if (n == -1) break;
17+
bos.write(buf, 0, n);
18+
total += n;
19+
}
20+
return bos.toByteArray();
21+
}
22+
723
HttpConnection hc = (HttpConnection) ModernConnector.open("https://cloudflare.com/cdn-cgi/trace");
824
hc.setRequestMethod(HttpConnection.GET);
925
hc.setRequestProperty("Accept", "*/*");

0 commit comments

Comments
 (0)