Esempio n. 1
0
        private static void SaveLong(Pickler /*!*/ pickler, object obj)
        {
            Debug.Assert(obj.GetType() == typeof(BigInteger), "arg must be long");

            BigInteger bi = (BigInteger)obj;

            if (bi.IsZero)
            {
                pickler.Write(Opcode.Long1);
                pickler.WriteUInt8(0);
            }
            else if (bi <= MaxInt && bi >= MinInt)
            {
                pickler.Write(Opcode.Long1);
                int value = (int)bi;
                if (IsInt8(value))
                {
                    pickler.WriteUInt8(1);
                    pickler._file.Write((char)(byte)value);
                }
                else if (IsInt16(value))
                {
                    pickler.WriteUInt8(2);
                    pickler.WriteUInt8(value & 0xff);
                    pickler.WriteUInt8((value >> 8) & 0xff);
                }
                else
                {
                    pickler.WriteUInt8(4);
                    pickler.WriteInt32(value);
                }
            }
            else
            {
                byte[] dataBytes = bi.ToByteArray();
                if (dataBytes.Length < 256)
                {
                    pickler.Write(Opcode.Long1);
                    pickler.WriteUInt8(dataBytes.Length);
                }
                else
                {
                    pickler.Write(Opcode.Long4);
                    pickler.WriteInt32(dataBytes.Length);
                }

                foreach (byte b in dataBytes)
                {
                    pickler.WriteUInt8(b);
                }
            }
        }
Esempio n. 2
0
 private static void SaveInteger(Pickler /*!*/ pickler, object obj)
 {
     Debug.Assert(obj.GetType() == typeof(int), "arg must be int");
     if (IsUInt8(obj))
     {
         pickler.Write(Opcode.BinInt1);
         pickler.WriteUInt8(obj);
     }
     else if (IsUInt16(obj))
     {
         pickler.Write(Opcode.BinInt2);
         pickler.WriteUInt16(obj);
     }
     else
     {
         pickler.Write(Opcode.BinInt);
         pickler.WriteInt32(obj);
     }
 }