Пример #1
0
        public override byte[] Serialize(ushort protocolVersion, Polygon value)
        {
            var totalRingsLength = value.Rings.Sum(r => 4 + r.Count * 16);
            var buffer           = new byte[9 + totalRingsLength];
            var isLittleEndian   = UseLittleEndianSerialization();

            buffer[0] = isLittleEndian ? (byte)1 : (byte)0;
            var offset = 1;

            EndianBitConverter.SetBytes(isLittleEndian, buffer, offset, (int)GeometryType.Polygon);
            offset += 4;
            EndianBitConverter.SetBytes(isLittleEndian, buffer, offset, value.Rings.Count);
            offset += 4;
            foreach (var ring in value.Rings)
            {
                EndianBitConverter.SetBytes(isLittleEndian, buffer, offset, ring.Count);
                offset += 4;
                foreach (var point in ring)
                {
                    EndianBitConverter.SetBytes(isLittleEndian, buffer, offset, point.X);
                    EndianBitConverter.SetBytes(isLittleEndian, buffer, offset + 8, point.Y);
                    offset += 16;
                }
            }
            return(buffer);
        }
Пример #2
0
        private static int WriteDateRangeBound(byte[] buffer, int offset, DateRangeBound value)
        {
            var millis = Convert.ToInt64(Math.Floor((value.Timestamp - UnixStart).TotalMilliseconds));

            EndianBitConverter.SetBytes(false, buffer, offset, millis);
            buffer[offset + 8] = (byte)value.Precision;
            return(offset + 9);
        }
        private static int WriteDateRangeBound(byte[] buffer, int offset, DateRangeBound value)
        {
            var ticksDiff        = value.Timestamp.Ticks - UnixStart.Ticks;
            var millisecondsDiff = ticksDiff / (decimal)TimeSpan.TicksPerMillisecond;
            var millis           = Convert.ToInt64(Math.Floor(millisecondsDiff));

            EndianBitConverter.SetBytes(false, buffer, offset, millis);
            buffer[offset + 8] = (byte)value.Precision;
            return(offset + 9);
        }
Пример #4
0
        public override byte[] Serialize(ushort protocolVersion, Point value)
        {
            var buffer         = new byte[21];
            var isLittleEndian = UseLittleEndianSerialization();

            buffer[0] = isLittleEndian ? (byte)1 : (byte)0;
            EndianBitConverter.SetBytes(isLittleEndian, buffer, 1, (int)GeometryType.Point2D);
            EndianBitConverter.SetBytes(isLittleEndian, buffer, 5, value.X);
            EndianBitConverter.SetBytes(isLittleEndian, buffer, 13, value.Y);
            return(buffer);
        }
        public override byte[] Serialize(ushort protocolVersion, LineString value)
        {
            var buffer         = new byte[9 + value.Points.Count * 16];
            var isLittleEndian = UseLittleEndianSerialization();

            buffer[0] = isLittleEndian ? (byte)1 : (byte)0;
            var offset = 1;

            EndianBitConverter.SetBytes(isLittleEndian, buffer, offset, (int)GeometryType.LineString);
            offset += 4;
            EndianBitConverter.SetBytes(isLittleEndian, buffer, offset, value.Points.Count);
            offset += 4;
            foreach (var point in value.Points)
            {
                EndianBitConverter.SetBytes(isLittleEndian, buffer, offset, point.X);
                EndianBitConverter.SetBytes(isLittleEndian, buffer, offset + 8, point.Y);
                offset += 16;
            }
            return(buffer);
        }
        public void ToDouble_SetBytes_Test()
        {
            var values = new[]
            {
                Tuple.Create(0D, new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }),
                Tuple.Create(-1D, new byte[] { 0xbf, 0xf0, 0, 0, 0, 0, 0, 0 }),
                Tuple.Create(-2D, new byte[] { 0xc0, 0, 0, 0, 0, 0, 0, 0 }),
                Tuple.Create(-2.3D, new byte[] { 0xc0, 0x02, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66 }),
                Tuple.Create(10.3112277778D, new byte[] { 0x40, 0x24, 0x9f, 0x59, 0x3f, 0x4e, 0x83, 0xf8 })
            };

            foreach (var v in values)
            {
                var buffer = new byte[8];
                EndianBitConverter.SetBytes(false, buffer, 0, v.Item1);
                CollectionAssert.AreEqual(v.Item2, buffer);
                EndianBitConverter.SetBytes(true, buffer, 0, v.Item1);
                CollectionAssert.AreEqual(v.Item2.Reverse(), buffer);
                var decoded = EndianBitConverter.ToDouble(false, v.Item2, 0);
                Assert.AreEqual(v.Item1, decoded);
                decoded = EndianBitConverter.ToDouble(true, v.Item2.Reverse().ToArray(), 0);
                Assert.AreEqual(v.Item1, decoded);
            }
        }
        public void ToInt32_SetBytes_Test()
        {
            var values = new[]
            {
                Tuple.Create(0, new byte[] { 0, 0, 0, 0 }),
                Tuple.Create(-1, new byte[] { 0xff, 0xff, 0xff, 0xff }),
                Tuple.Create(-2, new byte[] { 0xff, 0xff, 0xff, 0xfe }),
                Tuple.Create(-5661, new byte[] { 0xff, 0xff, 0xe9, 0xe3 }),
                Tuple.Create(0x7fffffff, new byte[] { 0x7f, 0xff, 0xff, 0xff })
            };

            foreach (var v in values)
            {
                var buffer = new byte[4];
                EndianBitConverter.SetBytes(false, buffer, 0, v.Item1);
                CollectionAssert.AreEqual(v.Item2, buffer);
                EndianBitConverter.SetBytes(true, buffer, 0, v.Item1);
                CollectionAssert.AreEqual(v.Item2.Reverse(), buffer);
                var decoded = EndianBitConverter.ToInt32(false, v.Item2, 0);
                Assert.AreEqual(v.Item1, decoded);
                decoded = EndianBitConverter.ToInt32(true, v.Item2.Reverse().ToArray(), 0);
                Assert.AreEqual(v.Item1, decoded);
            }
        }