com.microsoft.azure.eventhubs.impl.AmqpUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of azure-eventhubs Show documentation
Show all versions of azure-eventhubs Show documentation
Client library for talking to Microsoft Azure Event Hubs.
/*
* Copyright (c) Microsoft. All rights reserved.
* Licensed under the MIT license. See LICENSE file in the project root for full license information.
*/
package com.microsoft.azure.eventhubs.impl;
import org.apache.qpid.proton.amqp.Binary;
import org.apache.qpid.proton.amqp.Symbol;
import org.apache.qpid.proton.amqp.messaging.AmqpValue;
import org.apache.qpid.proton.amqp.messaging.ApplicationProperties;
import org.apache.qpid.proton.amqp.messaging.Data;
import org.apache.qpid.proton.amqp.messaging.MessageAnnotations;
import org.apache.qpid.proton.message.Message;
import java.util.Locale;
public final class AmqpUtil {
private AmqpUtil() {
}
private static int getPayloadSize(Message msg) {
if (msg == null || msg.getBody() == null) {
return 0;
}
if (msg.getBody() instanceof Data) {
final Data payloadSection = (Data) msg.getBody();
if (payloadSection == null) {
return 0;
}
final Binary payloadBytes = payloadSection.getValue();
if (payloadBytes == null) {
return 0;
}
return payloadBytes.getLength();
}
if (msg.getBody() instanceof AmqpValue) {
final AmqpValue amqpValue = (AmqpValue) msg.getBody();
if (amqpValue == null) {
return 0;
}
return amqpValue.getValue().toString().length() * 2;
}
return 0;
}
public static int getDataSerializedSize(Message amqpMessage) {
if (amqpMessage == null) {
return 0;
}
int payloadSize = getPayloadSize(amqpMessage);
// EventData - accepts only PartitionKey - which is a String & stuffed into MessageAnnotation
final MessageAnnotations messageAnnotations = amqpMessage.getMessageAnnotations();
final ApplicationProperties applicationProperties = amqpMessage.getApplicationProperties();
int annotationsSize = 0;
int applicationPropertiesSize = 0;
if (messageAnnotations != null) {
for (Symbol value : messageAnnotations.getValue().keySet()) {
annotationsSize += sizeof(value);
}
for (Object value : messageAnnotations.getValue().values()) {
annotationsSize += sizeof(value);
}
}
if (applicationProperties != null) {
for (Object value : applicationProperties.getValue().keySet()) {
applicationPropertiesSize += sizeof(value);
}
for (Object value : applicationProperties.getValue().values()) {
applicationPropertiesSize += sizeof(value);
}
}
return annotationsSize + applicationPropertiesSize + payloadSize;
}
private static int sizeof(Object obj) {
if (obj instanceof String) {
return obj.toString().length() << 1;
}
if (obj instanceof Symbol) {
return ((Symbol) obj).length() << 1;
}
if (obj instanceof Integer) {
return Integer.BYTES;
}
if (obj instanceof Long) {
return Long.BYTES;
}
if (obj instanceof Short) {
return Short.BYTES;
}
if (obj instanceof Character) {
return Character.BYTES;
}
if (obj instanceof Float) {
return Float.BYTES;
}
if (obj instanceof Double) {
return Double.BYTES;
}
throw new IllegalArgumentException(String.format(Locale.US, "Encoding Type: %s is not supported", obj.getClass()));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy