Beispiel #1
0
 public byte[] Serialize(double data, bool isKey, MessageMetadata messageMetadata, TopicPartition destination)
 {
     if (BitConverter.IsLittleEndian)
     {
         unsafe
         {
             byte[] result = new byte[8];
             byte * p      = (byte *)(&data);
             result[7] = *p++;
             result[6] = *p++;
             result[5] = *p++;
             result[4] = *p++;
             result[3] = *p++;
             result[2] = *p++;
             result[1] = *p++;
             result[0] = *p++;
             return(result);
         }
     }
     else
     {
         return(BitConverter.GetBytes(data));
     }
 }
Beispiel #2
0
 public byte[] Serialize(Null data, bool isKey, MessageMetadata messageMetadata, TopicPartition destination)
 => null;
Beispiel #3
0
 public Ignore Deserialize(ReadOnlySpan <byte> data, bool isNull, bool isKey, MessageMetadata messageMetadata, TopicPartition source)
 => null;
Beispiel #4
0
            public long Deserialize(ReadOnlySpan <byte> data, bool isNull, bool isKey, MessageMetadata messageMetadata, TopicPartition source)
            {
                if (isNull)
                {
                    throw new DeserializationException($"Null data encountered deserializing Int64 value.");
                }

                if (data.Length != 8)
                {
                    throw new DeserializationException($"Deserializer<Long> encountered data of length {data.Length}. Expecting data length to be 8.");
                }

                // network byte order -> big endian -> most significant byte in the smallest address.
                long result = ((long)data[0]) << 56 |
                              ((long)(data[1])) << 48 |
                              ((long)(data[2])) << 40 |
                              ((long)(data[3])) << 32 |
                              ((long)(data[4])) << 24 |
                              ((long)(data[5])) << 16 |
                              ((long)(data[6])) << 8 |
                              (data[7]);

                return(result);
            }
Beispiel #5
0
            public Null Deserialize(ReadOnlySpan <byte> data, bool isNull, bool isKey, MessageMetadata messageMetadata, TopicPartition source)
            {
                if (!isNull)
                {
                    throw new DeserializationException("Deserializer<Null> may only be used to deserialize data that is null.");
                }

                return(null);
            }
Beispiel #6
0
            public string Deserialize(ReadOnlySpan <byte> data, bool isNull, bool isKey, MessageMetadata messageMetadata, TopicPartition source)
            {
                if (isNull)
                {
                    return(null);
                }

                try
                {
                    #if NETCOREAPP2_1
                    return(Encoding.UTF8.GetString(data));
                    #else
                    return(Encoding.UTF8.GetString(data.ToArray()));
                    #endif
                }
                catch (Exception e)
                {
                    throw new DeserializationException("Error occured deserializing UTF8 string value", e);
                }
            }
Beispiel #7
0
 public byte[] Deserialize(ReadOnlySpan <byte> data, bool isNull, bool isKey, MessageMetadata messageMetadata, TopicPartition source)
 {
     if (isNull)
     {
         return(null);
     }
     return(data.ToArray());
 }
Beispiel #8
0
            public double Deserialize(ReadOnlySpan <byte> data, bool isNull, bool isKey, MessageMetadata messageMetadata, TopicPartition source)
            {
                if (isNull)
                {
                    throw new DeserializationException($"Null data encountered deserializing an double value.");
                }

                if (data.Length != 8)
                {
                    throw new DeserializationException($"Deserializer<double> encountered data of length {data.Length}. Expecting data length to be 8.");
                }

                // network byte order -> big endian -> most significant byte in the smallest address.
                if (BitConverter.IsLittleEndian)
                {
                    unsafe
                    {
                        double result = default(double);
                        byte * p      = (byte *)(&result);
                        *      p++    = data[7];
                        *      p++    = data[6];
                        *      p++    = data[5];
                        *      p++    = data[4];
                        *      p++    = data[3];
                        *      p++    = data[2];
                        *      p++    = data[1];
                        *      p++    = data[0];
                        return(result);
                    }
                }
                else
                {
                    try
                    {
                        #if NETCOREAPP2_1
                        return(BitConverter.ToDouble(data));
                        #else
                        return(BitConverter.ToDouble(data.ToArray(), 0));
                        #endif
                    }
                    catch (Exception e)
                    {
                        throw new DeserializationException("Error occured deserializing double value.", e);
                    }
                }
            }
Beispiel #9
0
            public int Deserialize(ReadOnlySpan <byte> data, bool isNull, bool isKey, MessageMetadata messageMetadata, TopicPartition source)
            {
                if (isNull)
                {
                    throw new DeserializationException($"Null data encountered deserializing an Int32 value");
                }

                if (data.Length != 4)
                {
                    throw new DeserializationException($"Deserializer<Int32> encountered data of length {data.Length}. Expecting data length to be 4.");
                }

                // network byte order -> big endian -> most significant byte in the smallest address.
                return
                    ((((int)data[0]) << 24) |
                     (((int)data[1]) << 16) |
                     (((int)data[2]) << 8) |
                     (int)data[3]);
            }