// 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); }
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]); }
// 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); }