/// <summary>
 /// Write data (short)
 /// </summary>
 /// <param name="address"></param>
 /// <param name="data"></param>
 /// <returns></returns>
 public void Write(long address, short data)
 {
     Write(address, VSLib.ConvertShortToByte(data), 2);
 }
Exemple #2
0
 /// <summary>
 /// Set short value
 /// </summary>
 /// <param name="name"></param>
 /// <returns></returns>
 public void Set(string name, short value)
 {
     set_field(name, FIELD_TYPE_SHORT, VSLib.ConvertShortToByte(value));
 }
Exemple #3
0
        /// <summary>
        /// Serialize all fields to object fields space
        /// Free excessive chunks if needed
        /// </summary>
        private void serialize()
        {
            // If no fields
            if (FCache.Count == 0)
            {
                set_alloc(ALLOC_RESET);
                FIELDS_SIZE = 0;
                FreeSpace   = -1;
                FCache      = null;
                return;
            }

            byte[] b        = null;
            int    old_size = (int)(FIELDS_SIZE);                  // Current used size (number of fields + all fields size)

            // 1. Calculate new size
            int new_size = FIRST_FIELD_OFFSET_REL;

            for (int i = 0; i < FCache.Count; i++)
            {
                new_size += FCache[i].FULL_LENGTH;
            }

            // 2. Check if space is availabe, extend if required
            if (new_size > (old_size + FreeSpace))
            {
                sp.Extend(this, (new_size - old_size));
            }

            // 3. Check if only value update is required

            List <FieldCache> afc = FCache;

            FCache = new List <FieldCache>(32);

            int array_size = (new_size > old_size) ? new_size : old_size;

            byte[] data = new byte[array_size];


            int data_pos = FIRST_FIELD_OFFSET_REL;

            VSLib.CopyBytes(data, VSLib.ConvertIntToByte(new_size), FIELDS_SIZE_POS, FIELDS_SIZE_LEN);                          // Size
            VSLib.CopyBytes(data, VSLib.ConvertShortToByte((short)afc.Count), FIELDS_NUMBER_POS, FIELDS_NUMBER_LEN);            // Fields number

            FCache.Clear();
            for (int i = 0; i < afc.Count; i++)
            {
                FieldCache fc_element = afc[i];
                if (!fc_element.DELETED)
                {
                    fc_element.OFFSET = data_pos;

                    // Type
                    data[data_pos] = fc_element.TYPE;
                    data_pos++;

                    // Name length
                    data[data_pos] = (byte)(fc_element.NAME.Length);
                    data_pos++;

                    // Name
                    b = VSLib.ConvertStringToByte(fc_element.NAME);
                    VSLib.CopyBytes(data, b, data_pos, b.Length);
                    data_pos += b.Length;

                    // Value
                    if (FIELD_COMPRESS[fc_element.TYPE])                                                              // int/long/decimal
                    {
                        data[data_pos] = (byte)(fc_element.LENGTH);
                        data_pos++;
                    }
                    else if ((fc_element.TYPE == FIELD_TYPE_BYTES) | (fc_element.TYPE == FIELD_TYPE_STRING))
                    {
                        b = compress(VSLib.ConvertLongToByte(fc_element.LENGTH), FIELD_TYPE_LONG);
                        data[data_pos] = (byte)b.Length;
                        data_pos++;

                        if (b.Length > 0)
                        {
                            VSLib.CopyBytes(data, b, data_pos, b.Length);
                            data_pos += b.Length;
                        }
                    }

                    // Write value
                    if (fc_element.VALUE.Length > 0)
                    {
                        VSLib.CopyBytes(data, fc_element.VALUE, data_pos, fc_element.VALUE.Length);
                        data_pos += afc[i].LENGTH;
                    }

                    // Shift offset and add to cache
                    fc_element.OLDLENGTH = afc[i].LENGTH;
                    fc_element.STATE     = STATE_LOADED;

                    FCacheTree.Insert(fc_element.NAME, FCache.Count);
                    FCache.Add(fc_element);
                }
            }

            base.Write(base.FIXED, data, data.Length);

            set_alloc(ALLOC_RENEW);

            // Fill the rest by zeros if le length < old length
            if (new_size < old_size)
            {
                long full_used_size = base.FIXED + new_size;
                b = new byte[old_size - new_size];
                base.Write(full_used_size, b, b.Length);                              // Fill be zeros unused space

                // Multiple chunks, chech if there are exessive
                if (base.Chunk > 0)
                {
                    sp.ShrinkObject(this, full_used_size);
                }
            }

            FreeSpace = (int)(this.Size - base.FIXED - new_size);
        }
Exemple #4
0
 /// <summary>
 /// Write short
 /// </summary>
 /// <param name="offset"></param>
 /// <param name="data"></param>
 public void Write(long offset, short data)
 {
     byte[] b = VSLib.ConvertShortToByte(data);
     this.Write(offset, ref b);
 }