Created SignatureMismatch exception to better describe that case

This commit is contained in:
Collin Smith 2020-08-13 00:10:10 -07:00
parent 5560eb2b3f
commit a2d8157987
2 changed files with 31 additions and 14 deletions

View File

@ -1,7 +1,6 @@
package com.riiablo.io;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;
import java.nio.charset.Charset;
@ -175,13 +174,13 @@ public class ByteInput {
/**
* Checks if the subsequent bytes match the specified sequence of bytes.
* If they do, they will be consumed by this method call, otherwise an
* {@link InvalidFormat} will be thrown and any bytes read by this method
* {@link SignatureMismatch} will be thrown and any bytes read by this method
* will be unread.
*
* @throws InvalidFormat if the next bytes do not match the specified
* @throws SignatureMismatch if the next bytes do not match the specified
* signature.
*
* @see InvalidFormat
* @see SignatureMismatch
* @see #skipUntil(byte[])
*/
public ByteInput readSignature(byte[] signature) {
@ -191,11 +190,7 @@ public class ByteInput {
final byte[] actual = new byte[buffer.readableBytes()];
buffer.readBytes(actual);
buffer.resetReaderIndex();
throw new InvalidFormat(
this,
String.format("Signatures do not match: %s, expected %s",
ByteBufUtil.hexDump(actual),
ByteBufUtil.hexDump(signature)));
throw new SignatureMismatch(this, actual, signature);
}
final byte[] actual = new byte[signature.length];
@ -203,11 +198,7 @@ public class ByteInput {
final boolean match = Arrays.equals(actual, signature);
if (!match) {
buffer.resetReaderIndex();
throw new InvalidFormat(
this,
String.format("Signatures do not match: %s, expected %s",
ByteBufUtil.hexDump(actual),
ByteBufUtil.hexDump(signature)));
throw new SignatureMismatch(this, actual, signature);
}
incrementBitsRead((long) signature.length * Byte.SIZE);
return this;

View File

@ -0,0 +1,26 @@
package com.riiablo.io;
import io.netty.buffer.ByteBufUtil;
public class SignatureMismatch extends InvalidFormat {
public final byte[] actual;
public final byte[] expected;
public SignatureMismatch(ByteInput in, byte[] actual, byte[] expected) {
super(
in,
String.format("Signatures do not match: %s, expected %s",
ByteBufUtil.hexDump(actual),
ByteBufUtil.hexDump(expected)));
this.actual = actual;
this.expected = expected;
}
public byte[] actual() {
return actual;
}
public byte[] expected() {
return expected;
}
}