diff --git a/core/src/main/java/com/boydti/fawe/util/HastebinUtility.java b/core/src/main/java/com/boydti/fawe/util/HastebinUtility.java index 0c110ef1..26535e86 100644 --- a/core/src/main/java/com/boydti/fawe/util/HastebinUtility.java +++ b/core/src/main/java/com/boydti/fawe/util/HastebinUtility.java @@ -1,14 +1,10 @@ package com.boydti.fawe.util; -import com.boydti.fawe.object.RunnableVal; -import java.io.BufferedReader; -import java.io.DataOutputStream; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.io.InputStreamReader; +import java.io.*; import java.net.HttpURLConnection; import java.net.URL; +import java.util.ArrayList; +import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -17,15 +13,17 @@ public class HastebinUtility { public static final String BIN_URL = "http://hastebin.com/documents", USER_AGENT = "Mozilla/5.0"; public static final Pattern PATTERN = Pattern.compile("\\{\"key\":\"([\\S\\s]*)\"\\}"); - public static String upload(final RunnableVal writeTask) throws IOException { + public static String upload(final String string) throws IOException { final URL url = new URL(BIN_URL); final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("User-Agent", USER_AGENT); connection.setDoOutput(true); - try (DataOutputStream os = new DataOutputStream(connection.getOutputStream())) { - writeTask.run(os); + + try (DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream())) { + outputStream.write(string.getBytes()); + outputStream.flush(); } StringBuilder response; @@ -46,30 +44,20 @@ public class HastebinUtility { } } - public static String upload(final String s) throws IOException { - return upload(new RunnableVal() { - @Override - public void run(DataOutputStream value) { - try { - value.writeChars(s); - } catch (IOException e) { - e.printStackTrace(); - } - } - }); - } - public static String upload(final File file) throws IOException { final StringBuilder content = new StringBuilder(); + List lines = new ArrayList<>(); try (BufferedReader reader = new BufferedReader(new FileReader(file))) { String line; int i = 0; - while ((line = reader.readLine()) != null && i++ < 1000) { - content.append(line).append("\n"); + while ((line = reader.readLine()) != null) { + lines.add(line); } } + for (int i = Math.max(0, lines.size() - 1000); i < lines.size(); i++) { + content.append(lines.get(i)).append("\n"); + } return upload(content.toString()); - } } diff --git a/core/src/main/java/com/boydti/fawe/util/task/TaskBuilder.java b/core/src/main/java/com/boydti/fawe/util/task/TaskBuilder.java index b60aecc5..66208896 100644 --- a/core/src/main/java/com/boydti/fawe/util/task/TaskBuilder.java +++ b/core/src/main/java/com/boydti/fawe/util/task/TaskBuilder.java @@ -8,6 +8,7 @@ import com.boydti.fawe.util.SetQueue; import com.boydti.fawe.util.TaskManager; import java.util.ArrayDeque; import java.util.ArrayList; +import java.util.Objects; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.TimeUnit; @@ -162,6 +163,50 @@ public class TaskBuilder extends Metadatable { return this; } + public TaskBuilder abortIfTrue(final Runnable run) { + tasks.add(RunnableTask.adapt(new Task() { + @Override + public Boolean run(Boolean previous) { + if (previous == Boolean.TRUE) run.run(); + return previous == Boolean.TRUE; + } + }, TaskType.ABORT)); + return this; + } + + public TaskBuilder abortIfNull(final Runnable run) { + tasks.add(RunnableTask.adapt(new Task() { + @Override + public Boolean run(Object previous) { + if (previous == null) run.run(); + return previous == null; + } + }, TaskType.ABORT)); + return this; + } + + public TaskBuilder abortIfEqual(final Runnable run, final Object other) { + tasks.add(RunnableTask.adapt(new Task() { + @Override + public Boolean run(Object previous) { + if (Objects.equals(previous, other)) run.run(); + return Objects.equals(previous, other); + } + }, TaskType.ABORT)); + return this; + } + + public TaskBuilder abortIfNotEqual(final Runnable run, final Object other) { + tasks.add(RunnableTask.adapt(new Task() { + @Override + public Boolean run(Object previous) { + if (!Objects.equals(previous, other)) run.run(); + return !Objects.equals(previous, other); + } + }, TaskType.ABORT)); + return this; + } + /** * Have all async tasks run on a new thread
* - As opposed to trying to using the current thread @@ -283,15 +328,29 @@ public class TaskBuilder extends Metadatable { if (task.isAborted()) { return; } - } catch (Throwable e) { + } catch (TaskAbortException abort) { + return; + } catch (Throwable e1) { if (handler != null) { - handler.uncaughtException(Thread.currentThread(), e); + try { + handler.uncaughtException(Thread.currentThread(), e1); + } catch (Throwable e2) { + e1.printStackTrace(); + e2.printStackTrace(); + } } return; } } } + public static final class TaskAbortException extends RuntimeException { + @Override + public synchronized Throwable fillInStackTrace() { + return this; + } + } + private FaweQueue queue; private long last; private long start;