public void Write2DAToExport() { using (var stream = new MemoryStream()) { //Cell count if (IsIndexed) { //Indexed ones seem to have 0 at start stream.WriteBytes(BitConverter.GetBytes(0)); } stream.WriteBytes(BitConverter.GetBytes(PopulatedCellCount)); //Write cell data for (int rowindex = 0; rowindex < RowCount; rowindex++) { for (int colindex = 0; colindex < ColumnCount; colindex++) { Bio2DACell cell = Cells[rowindex, colindex]; if (cell != null) { if (IsIndexed) { //write index int index = (rowindex * ColumnCount) + colindex; //+1 because they are not zero based indexes since they are numerals stream.WriteBytes(BitConverter.GetBytes(index)); } stream.WriteByte((byte)cell.Type); stream.WriteBytes(cell.Data); } else { if (IsIndexed) { //this is a blank cell. It is not present in the table. continue; } else { Debug.WriteLine("THIS SHOULDN'T OCCUR!"); Debugger.Break(); throw new Exception("A non-indexed Bio2DA cannot have null cells."); } } } } //Write Columns if (!IsIndexed) { stream.WriteBytes(BitConverter.GetBytes(0)); //seems to be a 0 before column definitions } //Console.WriteLine("Columns defs start at " + stream.Position.ToString("X6")); stream.WriteBytes(BitConverter.GetBytes(ColumnCount)); for (int colindex = 0; colindex < ColumnCount; colindex++) { //Console.WriteLine("Writing column definition " + columnNames[colindex]); int nameIndexForCol = export.FileRef.FindNameOrAdd(ColumnNames[colindex]); stream.WriteBytes(BitConverter.GetBytes(nameIndexForCol)); stream.WriteBytes(BitConverter.GetBytes(0)); //second half of name reference in 2da is always zero since they're always indexed at 0 stream.WriteBytes(BitConverter.GetBytes(colindex)); } int propsEnd = export.propsEnd(); byte[] binarydata = stream.ToArray(); //Todo: Rewrite properties here PropertyCollection props = new PropertyCollection(); if (export.ClassName == "Bio2DA") { var indicies = new ArrayProperty <NameProperty>(ArrayType.Name, "m_sRowLabel"); foreach (var rowname in RowNames) { indicies.Add(new NameProperty { Value = rowname }); } props.Add(indicies); } else { var indices = new ArrayProperty <IntProperty>(ArrayType.Int, "m_lstRowNumbers"); foreach (var rowname in RowNames) { indices.Add(new IntProperty(int.Parse(rowname))); } props.Add(indices); } MemoryStream propsStream = new MemoryStream(); props.WriteTo(propsStream, export.FileRef); MemoryStream currentDataStream = new MemoryStream(export.Data); byte[] propertydata = propsStream.ToArray(); int propertyStartOffset = export.GetPropertyStart(); var newExportData = new byte[propertyStartOffset + propertydata.Length + binarydata.Length]; Buffer.BlockCopy(export.Data, 0, newExportData, 0, propertyStartOffset); propertydata.CopyTo(newExportData, propertyStartOffset); binarydata.CopyTo(newExportData, propertyStartOffset + propertydata.Length); //Console.WriteLine("Old data size: " + export.Data.Length); //Console.WriteLine("NEw data size: " + newExportData.Length); //This assumes the input and output data sizes are the same. We should not assume this with new functionality //if (export.Data.Length != newExportData.Length) //{ // Debug.WriteLine("FILES ARE WRONG SIZE"); // Debugger.Break(); //} export.Data = newExportData; } }