From 98165c4b69f0a5df93a7c964b9b9e143c7f7a083 Mon Sep 17 00:00:00 2001 From: Timothy Bish Date: Thu, 31 Mar 2016 16:52:34 -0400 Subject: [PATCH] https://issues.apache.org/jira/browse/AMQ-6228 Create a better error message that for the invalid frame size error. --- .../transport/amqp/AmqpFrameParser.java | 3 +- .../activemq/openwire/OpenWireFormat.java | 5 +-- .../activemq/util/IOExceptionSupport.java | 33 +++++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/activemq-amqp/src/main/java/org/apache/activemq/transport/amqp/AmqpFrameParser.java b/activemq-amqp/src/main/java/org/apache/activemq/transport/amqp/AmqpFrameParser.java index 247a5e9e36..06bc97b219 100644 --- a/activemq-amqp/src/main/java/org/apache/activemq/transport/amqp/AmqpFrameParser.java +++ b/activemq-amqp/src/main/java/org/apache/activemq/transport/amqp/AmqpFrameParser.java @@ -21,6 +21,7 @@ import java.nio.ByteBuffer; import org.apache.activemq.transport.amqp.AmqpWireFormat.ResetListener; import org.apache.activemq.transport.tcp.TcpTransport; +import org.apache.activemq.util.IOExceptionSupport; import org.fusesource.hawtbuf.Buffer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -84,7 +85,7 @@ public class AmqpFrameParser { } if (frameSize > maxFrameSize) { - throw new IOException("Frame size of " + frameSize + " larger than max allowed " + maxFrameSize); + throw IOExceptionSupport.createFrameSizeException(frameSize, maxFrameSize); } } diff --git a/activemq-client/src/main/java/org/apache/activemq/openwire/OpenWireFormat.java b/activemq-client/src/main/java/org/apache/activemq/openwire/OpenWireFormat.java index f70bff840c..c1297a24d1 100755 --- a/activemq-client/src/main/java/org/apache/activemq/openwire/OpenWireFormat.java +++ b/activemq-client/src/main/java/org/apache/activemq/openwire/OpenWireFormat.java @@ -30,6 +30,7 @@ import org.apache.activemq.util.ByteSequence; import org.apache.activemq.util.ByteSequenceData; import org.apache.activemq.util.DataByteArrayInputStream; import org.apache.activemq.util.DataByteArrayOutputStream; +import org.apache.activemq.util.IOExceptionSupport; import org.apache.activemq.wireformat.WireFormat; /** @@ -193,7 +194,7 @@ public final class OpenWireFormat implements WireFormat { } 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) { int size = dis.readInt(); 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(); // byte[] data = new byte[size]; diff --git a/activemq-client/src/main/java/org/apache/activemq/util/IOExceptionSupport.java b/activemq-client/src/main/java/org/apache/activemq/util/IOExceptionSupport.java index ed0925f05d..7aa2fc47b8 100755 --- a/activemq-client/src/main/java/org/apache/activemq/util/IOExceptionSupport.java +++ b/activemq-client/src/main/java/org/apache/activemq/util/IOExceptionSupport.java @@ -17,6 +17,7 @@ package org.apache.activemq.util; import java.io.IOException; +import java.math.BigInteger; public final class IOExceptionSupport { @@ -47,4 +48,36 @@ public final class IOExceptionSupport { 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; + } }