Example #1
0
        // TODO: Find a better way to do this.
        public byte[] ToBytes()
        {
            var bytes = new DynamicByteArray();
            var data  = new DynamicByteArray();

            // write type
            bytes.Write <byte>((byte)BlockType.Collection);

            // write name
            data.Write <byte>(0x1F);
            data.Write <short>((short)Name.Length);
            data.Write <string>(Name);

            // write tables
            foreach (var table in Tables)
            {
                data.Write <byte[]>(table.ToBytes());
            }

            // write length
            bytes.Write <int>(data.Length);

            // write the data
            bytes.Write <byte[]>(data.Bytes);

            return(bytes.Bytes);
        }
Example #2
0
        public void WriteByOffset()
        {
            var bytes = new DynamicByteArray(new byte[] { 1, 3, 5 });

            // write a 2
            bytes.Write <byte>(2, 1);

            // and then a 4
            bytes.Write <byte>(4, 3);

            Assert.AreEqual(2, bytes.Bytes[1]);
            Assert.AreEqual(4, bytes.Bytes[3]);
        }
        public void testByteArray()
        {
            DynamicByteArray dba = new DynamicByteArray(3, 10);

            dba.add((byte)0);
            dba.add((byte)1);
            dba.set(3, (byte)3);
            dba.set(2, (byte)2);
            dba.add((byte)4);
            Assert.Equal("{0,1,2,3,4}", dba.ToString());
            Assert.Equal(5, dba.size());
            byte[] val;
            val = new byte[0];
            Assert.Equal(0, dba.compare(val, 0, 0, 2, 0));
            Assert.Equal(-1, dba.compare(val, 0, 0, 2, 1));
            val = new byte[] { 3, 42 };
            Assert.Equal(1, dba.compare(val, 0, 1, 2, 0));
            Assert.Equal(1, dba.compare(val, 0, 1, 2, 1));
            Assert.Equal(0, dba.compare(val, 0, 1, 3, 1));
            Assert.Equal(-1, dba.compare(val, 0, 1, 3, 2));
            Assert.Equal(1, dba.compare(val, 0, 2, 3, 1));
            val = new byte[256];
            for (int b = -128; b < 128; ++b)
            {
                dba.add((byte)b);
                val[b + 128] = (byte)b;
            }
            Assert.Equal(0, dba.compare(val, 0, 256, 5, 256));
            Assert.Equal(1, dba.compare(val, 0, 1, 0, 1));
            Assert.Equal(1, dba.compare(val, 254, 1, 0, 1));
            Assert.Equal(1, dba.compare(val, 120, 1, 64, 1));
            val = new byte[1024];
            Random rand = new Random(1701);

            for (int i = 0; i < val.Length; ++i)
            {
                rand.NextBytes(val);
            }
            dba.add(val, 0, 1024);
            Assert.Equal(1285, dba.size());
            Assert.Equal(0, dba.compare(val, 0, 1024, 261, 1024));
        }
Example #4
0
        // TODO: Find a better way to do this.
        public byte[] ToBytes()
        {
            var bytes = new DynamicByteArray();

            var columns    = new DynamicByteArray();
            var columnData = new DynamicByteArray();

            var rows    = new DynamicByteArray();
            var rowData = new DynamicByteArray();

            // plug table name in before columns
            columns.Write <byte>(0x1F); // this is a string.
            columns.Write <short>((short)Name.Length);
            columns.Write <string>(Name);

            // write types
            bytes.Write <byte>((byte)BlockType.Table);
            columns.Write <byte>((byte)BlockType.Columns);
            rows.Write <byte>((byte)BlockType.Rows);

            // write columns
            foreach (var column in Columns)
            {
                columnData.Write <byte>(Constants.SEPARATOR);             //separator
                columnData.Write <byte>((byte)column.Type);               // the type
                columnData.Write <byte>(Convert.ToByte(column.Primary));  // primary bit
                columnData.Write <byte>(Convert.ToByte(column.Nullable)); // is-nullable bit
                columnData.Write <byte>(0x1F);                            // this is a string.
                columnData.Write <short>((short)column.Name.Length);
                columnData.Write <string>(column.Name);

                if (column.Default != null)
                {
                    columnData.Write <byte>(0x1E); // there is a default present.
                    if (column.Type == StructureType.String)
                    {
                        columnData.Write <byte>(0x1F); // this default is a string.
                        columnData.Write <short>((short)(column.Default as string).Length);
                        columnData.Write <string>(column.Default as string);
                    }
                    else
                    {
                        if (Structure.GetType(column.Type) == typeof(byte))
                        {
                            columnData.Write <byte>((byte)column.Default);
                        }
                    }
                }
            }

            foreach (var row in Rows)
            {
                rowData.Write <byte>(Constants.SEPARATOR); //separator
                foreach (var val in row.Values)
                {
                    if (val.GetType() == typeof(string))
                    {
                        rowData.Write <byte>(0x1F); // this is a string.
                        rowData.Write <short>((short)(val.Value as string).Length);
                        rowData.Write <string>(val.Value as string);
                    }
                    else if (val.GetType() == typeof(byte))
                    {
                        rowData.Write <byte>((byte)val.Value); //separator
                    }
                }
            }

            // write lengths
            columns.Write <int>(columnData.Length);
            rows.Write <int>(rowData.Length);

            // write data
            columns.Write <byte[]>(columnData.Bytes);
            rows.Write <byte[]>(rowData.Bytes);

            // write overall length
            bytes.Write <int>(columns.Length + rows.Length);

            // write overall data
            bytes.Write <byte[]>(columns.Bytes);
            bytes.Write <byte[]>(rows.Bytes);

            return(bytes.Bytes);
        }
 /// <summary>
 /// Construct a WSP buffer.
 /// </summary>
 /// <param name="bytes">Initial data in buffer.</param>
 public WspBuffer(byte[] bytes)
 {
     ReadOffset = 0;
     buffer     = new DynamicByteArray(bytes);
 }
 /// <summary>
 /// Construct an empty WSP buffer.
 /// </summary>
 public WspBuffer()
 {
     ReadOffset = 0;
     buffer     = new DynamicByteArray();
 }
Example #7
0
 internal static HandleRef getCPtr(DynamicByteArray obj) {
   return (obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr;
 }
Example #8
0
 internal static HandleRef getCPtr(DynamicByteArray obj)
 {
     return((obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr);
 }