From e5460d4aac7ffcd910528fb7eec1ff0d212a2389 Mon Sep 17 00:00:00 2001 From: Adam Peck Date: Thu, 27 Mar 2025 23:33:23 -0600 Subject: [PATCH 1/2] Remove encode conditions by using a consumer pattern --- .../java/com/dampcake/bencode/Bencode.java | 37 +++++++++---------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/dampcake/bencode/Bencode.java b/src/main/java/com/dampcake/bencode/Bencode.java index 89788bd..d8b1800 100644 --- a/src/main/java/com/dampcake/bencode/Bencode.java +++ b/src/main/java/com/dampcake/bencode/Bencode.java @@ -17,6 +17,7 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.nio.charset.Charset; import java.util.Map; @@ -51,7 +52,7 @@ public final class Bencode { /** * Create a new Bencoder using the default {@link Charset} (UTF-8) and useBytes as false. - * + * * @see #Bencode(Charset, boolean) */ public Bencode() { @@ -64,7 +65,7 @@ public Bencode() { * @param charset the {@link Charset} to use * * @throws NullPointerException if the {@link Charset} passed is null - * + * * @see #Bencode(Charset, boolean) */ public Bencode(final Charset charset) { @@ -74,8 +75,8 @@ public Bencode(final Charset charset) { /** * Creates a new Bencoder using the boolean passed to control String parsing. * - * @param useBytes {@link #Bencode(Charset, boolean)} - * + * @param useBytes {@link #Bencode(Charset, boolean)} + * * @since 1.3 */ public Bencode(final boolean useBytes) { @@ -84,7 +85,7 @@ public Bencode(final boolean useBytes) { /** * Creates a new Bencoder using the {@link Charset} passed for encoding/decoding and boolean passed to control String parsing. - * + * * If useBytes is false, then dictionary values that contain byte string data will be coerced to a {@link String}. * if useBytes is true, then dictionary values that contain byte string data will be coerced to a {@link java.nio.ByteBuffer}. * @@ -92,7 +93,7 @@ public Bencode(final boolean useBytes) { * @param useBytes true to have dictionary byte data to stay as bytes * * @throws NullPointerException if the {@link Charset} passed is null - * + * * @since 1.3 */ public Bencode(final Charset charset, final boolean useBytes) { @@ -176,7 +177,7 @@ public T decode(final byte[] bytes, final Type type) { public byte[] encode(final String s) { if (s == null) throw new NullPointerException("s cannot be null"); - return encode(s, Type.STRING); + return encode(bencode -> bencode.writeString(s)); } /** @@ -194,7 +195,7 @@ public byte[] encode(final String s) { public byte[] encode(final Number n) { if (n == null) throw new NullPointerException("n cannot be null"); - return encode(n, Type.NUMBER); + return encode(bencode -> bencode.writeNumber(n)); } /** @@ -214,7 +215,7 @@ public byte[] encode(final Number n) { public byte[] encode(final Iterable l) { if (l == null) throw new NullPointerException("l cannot be null"); - return encode(l, Type.LIST); + return encode(bencode -> bencode.writeList(l)); } /** @@ -234,25 +235,23 @@ public byte[] encode(final Iterable l) { public byte[] encode(final Map m) { if (m == null) throw new NullPointerException("m cannot be null"); - return encode(m, Type.DICTIONARY); + return encode(bencode -> bencode.writeDictionary(m)); } - private byte[] encode(final Object o, final Type type) { + private byte[] encode(final ThrowingConsumer function) { ByteArrayOutputStream out = new ByteArrayOutputStream(); try (BencodeOutputStream bencode = new BencodeOutputStream(out, charset)) { - if (type == Type.STRING) - bencode.writeString((String) o); - else if (type == Type.NUMBER) - bencode.writeNumber((Number) o); - else if (type == Type.LIST) - bencode.writeList((Iterable) o); - else if (type == Type.DICTIONARY) - bencode.writeDictionary((Map) o); + function.accept(bencode); } catch (Throwable t) { throw new BencodeException("Exception thrown during encoding", t); } return out.toByteArray(); } + + @FunctionalInterface + public interface ThrowingConsumer { + public void accept(T t) throws IOException; + } } From ab4d4ab03da273a733544167d456fff9ee95bb72 Mon Sep 17 00:00:00 2001 From: Adam Peck Date: Thu, 27 Mar 2025 23:33:51 -0600 Subject: [PATCH 2/2] Revert whitespace changes --- src/main/java/com/dampcake/bencode/Bencode.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/dampcake/bencode/Bencode.java b/src/main/java/com/dampcake/bencode/Bencode.java index d8b1800..e72b2df 100644 --- a/src/main/java/com/dampcake/bencode/Bencode.java +++ b/src/main/java/com/dampcake/bencode/Bencode.java @@ -52,7 +52,7 @@ public final class Bencode { /** * Create a new Bencoder using the default {@link Charset} (UTF-8) and useBytes as false. - * + * * @see #Bencode(Charset, boolean) */ public Bencode() { @@ -65,7 +65,7 @@ public Bencode() { * @param charset the {@link Charset} to use * * @throws NullPointerException if the {@link Charset} passed is null - * + * * @see #Bencode(Charset, boolean) */ public Bencode(final Charset charset) { @@ -75,8 +75,8 @@ public Bencode(final Charset charset) { /** * Creates a new Bencoder using the boolean passed to control String parsing. * - * @param useBytes {@link #Bencode(Charset, boolean)} - * + * @param useBytes {@link #Bencode(Charset, boolean)} + * * @since 1.3 */ public Bencode(final boolean useBytes) { @@ -85,7 +85,7 @@ public Bencode(final boolean useBytes) { /** * Creates a new Bencoder using the {@link Charset} passed for encoding/decoding and boolean passed to control String parsing. - * + * * If useBytes is false, then dictionary values that contain byte string data will be coerced to a {@link String}. * if useBytes is true, then dictionary values that contain byte string data will be coerced to a {@link java.nio.ByteBuffer}. * @@ -93,7 +93,7 @@ public Bencode(final boolean useBytes) { * @param useBytes true to have dictionary byte data to stay as bytes * * @throws NullPointerException if the {@link Charset} passed is null - * + * * @since 1.3 */ public Bencode(final Charset charset, final boolean useBytes) {