Ejemplo n.º 1
0
        public override void WriteExtras(OperationBuilder builder)
        {
            Span <byte> extras = stackalloc byte[8];

            Flags       = Transcoder.GetFormat(Content);
            Format      = Flags.DataFormat;
            Compression = Flags.Compression;

            byte format      = (byte)Format;
            byte compression = (byte)Compression;

            BitUtils.SetBit(ref extras[0], 0, BitUtils.GetBit(format, 0));
            BitUtils.SetBit(ref extras[0], 1, BitUtils.GetBit(format, 1));
            BitUtils.SetBit(ref extras[0], 2, BitUtils.GetBit(format, 2));
            BitUtils.SetBit(ref extras[0], 3, BitUtils.GetBit(format, 3));
            BitUtils.SetBit(ref extras[0], 4, false);
            BitUtils.SetBit(ref extras[0], 5, BitUtils.GetBit(compression, 0));
            BitUtils.SetBit(ref extras[0], 6, BitUtils.GetBit(compression, 1));
            BitUtils.SetBit(ref extras[0], 7, BitUtils.GetBit(compression, 2));

            var typeCode = (ushort)Flags.TypeCode;

            ByteConverter.FromUInt16(typeCode, extras.Slice(2));
            ByteConverter.FromUInt32(Expires, extras.Slice(4));

            builder.Write(extras);
        }
        public virtual void ReadExtras(ReadOnlySpan <byte> buffer)
        {
            if (buffer.Length > Header.ExtrasOffset)
            {
                var format = new byte();
                var flags  = buffer[Header.ExtrasOffset];
                BitUtils.SetBit(ref format, 0, BitUtils.GetBit(flags, 0));
                BitUtils.SetBit(ref format, 1, BitUtils.GetBit(flags, 1));
                BitUtils.SetBit(ref format, 2, BitUtils.GetBit(flags, 2));
                BitUtils.SetBit(ref format, 3, BitUtils.GetBit(flags, 3));

                var compression = new byte();
                BitUtils.SetBit(ref compression, 4, BitUtils.GetBit(flags, 4));
                BitUtils.SetBit(ref compression, 5, BitUtils.GetBit(flags, 5));
                BitUtils.SetBit(ref compression, 6, BitUtils.GetBit(flags, 6));

                var typeCode = (TypeCode)(ByteConverter.ToUInt16(buffer.Slice(26)) & 0xff);
                Format            = (DataFormat)format;
                Compression       = (Compression)compression;
                Flags.DataFormat  = Format;
                Flags.Compression = Compression;
                Flags.TypeCode    = typeCode;
                Expires           = ByteConverter.ToUInt32(buffer.Slice(25));
            }
        }
        public void When_ByteArray_Is_Stored_With_Legacy_Flags_It_Is_Decoded_As_A_ByteArray()
        {
            var legacyByteArray = new byte[]
            {
                129, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 8, 0,
                0, 0, 5, 19, 185, 8, 248, 3, 104, 208, 188, 0,
                0, 250, 82, 116, 101, 115, 116
            };

            var converter = new DefaultConverter();
            var format    = new byte();

            var temp = legacyByteArray[24];

            BitUtils.SetBit(ref format, 0, BitUtils.GetBit(temp, 0));
            BitUtils.SetBit(ref format, 1, BitUtils.GetBit(temp, 1));
            BitUtils.SetBit(ref format, 2, BitUtils.GetBit(temp, 2));
            BitUtils.SetBit(ref format, 3, BitUtils.GetBit(temp, 3));

            var compression = new byte();

            BitUtils.SetBit(ref compression, 4, BitUtils.GetBit(temp, 4));
            BitUtils.SetBit(ref compression, 5, BitUtils.GetBit(temp, 5));
            BitUtils.SetBit(ref compression, 6, BitUtils.GetBit(temp, 6));

            var flags = new Flags
            {
                DataFormat  = (DataFormat)format,
                Compression = (Compression)compression,
                TypeCode    = (TypeCode)(converter.ToUInt16(legacyByteArray.AsSpan(26)) & 0xff),
            };

            var transcoder = new DefaultTranscoder(new DefaultConverter());
            var result     = transcoder.Decode <byte[]>(legacyByteArray.AsMemory(28, 4), flags, OpCode.Get);

            Assert.Equal("test", Encoding.UTF8.GetString(result));
        }
        public void Test_GetBit(byte value, int position, bool expected)
        {
            var actual = BitUtils.GetBit(value, position);

            Assert.Equal(expected, actual);
        }