diff --git a/src/main/java/net/spy/memcached/AddrUtil.java b/src/main/java/net/spy/memcached/AddrUtil.java index 4a60adae7..0eb89eb47 100644 --- a/src/main/java/net/spy/memcached/AddrUtil.java +++ b/src/main/java/net/spy/memcached/AddrUtil.java @@ -21,29 +21,21 @@ private AddrUtil() { * * Note that colon-delimited IPv6 is also supported. * For example: ::1:11211 - * @param s The {@link java.util.List} of {@link java.lang.String} + * @param hosts The {@link java.util.List} of {@link java.lang.String} * containing {@code host:port} * @return The {@link java.util.List} of {@link java.net.InetSocketAddress} */ - public static List getAddresses(List s) { - if (s == null) { - throw new NullPointerException("Null host list"); + public static List getAddresses(List hosts) { + if (hosts == null) { + throw new IllegalArgumentException("Hosts must not be null"); } - ArrayList addrs = - new ArrayList<>(); - if (s.isEmpty()) { - return addrs; - } - - for (String hoststuff : s) { - hoststuff = hoststuff.trim(); - if (hoststuff.equals("")) { - continue; + ArrayList addrs = new ArrayList<>(); + for (String host : hosts) { + if (!host.trim().isEmpty()) { + addrs.add(getAddress(host)); } - addrs.add(getAddress(hoststuff)); } - assert !addrs.isEmpty() : "No addrs found"; return Collections.unmodifiableList(addrs); } diff --git a/src/main/java/net/spy/memcached/protocol/ascii/BTreeFindPositionWithGetOperationImpl.java b/src/main/java/net/spy/memcached/protocol/ascii/BTreeFindPositionWithGetOperationImpl.java index b59900b8a..e7e5a8bdf 100644 --- a/src/main/java/net/spy/memcached/protocol/ascii/BTreeFindPositionWithGetOperationImpl.java +++ b/src/main/java/net/spy/memcached/protocol/ascii/BTreeFindPositionWithGetOperationImpl.java @@ -105,14 +105,12 @@ public void handleLine(String line) { */ if (line.startsWith("VALUE ")) { String[] stuff = line.split(" "); - assert stuff.length == 5; position = Integer.parseInt(stuff[1]); flags = Integer.parseInt(stuff[2]); count = Integer.parseInt(stuff[3]); index = Integer.parseInt(stuff[4]); - assert count > 0; // position counter pos = position - index; posDiff = 1; @@ -171,13 +169,6 @@ public void handleRead(ByteBuffer bb) { return; } - // Read data - assert key != null; - assert data != null; - // This will be the case, because we'll clear them when it's not. - assert readOffset <= data.length - : "readOffset is " + readOffset + " data.length is " + data.length; - getLogger().debug("readOffset: %d, length: %d", readOffset, data.length); if (lookingFor == '\0') { @@ -205,19 +196,14 @@ public void handleRead(ByteBuffer bb) { if (lookingFor != '\0' && bb.hasRemaining()) { do { byte tmp = bb.get(); - assert tmp == lookingFor : "Expecting " + lookingFor + ", got " - + (char) tmp; + if (tmp != lookingFor) { + throw new IllegalStateException("Expecting " + lookingFor + ", got " + (char) tmp); + } - switch (lookingFor) { - case '\r': - lookingFor = '\n'; - break; - case '\n': - lookingFor = '\0'; - break; - default: - assert false : "Looking for unexpected char: " - + (char) lookingFor; + if (lookingFor == '\r') { + lookingFor = '\n'; + } else { // lookingFor == '\n'; + lookingFor = '\0'; } } while (lookingFor != '\0' && bb.hasRemaining()); diff --git a/src/main/java/net/spy/memcached/protocol/ascii/BTreeGetBulkOperationImpl.java b/src/main/java/net/spy/memcached/protocol/ascii/BTreeGetBulkOperationImpl.java index 986c17499..ba67b17c1 100644 --- a/src/main/java/net/spy/memcached/protocol/ascii/BTreeGetBulkOperationImpl.java +++ b/src/main/java/net/spy/memcached/protocol/ascii/BTreeGetBulkOperationImpl.java @@ -161,14 +161,6 @@ public void handleRead(ByteBuffer bb) { return; } - // Read data - // assert key != null; - assert data != null; - - // This will be the case, because we'll clear them when it's not. - assert readOffset <= data.length : "readOffset is " + readOffset - + " data.length is " + data.length; - getLogger().debug("readOffset: %d, length: %d", readOffset, data.length); if (lookingFor == '\0') { @@ -191,19 +183,14 @@ public void handleRead(ByteBuffer bb) { if (lookingFor != '\0' && bb.hasRemaining()) { do { byte tmp = bb.get(); - assert tmp == lookingFor : "Expecting " + lookingFor + ", got " - + (char) tmp; + if (tmp != lookingFor) { + throw new IllegalStateException("Expecting " + lookingFor + ", got " + (char) tmp); + } - switch (lookingFor) { - case '\r': - lookingFor = '\n'; - break; - case '\n': - lookingFor = '\0'; - break; - default: - assert false : "Looking for unexpected char: " - + (char) lookingFor; + if (lookingFor == '\r') { + lookingFor = '\n'; + } else { // lookingFor == '\n'; + lookingFor = '\0'; } } while (lookingFor != '\0' && bb.hasRemaining()); diff --git a/src/main/java/net/spy/memcached/protocol/ascii/BTreeGetByPositionOperationImpl.java b/src/main/java/net/spy/memcached/protocol/ascii/BTreeGetByPositionOperationImpl.java index da6a6cfc1..2d3f6b621 100644 --- a/src/main/java/net/spy/memcached/protocol/ascii/BTreeGetByPositionOperationImpl.java +++ b/src/main/java/net/spy/memcached/protocol/ascii/BTreeGetByPositionOperationImpl.java @@ -102,8 +102,6 @@ public void handleLine(String line) { */ if (line.startsWith("VALUE ")) { String[] stuff = line.split(" "); - assert stuff.length == 3; - assert "VALUE".equals(stuff[0]); flags = Integer.parseInt(stuff[1]); count = Integer.parseInt(stuff[2]); @@ -173,13 +171,6 @@ public void handleRead(ByteBuffer bb) { return; } - // Read data - assert key != null; - assert data != null; - // This will be the case, because we'll clear them when it's not. - assert readOffset <= data.length - : "readOffset is " + readOffset + " data.length is " + data.length; - getLogger().debug("readOffset: %d, length: %d", readOffset, data.length); if (lookingFor == '\0') { @@ -207,19 +198,14 @@ public void handleRead(ByteBuffer bb) { if (lookingFor != '\0' && bb.hasRemaining()) { do { byte tmp = bb.get(); - assert tmp == lookingFor : "Expecting " + lookingFor + ", got " - + (char) tmp; + if (tmp != lookingFor) { + throw new IllegalStateException("Expecting " + lookingFor + ", got " + (char) tmp); + } - switch (lookingFor) { - case '\r': - lookingFor = '\n'; - break; - case '\n': - lookingFor = '\0'; - break; - default: - assert false : "Looking for unexpected char: " - + (char) lookingFor; + if (lookingFor == '\r') { + lookingFor = '\n'; + } else { // lookingFor == '\n'; + lookingFor = '\0'; } } while (lookingFor != '\0' && bb.hasRemaining()); diff --git a/src/main/java/net/spy/memcached/protocol/ascii/BTreeInsertAndGetOperationImpl.java b/src/main/java/net/spy/memcached/protocol/ascii/BTreeInsertAndGetOperationImpl.java index bb430f52b..cda99068f 100644 --- a/src/main/java/net/spy/memcached/protocol/ascii/BTreeInsertAndGetOperationImpl.java +++ b/src/main/java/net/spy/memcached/protocol/ascii/BTreeInsertAndGetOperationImpl.java @@ -135,8 +135,6 @@ public void handleLine(String line) { */ if (line.startsWith("VALUE ")) { String[] stuff = line.split(" "); - assert stuff.length == 3; - assert "VALUE".equals(stuff[0]); flags = Integer.parseInt(stuff[1]); count = Integer.parseInt(stuff[2]); @@ -200,13 +198,6 @@ public void handleRead(ByteBuffer bb) { return; } - // Read data - assert key != null; - assert data != null; - // This will be the case, because we'll clear them when it's not. - assert readOffset <= data.length - : "readOffset is " + readOffset + " data.length is " + data.length; - getLogger().debug("readOffset: %d, length: %d", readOffset, data.length); if (lookingFor == '\0') { @@ -231,19 +222,14 @@ public void handleRead(ByteBuffer bb) { if (lookingFor != '\0' && bb.hasRemaining()) { do { byte tmp = bb.get(); - assert tmp == lookingFor : "Expecting " + lookingFor + ", got " - + (char) tmp; + if (tmp != lookingFor) { + throw new IllegalStateException("Expecting " + lookingFor + ", got " + (char) tmp); + } - switch (lookingFor) { - case '\r': - lookingFor = '\n'; - break; - case '\n': - lookingFor = '\0'; - break; - default: - assert false : "Looking for unexpected char: " - + (char) lookingFor; + if (lookingFor == '\r') { + lookingFor = '\n'; + } else { // lookingFor == '\n'; + lookingFor = '\0'; } } while (lookingFor != '\0' && bb.hasRemaining()); diff --git a/src/main/java/net/spy/memcached/protocol/ascii/BTreeSortMergeGetOperationImpl.java b/src/main/java/net/spy/memcached/protocol/ascii/BTreeSortMergeGetOperationImpl.java index fcca0cf0e..49c1d1af8 100644 --- a/src/main/java/net/spy/memcached/protocol/ascii/BTreeSortMergeGetOperationImpl.java +++ b/src/main/java/net/spy/memcached/protocol/ascii/BTreeSortMergeGetOperationImpl.java @@ -105,7 +105,6 @@ public void handleLine(String line) { readState = ReadState.ELEMENTS; String[] stuff = line.split(" "); - assert "ELEMENTS".equals(stuff[0]) || "VALUE".equals(stuff[0]); lineCount = Integer.parseInt(stuff[1]); if (lineCount > 0) { @@ -125,8 +124,6 @@ public void handleLine(String line) { readState = ReadState.TRIMMED_KEYS; String[] stuff = line.split(" "); - assert "TRIMMED_KEYS".equals(stuff[0]); - lineCount = Integer.parseInt(stuff[1]); if (lineCount > 0) { setReadType(OperationReadType.DATA); @@ -220,14 +217,6 @@ private void readElements(ByteBuffer bb) { return; } - // Read data - // assert key != null; - assert data != null; - - // This will be the case, because we'll clear them when it's not. - assert readOffset <= data.length : "readOffset is " + readOffset - + " data.length is " + data.length; - getLogger() .debug("readOffset: %d, length: %d", readOffset, data.length); @@ -251,19 +240,14 @@ private void readElements(ByteBuffer bb) { if (lookingFor != '\0' && bb.hasRemaining()) { do { byte tmp = bb.get(); - assert tmp == lookingFor : "Expecting " + lookingFor + ", got " - + (char) tmp; + if (tmp != lookingFor) { + throw new IllegalStateException("Expecting " + lookingFor + ", got " + (char) tmp); + } - switch (lookingFor) { - case '\r': - lookingFor = '\n'; - break; - case '\n': - lookingFor = '\0'; - break; - default: - assert false : "Looking for unexpected char: " - + (char) lookingFor; + if (lookingFor == '\r') { + lookingFor = '\n'; + } else { // lookingFor == '\n'; + lookingFor = '\0'; } } while (lookingFor != '\0' && bb.hasRemaining()); diff --git a/src/main/java/net/spy/memcached/protocol/ascii/BaseGetOpImpl.java b/src/main/java/net/spy/memcached/protocol/ascii/BaseGetOpImpl.java index 5e7a683b8..3ddd7fa1e 100644 --- a/src/main/java/net/spy/memcached/protocol/ascii/BaseGetOpImpl.java +++ b/src/main/java/net/spy/memcached/protocol/ascii/BaseGetOpImpl.java @@ -106,7 +106,6 @@ public final void handleLine(String line) { } else if (line.startsWith("VALUE ")) { getLogger().debug("Got line %s", line); String[] stuff = line.split(" "); - assert stuff[0].equals("VALUE"); currentKey = stuff[1]; currentFlags = Integer.parseInt(stuff[2]); data = new byte[Integer.parseInt(stuff[3])]; @@ -129,12 +128,6 @@ public final void handleLine(String line) { @Override public final void handleRead(ByteBuffer bb) { - assert currentKey != null; - assert data != null; - // This will be the case, because we'll clear them when it's not. - assert readOffset <= data.length : "readOffset is " + readOffset - + " data.length is " + data.length; - getLogger().debug("readOffset: %d, length: %d", readOffset, data.length); // If we're not looking for termination, we're still looking for data if (lookingFor == '\0') { @@ -163,17 +156,14 @@ public final void handleRead(ByteBuffer bb) { if (lookingFor != '\0' && bb.hasRemaining()) { do { byte tmp = bb.get(); - assert tmp == lookingFor : "Expecting " + lookingFor + ", got " - + (char) tmp; - switch (lookingFor) { - case '\r': - lookingFor = '\n'; - break; - case '\n': - lookingFor = '\0'; - break; - default: - assert false : "Looking for unexpected char: " + (char) lookingFor; + if (tmp != lookingFor) { + throw new IllegalStateException("Expecting " + lookingFor + ", got " + (char) tmp); + } + + if (lookingFor == '\r') { + lookingFor = '\n'; + } else { // lookingFor == '\n'; + lookingFor = '\0'; } } while (lookingFor != '\0' && bb.hasRemaining()); // Completed the read, reset stuff. @@ -197,34 +187,39 @@ public final void initialize() { String keysString = generateKeysString(); - if (cmd.equals("get") || cmd.equals("gets")) { - // syntax: get \r\n - commandBuilder.append(cmd); - commandBuilder.append(' '); - commandBuilder.append(keysString); - commandBuilder.append(RN_STRING); - } else if (cmd.equals("gat") || cmd.equals("gats")) { - // syntax: gat || gats \r\n - commandBuilder.append(cmd); - commandBuilder.append(' '); - commandBuilder.append(exp); - commandBuilder.append(' '); - commandBuilder.append(keysString); - commandBuilder.append(RN_STRING); - } else { - assert (cmd.equals("mget") || cmd.equals("mgets")) - : "Unknown Command " + cmd; - // syntax: mget \r\n\r\n - int lenKeys = keysString.getBytes().length; - int numKeys = keys.size(); - commandBuilder.append(cmd); - commandBuilder.append(' '); - commandBuilder.append(lenKeys); - commandBuilder.append(' '); - commandBuilder.append(numKeys); - commandBuilder.append(RN_STRING); - commandBuilder.append(keysString); - commandBuilder.append(RN_STRING); + switch (cmd) { + case "get": + case "gets": + // syntax: get || gets \r\n + commandBuilder.append(cmd); + commandBuilder.append(' '); + commandBuilder.append(keysString); + commandBuilder.append(RN_STRING); + break; + case "gat": + case "gats": + // syntax: gat || gats \r\n + commandBuilder.append(cmd); + commandBuilder.append(' '); + commandBuilder.append(exp); + commandBuilder.append(' '); + commandBuilder.append(keysString); + commandBuilder.append(RN_STRING); + break; + case "mget": + case "mgets": + // syntax: mget || mgets \r\n\r\n + commandBuilder.append(cmd); + commandBuilder.append(' '); + commandBuilder.append(keysString.getBytes().length); + commandBuilder.append(' '); + commandBuilder.append(keys.size()); + commandBuilder.append(RN_STRING); + commandBuilder.append(keysString); + commandBuilder.append(RN_STRING); + break; + default: + assert false : "Unknown command: " + cmd; } commandLine = commandBuilder.toString().getBytes(); diff --git a/src/main/java/net/spy/memcached/protocol/ascii/CollectionGetOperationImpl.java b/src/main/java/net/spy/memcached/protocol/ascii/CollectionGetOperationImpl.java index 98388d83d..0dd944aae 100644 --- a/src/main/java/net/spy/memcached/protocol/ascii/CollectionGetOperationImpl.java +++ b/src/main/java/net/spy/memcached/protocol/ascii/CollectionGetOperationImpl.java @@ -129,7 +129,6 @@ public void handleLine(String line) { getLogger().debug("Got line %s", line); String[] stuff = line.split(" "); - assert "VALUE".equals(stuff[0]); flags = Integer.parseInt(stuff[1]); count = Integer.parseInt(stuff[2]); @@ -198,13 +197,6 @@ public void handleRead(ByteBuffer bb) { return; } - // Read data - assert key != null; - assert data != null; - // This will be the case, because we'll clear them when it's not. - assert readOffset <= data.length - : "readOffset is " + readOffset + " data.length is " + data.length; - getLogger().debug("readOffset: %d, length: %d", readOffset, data.length); if (lookingFor == '\0') { @@ -228,19 +220,14 @@ public void handleRead(ByteBuffer bb) { if (lookingFor != '\0' && bb.hasRemaining()) { do { byte tmp = bb.get(); - assert tmp == lookingFor : "Expecting " + lookingFor + ", got " - + (char) tmp; + if (tmp != lookingFor) { + throw new IllegalStateException("Expecting " + lookingFor + ", got " + (char) tmp); + } - switch (lookingFor) { - case '\r': - lookingFor = '\n'; - break; - case '\n': - lookingFor = '\0'; - break; - default: - assert false : "Looking for unexpected char: " - + (char) lookingFor; + if (lookingFor == '\r') { + lookingFor = '\n'; + } else { // lookingFor == '\n'; + lookingFor = '\0'; } } while (lookingFor != '\0' && bb.hasRemaining()); diff --git a/src/main/java/net/spy/memcached/transcoders/TranscoderUtils.java b/src/main/java/net/spy/memcached/transcoders/TranscoderUtils.java index 39fb5f049..452ac1f31 100644 --- a/src/main/java/net/spy/memcached/transcoders/TranscoderUtils.java +++ b/src/main/java/net/spy/memcached/transcoders/TranscoderUtils.java @@ -107,6 +107,10 @@ public byte[] encodeLong(long l) { } public long decodeLong(byte[] b) { + if (b.length > 8) { + throw new IllegalStateException("Too long to be an long (" + b.length + ") bytes"); + } + long rv = 0; for (byte i : b) { rv = (rv << 8) | (i < 0 ? 256 + i : i); @@ -119,8 +123,9 @@ public byte[] encodeInt(int in) { } public int decodeInt(byte[] in) { - assert in.length <= 4 - : "Too long to be an int (" + in.length + ") bytes"; + if (in.length > 4) { + throw new IllegalStateException("Too long to be an int (" + in.length + ") bytes"); + } return (int) decodeLong(in); } @@ -129,12 +134,10 @@ public byte[] encodeByte(byte in) { } public byte decodeByte(byte[] in) { - assert in.length <= 1 : "Too long for a byte"; - byte rv = 0; - if (in.length == 1) { - rv = in[0]; + if (in.length != 1) { + throw new IllegalStateException("Wrong length for a byte"); } - return rv; + return in[0]; } public byte[] encodeBoolean(boolean b) { @@ -144,7 +147,9 @@ public byte[] encodeBoolean(boolean b) { } public boolean decodeBoolean(byte[] in) { - assert in.length == 1 : "Wrong length for a boolean"; + if (in.length != 1) { + throw new IllegalStateException("Wrong length for a boolean"); + } return in[0] == '1'; } diff --git a/src/main/java/net/spy/memcached/transcoders/WhalinTranscoder.java b/src/main/java/net/spy/memcached/transcoders/WhalinTranscoder.java index 53d9f5c17..69462eb30 100644 --- a/src/main/java/net/spy/memcached/transcoders/WhalinTranscoder.java +++ b/src/main/java/net/spy/memcached/transcoders/WhalinTranscoder.java @@ -177,7 +177,9 @@ public byte[] encodeBoolean(boolean b) { } public boolean decodeBoolean(byte[] in) { - assert in.length == 1 : "Wrong length for a boolean"; + if (in.length != 1) { + throw new IllegalStateException("Wrong length for a boolean"); + } return in[0] == 1; } diff --git a/src/main/java/net/spy/memcached/transcoders/WhalinV1Transcoder.java b/src/main/java/net/spy/memcached/transcoders/WhalinV1Transcoder.java index fc9bb907c..d98c76fba 100644 --- a/src/main/java/net/spy/memcached/transcoders/WhalinV1Transcoder.java +++ b/src/main/java/net/spy/memcached/transcoders/WhalinV1Transcoder.java @@ -141,32 +141,40 @@ private Short decodeShort(byte[] data) { } private Byte decodeByte(byte[] in) { - assert in.length == 2 : "Wrong length for a byte"; + if (in.length != 2) { + throw new IllegalStateException("Wrong length for a byte"); + } byte value = in[1]; return Byte.valueOf(value); - } private Integer decodeInteger(byte[] in) { - assert in.length == 5 : "Wrong length for an int"; + if (in.length != 5) { + throw new IllegalStateException("Wrong length for an int"); + } return Integer.valueOf((int) decodeLong(in).longValue()); - } private Float decodeFloat(byte[] in) { - assert in.length == 5 : "Wrong length for a float"; + if (in.length != 5) { + throw new IllegalStateException("Wrong length for a float"); + } Integer l = decodeInteger(in); return Float.valueOf(Float.intBitsToFloat(l.intValue())); } private Double decodeDouble(byte[] in) { - assert in.length == 9 : "Wrong length for a double"; + if (in.length != 9) { + throw new IllegalStateException("Wrong length for a double"); + } Long l = decodeLong(in); return Double.valueOf(Double.longBitsToDouble(l.longValue())); } private Boolean decodeBoolean(byte[] in) { - assert in.length == 2 : "Wrong length for a boolean"; + if (in.length != 2) { + throw new IllegalStateException("Wrong length for a boolean"); + } return Boolean.valueOf(in[1] == 1); } diff --git a/src/test/java/net/spy/memcached/AddrUtilTest.java b/src/test/java/net/spy/memcached/AddrUtilTest.java index 18135b268..9dfb03996 100644 --- a/src/test/java/net/spy/memcached/AddrUtilTest.java +++ b/src/test/java/net/spy/memcached/AddrUtilTest.java @@ -94,8 +94,8 @@ void testNullList() throws Exception { try { List addrs = AddrUtil.getAddresses(s); fail("Expected failure, got " + addrs); - } catch (NullPointerException e) { - assertEquals("Null host list", e.getMessage()); + } catch (IllegalArgumentException e) { + assertEquals("Hosts must not be null", e.getMessage()); } } diff --git a/src/test/java/net/spy/memcached/transcoders/TranscoderUtilsTest.java b/src/test/java/net/spy/memcached/transcoders/TranscoderUtilsTest.java index 1bcfc2219..56f4f2d86 100644 --- a/src/test/java/net/spy/memcached/transcoders/TranscoderUtilsTest.java +++ b/src/test/java/net/spy/memcached/transcoders/TranscoderUtilsTest.java @@ -27,7 +27,7 @@ void testBooleanOverflow() { try { boolean b = tu.decodeBoolean(oversizeBytes); fail("Got " + b + " expected assertion."); - } catch (AssertionError e) { + } catch (IllegalStateException e) { // pass } } @@ -37,7 +37,7 @@ void testByteOverflow() { try { byte b = tu.decodeByte(oversizeBytes); fail("Got " + b + " expected assertion."); - } catch (AssertionError e) { + } catch (IllegalStateException e) { // pass } } @@ -47,7 +47,7 @@ void testIntOverflow() { try { int b = tu.decodeInt(oversizeBytes); fail("Got " + b + " expected assertion."); - } catch (AssertionError e) { + } catch (IllegalStateException e) { // pass } } @@ -57,7 +57,7 @@ void testLongOverflow() { try { long b = tu.decodeLong(oversizeBytes); fail("Got " + b + " expected assertion."); - } catch (AssertionError e) { + } catch (IllegalStateException e) { // pass } } diff --git a/src/test/java/net/spy/memcached/v2/KVAsyncArcusCommandsTest.java b/src/test/java/net/spy/memcached/v2/KVAsyncArcusCommandsTest.java index 903feafa9..170c28b8c 100644 --- a/src/test/java/net/spy/memcached/v2/KVAsyncArcusCommandsTest.java +++ b/src/test/java/net/spy/memcached/v2/KVAsyncArcusCommandsTest.java @@ -172,9 +172,7 @@ void appendNonStringException() throws ExecutionException, InterruptedException, }) .toCompletableFuture(); // then - // AssertionError in the Transcoder causes the I/O thread to terminate abruptly. - // The future is never completed, leading to a TimeoutException in the main thread. - assertThrows(TimeoutException.class, () -> future.get(300L, TimeUnit.MILLISECONDS)); + assertThrows(ExecutionException.class, () -> future.get(300L, TimeUnit.MILLISECONDS)); } @Test