public bool Write(ref Buffer buffer) { if (!buffer.HasSpace(Size)) { return(false); } buffer.WriteBytes <byte>((byte)DataType.DATA_ARRAY); buffer.WriteBytes(name); buffer.WriteBytes <byte>((byte)dataType); buffer.WriteBytes <uint>(count); uint s; if (dataType != DataType.DATA_STRING) { s = SizeOf(dataType) * count; } else { s = size; } buffer.Copy(data, s); return(true); }
public bool Write(ref Buffer buffer) { if (!buffer.HasSpace((uint)Size)) { return(false); } buffer.WriteBytes <ushort>((ushort)version); if (version == Global.Version.VERSION_INVALID) { throw new ArgumentOutOfRangeException("version", "Invalid database version!"); } switch (version) { case Global.Version.VERSION_1_0: if (Objects.Count > 65536) { throw new OverflowException("Too many objects!"); } if (this.Size > 4294967296) { throw new OverflowException("Database size is too big!"); // 2^32, maximum database size } buffer.WriteBytes(name); buffer.WriteBytes <uint>((uint)Size); buffer.WriteBytes <ushort>((ushort)Objects.Count); foreach (Object obj in Objects) { obj.Write(ref buffer); } break; case Global.Version.VERSION_2_0: if (Objects.Count > 65536) { throw new OverflowException("Too many objects!"); } if (this.Size > 4294967296) { throw new OverflowException("Database size is too big!"); // 2^32, maximum database size } uint size = (uint)this.Size - sizeof(short) - sizeof(short) - (uint)name.Length - sizeof(uint); Buffer tempBuffer = new Buffer(size); buffer.WriteBytes(name); tempBuffer.WriteBytes((uint)this.Size); tempBuffer.WriteBytes((ushort)Objects.Count); foreach (Object obj in Objects) { obj.Write(ref tempBuffer); } uint checksum = crc32(tempBuffer.Data, size); buffer.WriteBytes(checksum); buffer.Copy(ref tempBuffer); break; default: throw new ArgumentOutOfRangeException("version", "Invalid database version!"); } return(true); }