diff --git a/src/main/java/io/vertx/redis/client/Request.java b/src/main/java/io/vertx/redis/client/Request.java index b4a5117d..82b27271 100644 --- a/src/main/java/io/vertx/redis/client/Request.java +++ b/src/main/java/io/vertx/redis/client/Request.java @@ -28,8 +28,8 @@ import java.util.Map; /** - * Builder for REDIS requests that will be encoded according to the RESP protocol was introduced in Redis 1.2. - * Which became the standard way for talking with the Redis server in Redis 2.0. + * Builder for Redis requests that will be encoded according to the RESP protocol. + * RESP was introduced in Redis 1.2 and became the standard way for talking with the Redis server in Redis 2.0. *
* Redis protocol documentation states: * @@ -45,7 +45,7 @@ public interface Request { /** - * Creates a new request command. Requests can be reused to avoid GC. + * Creates a new request. Requests can be reused to avoid GC. * * @param command the command to use * @return a new request instance @@ -55,9 +55,10 @@ static Request cmd(Command command) { } /** - * Creates a new request command only with simple types: {@link Number}, {@link Boolean}, {@link String}, - * {@code byte[]} or {@link Buffer}. This factory reduces the GC as it allocates the arguments array with - * the right size. + * Creates a new request only with simple types of arguments: {@link Number}, {@link Boolean}, + * {@link String}, {@code byte[]} or {@link Buffer}. Other kinds of arguments throw an exception. + *
+ * This factory reduces the GC pressure as it allocates the arguments array with the right size. * * @param command the command to use * @param args the fixed list of arguments diff --git a/src/main/java/io/vertx/redis/client/impl/RequestImpl.java b/src/main/java/io/vertx/redis/client/impl/RequestImpl.java index c0bbe232..d9a3bbfd 100644 --- a/src/main/java/io/vertx/redis/client/impl/RequestImpl.java +++ b/src/main/java/io/vertx/redis/client/impl/RequestImpl.java @@ -28,7 +28,6 @@ public final class RequestImpl implements Request { private static final byte[] EMPTY_BULK = "$0\r\n\r\n".getBytes(StandardCharsets.ISO_8859_1); private static final byte[] EMPTY_BYTES = new byte[0]; - private static final byte[] NULL_BULK = "$4\r\nnull\r\n".getBytes(StandardCharsets.ISO_8859_1); private static final byte[] EOL = "\r\n".getBytes(StandardCharsets.ISO_8859_1); private static final byte[] TRUE = new byte[]{'t'}; private static final byte[] FALSE = new byte[]{'f'}; @@ -48,39 +47,31 @@ public RequestImpl(Command cmd) { public RequestImpl(Command cmd, Object[] args) { this.cmd = (CommandImpl) cmd; - if (args != null) { - final int len = args.length; - if (len > 0) { - for (int i = 0; i < args.length; i++) { - final Object o = args[i]; - if (o != null) { - if (o instanceof Number) { - args[i] = o.toString().getBytes(StandardCharsets.US_ASCII); - continue; - } - if (o instanceof Boolean) { - args[i] = ((Boolean) o) ? TRUE : FALSE; - continue; - } - if (o instanceof String) { - args[i] = ((String) o).getBytes(StandardCharsets.UTF_8); - continue; - } - if (o instanceof byte[]) { - continue; - } - if (o instanceof Buffer) { - args[i] = ((Buffer) o).getBytes(); - continue; - } - throw new IllegalArgumentException("Unsupported argument type: " + o.getClass()); - } - } - this.args = (List) Arrays.asList(args); - return; + + if (args == null) { + this.args = Collections.emptyList(); + return; + } + + for (int i = 0; i < args.length; i++) { + final Object o = args[i]; + if (o == null) { + throw new IllegalArgumentException("Null argument at index " + i); + } else if (o instanceof Number) { + args[i] = o.toString().getBytes(StandardCharsets.US_ASCII); + } else if (o instanceof Boolean) { + args[i] = ((Boolean) o) ? TRUE : FALSE; + } else if (o instanceof String) { + args[i] = ((String) o).getBytes(StandardCharsets.UTF_8); + } else if (o instanceof byte[]) { + // already OK + } else if (o instanceof Buffer) { + args[i] = ((Buffer) o).getBytes(); + } else { + throw new IllegalArgumentException("Unsupported argument type at index " + i + ": " + o.getClass()); } } - this.args = Collections.emptyList(); + this.args = (List) Arrays.asList(args); } @Override @@ -146,11 +137,6 @@ Buffer encode(Buffer buffer) { .appendBytes(cmd.getBytes()); for (final byte[] arg : args) { - if (arg == null) { - buffer.appendBytes(NULL_BULK); - continue; - } - if (arg.length == 0) { buffer.appendBytes(EMPTY_BULK); continue;