HavretGC commented on a change in pull request #4: [WIP] Failover implementation
URL: https://github.com/apache/activemq-nms-amqp/pull/4#discussion_r302222547
##########
File path: src/NMS.AMQP/Provider/Amqp/Message/AmqpNmsBytesMessageFacade.cs
##########
@@ -0,0 +1,148 @@
+using System.IO;
+using Amqp.Framing;
+using Amqp.Types;
+using Apache.NMS.AMQP.Message;
+using Apache.NMS.AMQP.Message.Facade;
+using Apache.NMS.AMQP.Util;
+using Apache.NMS.Util;
+
+namespace Apache.NMS.AMQP.Provider.Amqp.Message
+{
+ public class AmqpNmsBytesMessageFacade : AmqpNmsMessageFacade,
INmsBytesMessageFacade
+ {
+ private EndianBinaryReader byteIn = null;
+ private EndianBinaryWriter byteOut = null;
+
+ private static readonly Data EMPTY_DATA = new Data {Binary = new
byte[0]};
+
+ public override double JmsMsgType => MessageSupport.JMS_TYPE_BYTE;
+ public long BodyLength => GetBinaryFromBody().Binary.LongLength;
+
+ public BinaryReader GetDataReader()
+ {
+ if (byteOut != null)
+ {
+ throw new IllegalStateException("Body is being written to,
cannot perform a read.");
+ }
+
+ if (byteIn == null)
+ {
+ Data body = GetBinaryFromBody();
+ Stream dataStream = new MemoryStream(body.Binary, false);
+ byteIn = new EndianBinaryReader(dataStream);
+ }
+
+ return byteIn;
+ }
+
+ private Data GetBinaryFromBody()
+ {
+ RestrictedDescribed body = Message.BodySection;
+ Data result = EMPTY_DATA;
+ if (body == null)
+ {
+ return result;
+ }
+ else if (body is Data)
+ {
+ byte[] binary = (body as Data).Binary;
+ if (binary != null && binary.Length != 0)
+ {
+ return body as Data;
+ }
+ }
+ else if (body is AmqpValue)
+ {
+ object value = (body as AmqpValue).Value;
+ if (value == null)
+ {
+ return result;
+ }
+
+ if (value is byte[])
+ {
+ byte[] dataValue = value as byte[];
+ if (dataValue.Length > 0)
+ {
+ result = new Data();
+ result.Binary = dataValue;
+ }
+ }
+ else
+ {
+ throw new IllegalStateException("Unexpected Amqp value
content-type: " + value.GetType().FullName);
+ }
+ }
+ else
+ {
+ throw new IllegalStateException("Unexpected body content-type:
" + body.GetType().FullName);
+ }
+
+ return result;
+ }
+
+ public BinaryWriter GetDataWriter()
+ {
+ if (byteIn != null)
+ {
+ throw new IllegalStateException("Body is being read from,
cannot perform a write.");
+ }
+
+ if (byteOut == null)
+ {
+ MemoryStream outputBuffer = new MemoryStream();
+ byteOut = new EndianBinaryWriter(outputBuffer);
+ Message.BodySection = EMPTY_DATA;
+ }
+
+ return byteOut;
+ }
+
+ protected override void InitializeEmptyBody()
+ {
+ ContentType = SymbolUtil.OCTET_STREAM_CONTENT_TYPE;
+ }
+
+ public void Reset()
+ {
+ if (byteOut != null)
Review comment:
Done.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services