mirror of
https://github.com/FalsePattern/FalsePatternLib.git
synced 2026-11-15 21:54:45 +00:00
added update checks and separate internet library
This commit is contained in:
7 files changed
+207
-101
No files matched your search
@@ -0,0 +1,40 @@
|
||||
package com.falsepattern.lib.internal;
|
||||
|
||||
import lombok.val;
|
||||
import lombok.var;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class Internet {
|
||||
public static void connect(URL URL, Consumer<Exception> onError, Consumer<InputStream> onSuccess) {
|
||||
try {
|
||||
val connection = (HttpURLConnection) URL.openConnection();
|
||||
connection.setConnectTimeout(3500);
|
||||
connection.setReadTimeout(5000);
|
||||
connection.setRequestProperty("User-Agent", "FalsePatternLib Internet Connector");
|
||||
if (connection.getResponseCode() != 200) {
|
||||
onError.accept(new Exception("HTTP response code " + connection.getResponseCode()));
|
||||
} else {
|
||||
onSuccess.accept(connection.getInputStream());
|
||||
}
|
||||
connection.disconnect();
|
||||
} catch (Exception e) {
|
||||
onError.accept(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void transferAndClose(InputStream is, OutputStream target) throws IOException {
|
||||
var bytesRead = 0;
|
||||
|
||||
byte[] smallBuffer = new byte[4096];
|
||||
while ((bytesRead = is.read(smallBuffer)) >= 0) {
|
||||
target.write(smallBuffer, 0, bytesRead);
|
||||
}
|
||||
target.close();
|
||||
is.close();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user