static decimal DecodeDecimal128(ByteBuffer buffer) { byte[] bytes = new byte[FixedWidth.Decimal128]; AmqpBitConverter.ReadBytes(buffer, bytes, 0, bytes.Length); int sign = 1; int exponent = 0; sign = (bytes[0] & 0x80) != 0 ? -1 : 1; if ((bytes[0] & 0x60) != 0x60) { // s 14-bit-exponent (0)113-bit-significant exponent = ((bytes[0] & 0x7F) << 7) | ((bytes[1] & 0xFE) >> 1); bytes[0] = 0; bytes[1] &= 0x1; } else if ((bytes[0] & 0x78) != 0) { // handle NaN and Infinity } else { // s 11 14-bit-exponent (100)111-bit-significant // it is out of the valid range already. Should not be used return(0); } int high = (int)AmqpBitConverter.ReadUInt(bytes, 4, 4); int middle = (int)AmqpBitConverter.ReadUInt(bytes, 8, 4); int low = (int)AmqpBitConverter.ReadUInt(bytes, 12, 4); return(CreateDecimal(low, middle, high, sign, exponent - Decimal128Bias)); }
static decimal DecodeDecimal64(ByteBuffer buffer) { byte[] bytes = new byte[FixedWidth.Decimal64]; AmqpBitConverter.ReadBytes(buffer, bytes, 0, bytes.Length); int sign = 1; int exponent = 0; sign = (bytes[0] & 0x80) != 0 ? -1 : 1; if ((bytes[0] & 0x60) != 0x60) { // s 10-bit-exponent (0)53-bit-significant exponent = ((bytes[0] & 0x7F) << 3) | ((bytes[1] & 0xE0) >> 5); bytes[0] = 0; bytes[1] &= 0x1F; } else if ((bytes[0] & 0x78) != 0) { // handle NaN and Infinity } else { // s 11 10-bit-exponent (100)51-bit-significant exponent = ((bytes[0] & 0x1F) << 8) | ((bytes[1] & 0xF8) >> 3); bytes[0] = 0; bytes[1] &= 0x7; bytes[1] |= 0x20; } int middle = (int)AmqpBitConverter.ReadUInt(bytes, 0, 4); int low = (int)AmqpBitConverter.ReadUInt(bytes, 4, 4); return(CreateDecimal(low, middle, 0, sign, exponent - Decimal64Bias)); }
static decimal DecodeDecimal32(ByteBuffer buffer) { byte[] bytes = new byte[FixedWidth.Decimal32]; AmqpBitConverter.ReadBytes(buffer, bytes, 0, bytes.Length); int sign = 1; int exponent = 0; sign = (bytes[0] & 0x80) != 0 ? -1 : 1; if ((bytes[0] & 0x60) != 0x60) { // s 8-bit-exponent (0)23-bit-significant exponent = ((bytes[0] & 0x7F) << 1) | ((bytes[1] & 0x80) >> 7); bytes[0] = 0; bytes[1] &= 0x7F; } else if ((bytes[0] & 0x78) != 0) { // handle NaN and Infinity } else { // s 11 8-bit-exponent (100)21-bit-significant exponent = ((bytes[0] & 0x1F) << 3) | ((bytes[1] & 0xE0) >> 5); bytes[0] = 0; bytes[1] &= 0x1F; bytes[1] |= 0x80; } int low = (int)AmqpBitConverter.ReadUInt(bytes, 0, bytes.Length); return(CreateDecimal(low, 0, 0, sign, exponent - Decimal32Bias)); }
public static string Decode(ByteBuffer buffer, FormatCode formatCode) { if (formatCode == 0 && (formatCode = AmqpEncoding.ReadFormatCode(buffer)) == FormatCode.Null) { return(null); } int count; Encoding encoding; if (formatCode == FormatCode.String8Utf8) { count = (int)AmqpBitConverter.ReadUByte(buffer); encoding = Encoding.UTF8; } else if (formatCode == FormatCode.String32Utf8) { count = (int)AmqpBitConverter.ReadUInt(buffer); encoding = Encoding.UTF8; } else { throw AmqpEncoding.GetEncodingException(AmqpResources.GetString(AmqpResources.AmqpInvalidFormatCode, formatCode, buffer.Offset)); } string value = encoding.GetString(buffer.Buffer, buffer.Offset, count); buffer.Complete(count); return(value); }
public static void ReadCount(ByteBuffer buffer, FormatCode formatCode, FormatCode formatCode8, FormatCode formatCode32, out int count) { if (formatCode == formatCode8) { count = AmqpBitConverter.ReadUByte(buffer); } else if (formatCode == formatCode32) { count = (int)AmqpBitConverter.ReadUInt(buffer); } else { throw GetEncodingException(AmqpResources.GetString(AmqpResources.AmqpInvalidFormatCode, formatCode, buffer.Offset)); } }
static T[] Decode <T>(ByteBuffer buffer, int size, int count, FormatCode formatCode) { object descriptor = null; if (formatCode == FormatCode.Described) { descriptor = AmqpEncoding.DecodeObject(buffer); formatCode = AmqpEncoding.ReadFormatCode(buffer); } // Special cases for primitive types switch (formatCode) { case FormatCode.UInt0: case FormatCode.SmallUInt: return((T[])DecodeArray <uint>(buffer, formatCode, count, (b, f) => UIntEncoding.Decode(b, f).Value)); case FormatCode.UInt: return((T[])DecodeArray <uint>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadUInt(b))); case FormatCode.SmallInt: return((T[])DecodeArray <int>(buffer, formatCode, count, (b, f) => IntEncoding.Decode(b, f).Value)); case FormatCode.Int: return((T[])DecodeArray <int>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadInt(b))); case FormatCode.ULong0: case FormatCode.SmallULong: return((T[])DecodeArray <ulong>(buffer, formatCode, count, (b, f) => ULongEncoding.Decode(b, f).Value)); case FormatCode.ULong: return((T[])DecodeArray <ulong>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadULong(b))); case FormatCode.SmallLong: return((T[])DecodeArray <long>(buffer, formatCode, count, (b, f) => LongEncoding.Decode(b, f).Value)); case FormatCode.Long: return((T[])DecodeArray <long>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadLong(b))); case FormatCode.Symbol8: case FormatCode.Symbol32: return((T[])DecodeArray <AmqpSymbol>(buffer, formatCode, count, (b, f) => SymbolEncoding.Decode(b, f))); case FormatCode.Boolean: return((T[])DecodeArray <bool>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadUByte(b) > 0)); case FormatCode.UByte: return((T[])DecodeArray <byte>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadUByte(b))); case FormatCode.Byte: return((T[])DecodeArray <sbyte>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadByte(b))); case FormatCode.UShort: return((T[])DecodeArray <ushort>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadUShort(b))); case FormatCode.Short: return((T[])DecodeArray <short>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadShort(b))); case FormatCode.Float: return((T[])DecodeArray <float>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadFloat(b))); case FormatCode.Double: return((T[])DecodeArray <double>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadDouble(b))); case FormatCode.Decimal32: case FormatCode.Decimal64: case FormatCode.Decimal128: return((T[])DecodeArray <decimal>(buffer, formatCode, count, (b, f) => DecimalEncoding.DecodeValue(b, f))); case FormatCode.Char: return((T[])DecodeArray <char>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadChar(b))); case FormatCode.TimeStamp: return((T[])DecodeArray <DateTime>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadTimestamp(b))); case FormatCode.Uuid: return((T[])DecodeArray <Guid>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadUuid(b))); default: break; } // General path EncodingBase encoding = AmqpEncoding.GetEncoding(formatCode); T[] array = new T[count]; for (int i = 0; i < count; ++i) { object value = encoding.DecodeObject(buffer, formatCode); if (descriptor != null) { value = new DescribedType(descriptor, value); } array[i] = (T)value; } return(array); }