Create a better error message that for the invalid frame size error.
This commit is contained in:
Timothy Bish 2016-03-31 16:52:34 -04:00
parent 0fb24cc4c1
commit 98165c4b69
3 changed files with 38 additions and 3 deletions

View File

@ -21,6 +21,7 @@ import java.nio.ByteBuffer;
import org.apache.activemq.transport.amqp.AmqpWireFormat.ResetListener; import org.apache.activemq.transport.amqp.AmqpWireFormat.ResetListener;
import org.apache.activemq.transport.tcp.TcpTransport; import org.apache.activemq.transport.tcp.TcpTransport;
import org.apache.activemq.util.IOExceptionSupport;
import org.fusesource.hawtbuf.Buffer; import org.fusesource.hawtbuf.Buffer;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -84,7 +85,7 @@ public class AmqpFrameParser {
} }
if (frameSize > maxFrameSize) { if (frameSize > maxFrameSize) {
throw new IOException("Frame size of " + frameSize + " larger than max allowed " + maxFrameSize); throw IOExceptionSupport.createFrameSizeException(frameSize, maxFrameSize);
} }
} }

View File

@ -30,6 +30,7 @@ import org.apache.activemq.util.ByteSequence;
import org.apache.activemq.util.ByteSequenceData; import org.apache.activemq.util.ByteSequenceData;
import org.apache.activemq.util.DataByteArrayInputStream; import org.apache.activemq.util.DataByteArrayInputStream;
import org.apache.activemq.util.DataByteArrayOutputStream; import org.apache.activemq.util.DataByteArrayOutputStream;
import org.apache.activemq.util.IOExceptionSupport;
import org.apache.activemq.wireformat.WireFormat; import org.apache.activemq.wireformat.WireFormat;
/** /**
@ -193,7 +194,7 @@ public final class OpenWireFormat implements WireFormat {
} }
if (size > maxFrameSize) { if (size > maxFrameSize) {
throw new IOException("Frame size of " + (size / (1024 * 1024)) + " MB larger than max allowed " + (maxFrameSize / (1024 * 1024)) + " MB"); throw IOExceptionSupport.createFrameSizeException(size, maxFrameSize);
} }
} }
@ -266,7 +267,7 @@ public final class OpenWireFormat implements WireFormat {
if (!sizePrefixDisabled) { if (!sizePrefixDisabled) {
int size = dis.readInt(); int size = dis.readInt();
if (size > maxFrameSize) { if (size > maxFrameSize) {
throw new IOException("Frame size of " + (size / (1024 * 1024)) + " MB larger than max allowed " + (maxFrameSize / (1024 * 1024)) + " MB"); throw IOExceptionSupport.createFrameSizeException(size, maxFrameSize);
} }
// int size = dis.readInt(); // int size = dis.readInt();
// byte[] data = new byte[size]; // byte[] data = new byte[size];

View File

@ -17,6 +17,7 @@
package org.apache.activemq.util; package org.apache.activemq.util;
import java.io.IOException; import java.io.IOException;
import java.math.BigInteger;
public final class IOExceptionSupport { public final class IOExceptionSupport {
@ -47,4 +48,36 @@ public final class IOExceptionSupport {
return exception; return exception;
} }
public static IOException createFrameSizeException(int size, long maxSize) {
return new IOException("Frame size of " + toHumanReadableSizeString(size) +
" larger than max allowed " + toHumanReadableSizeString(maxSize));
}
private static String toHumanReadableSizeString(final int size) {
return toHumanReadableSizeString(BigInteger.valueOf(size));
}
private static String toHumanReadableSizeString(final long size) {
return toHumanReadableSizeString(BigInteger.valueOf(size));
}
private static String toHumanReadableSizeString(final BigInteger size) {
String displaySize;
final BigInteger ONE_KB_BI = BigInteger.valueOf(1024);
final BigInteger ONE_MB_BI = ONE_KB_BI.multiply(ONE_KB_BI);
final BigInteger ONE_GB_BI = ONE_KB_BI.multiply(ONE_MB_BI);
if (size.divide(ONE_GB_BI).compareTo(BigInteger.ZERO) > 0) {
displaySize = String.valueOf(size.divide(ONE_GB_BI)) + " GB";
} else if (size.divide(ONE_MB_BI).compareTo(BigInteger.ZERO) > 0) {
displaySize = String.valueOf(size.divide(ONE_MB_BI)) + " MB";
} else if (size.divide(ONE_KB_BI).compareTo(BigInteger.ZERO) > 0) {
displaySize = String.valueOf(size.divide(ONE_KB_BI)) + " KB";
} else {
displaySize = String.valueOf(size) + " bytes";
}
return displaySize;
}
} }