diff options
author | Luck <git@lucko.me> | 2021-03-23 00:52:44 +0000 |
---|---|---|
committer | Luck <git@lucko.me> | 2021-03-23 00:52:44 +0000 |
commit | 0fd7d30f2a9027c2bd9df3215759c6d91d110acc (patch) | |
tree | bdf02fa8b7c6dfc7dc6db5eaaabeb1e17189d420 /spark-common/src/main/java/me/lucko/spark/common/util | |
parent | 9766754d28fcbca1ccbeefc11ef7a88a4e3d7946 (diff) | |
download | spark-0fd7d30f2a9027c2bd9df3215759c6d91d110acc.tar.gz spark-0fd7d30f2a9027c2bd9df3215759c6d91d110acc.tar.bz2 spark-0fd7d30f2a9027c2bd9df3215759c6d91d110acc.zip |
Refactor and tidy up, more consistent code style
Diffstat (limited to 'spark-common/src/main/java/me/lucko/spark/common/util')
5 files changed, 7 insertions, 60 deletions
diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/AbstractHttpClient.java b/spark-common/src/main/java/me/lucko/spark/common/util/AbstractHttpClient.java index 1ff169d..8ece3d4 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/util/AbstractHttpClient.java +++ b/spark-common/src/main/java/me/lucko/spark/common/util/AbstractHttpClient.java @@ -38,6 +38,7 @@ public class AbstractHttpClient { protected Response makeHttpRequest(Request request) throws IOException { Response response = this.okHttp.newCall(request).execute(); if (!response.isSuccessful()) { + response.close(); throw new RuntimeException("Request was unsuccessful: " + response.code() + " - " + response.message()); } return response; diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/BytebinClient.java b/spark-common/src/main/java/me/lucko/spark/common/util/BytebinClient.java index ff8f4e3..9202303 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/util/BytebinClient.java +++ b/spark-common/src/main/java/me/lucko/spark/common/util/BytebinClient.java @@ -51,11 +51,7 @@ public class BytebinClient extends AbstractHttpClient { */ public BytebinClient(OkHttpClient okHttpClient, String url, String userAgent) { super(okHttpClient); - if (url.endsWith("/")) { - this.url = url; - } else { - this.url = url + "/"; - } + this.url = url + (url.endsWith("/") ? "" : "/"); this.userAgent = userAgent; } @@ -64,11 +60,10 @@ public class BytebinClient extends AbstractHttpClient { * * @param buf the compressed content * @param contentType the type of the content - * @param allowModification if the paste should be modifiable * @return the key of the resultant content * @throws IOException if an error occurs */ - public Content postContent(byte[] buf, MediaType contentType, boolean allowModification) throws IOException { + public Content postContent(byte[] buf, MediaType contentType) throws IOException { RequestBody body = RequestBody.create(contentType, buf); Request.Builder requestBuilder = new Request.Builder() @@ -76,69 +71,21 @@ public class BytebinClient extends AbstractHttpClient { .header("User-Agent", this.userAgent) .header("Content-Encoding", "gzip"); - if (allowModification) { - requestBuilder.header("Allow-Modification", "true"); - } - Request request = requestBuilder.post(body).build(); try (Response response = makeHttpRequest(request)) { String key = response.header("Location"); if (key == null) { throw new IllegalStateException("Key not returned"); } - - if (allowModification) { - String modificationKey = response.header("Modification-Key"); - if (modificationKey == null) { - throw new IllegalStateException("Modification key not returned"); - } - return new Content(key, modificationKey); - } else { - return new Content(key); - } - } - } - - /** - * PUTs modified GZIP compressed content to bytebin in place of existing content. - * - * @param existingContent the existing content - * @param buf the compressed content to put - * @param contentType the type of the content - * @throws IOException if an error occurs - */ - public void modifyContent(Content existingContent, byte[] buf, MediaType contentType) throws IOException { - if (!existingContent.modifiable) { - throw new IllegalArgumentException("Existing content is not modifiable"); + return new Content(key); } - - RequestBody body = RequestBody.create(contentType, buf); - - Request.Builder requestBuilder = new Request.Builder() - .url(this.url + existingContent.key()) - .header("User-Agent", this.userAgent) - .header("Content-Encoding", "gzip") - .header("Modification-Key", existingContent.modificationKey); - - Request request = requestBuilder.put(body).build(); - makeHttpRequest(request).close(); } public static final class Content { private final String key; - private final boolean modifiable; - private final String modificationKey; Content(String key) { this.key = key; - this.modifiable = false; - this.modificationKey = null; - } - - Content(String key, String modificationKey) { - this.key = key; - this.modifiable = true; - this.modificationKey = modificationKey; } public String key() { diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/MethodDisambiguator.java b/spark-common/src/main/java/me/lucko/spark/common/util/MethodDisambiguator.java index e6311e0..2e113a9 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/util/MethodDisambiguator.java +++ b/spark-common/src/main/java/me/lucko/spark/common/util/MethodDisambiguator.java @@ -23,7 +23,9 @@ package me.lucko.spark.common.util; import com.google.common.collect.ImmutableListMultimap; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ListMultimap; + import me.lucko.spark.common.sampler.node.StackTraceNode; + import org.objectweb.asm.ClassReader; import org.objectweb.asm.ClassVisitor; import org.objectweb.asm.Label; diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/RollingAverage.java b/spark-common/src/main/java/me/lucko/spark/common/util/RollingAverage.java index 8160e96..2c6219a 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/util/RollingAverage.java +++ b/spark-common/src/main/java/me/lucko/spark/common/util/RollingAverage.java @@ -23,9 +23,7 @@ package me.lucko.spark.common.util; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.ArrayDeque; -import java.util.ArrayList; import java.util.Arrays; -import java.util.List; import java.util.Queue; public class RollingAverage { diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/ThreadFinder.java b/spark-common/src/main/java/me/lucko/spark/common/util/ThreadFinder.java index 0d1cbd3..66aa941 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/util/ThreadFinder.java +++ b/spark-common/src/main/java/me/lucko/spark/common/util/ThreadFinder.java @@ -21,7 +21,6 @@ package me.lucko.spark.common.util; import java.util.Arrays; -import java.util.Objects; import java.util.stream.Stream; /** @@ -56,7 +55,7 @@ public final class ThreadFinder { threads = new Thread[threads.length * 2]; } this.approxActiveCount = len; - return Arrays.stream(threads).filter(Objects::nonNull); + return Arrays.stream(threads, 0, len); } } |