/// <summary> /// Copies to the given stream. /// </summary> public override long CopyTo(Stream stream) { var initialPosition = stream.Position; stream.Write(BitConverter.GetBytes(_index.Length), 0, 8); stream.Seek(stream.Position + 8, System.IO.SeekOrigin.Begin); // leave room for the coordinates-size. // rewrite the coordinates array in the process removing all empty spaces after removing items. var position = stream.Position; var coordinatesPosition = position + (_index.Length * 8); long newPointer = 0; using (var indexStream = new BinaryWriter(new LimitedStream(stream, position))) { using (var coordinatesStream = new BinaryWriter(new LimitedStream(stream, coordinatesPosition))) { long pointer; int size; for (var i = 0; i < _index.Length; i++) { coordinatesStream.SeekBegin(newPointer * 4); ShapesArray.ExtractPointerAndSize(_index[i], out pointer, out size); if (size > 0) { for (var p = 0; p < size; p++) { coordinatesStream.Write(_coordinates[pointer + (p * 2)]); coordinatesStream.Write(_coordinates[pointer + (p * 2) + 1]); } indexStream.SeekBegin(i * 8); indexStream.Write(ShapesArray.BuildPointerAndSize(newPointer, size)); newPointer += size * 2; } else { indexStream.SeekBegin(i * 8); indexStream.Write((ulong)0); } } } } // write coordinates size. stream.Seek(initialPosition + 8, System.IO.SeekOrigin.Begin); stream.Write(BitConverter.GetBytes(newPointer), 0, 8); // seek until after. var sizeInBytes = 16 + (_index.Length * 8) + (newPointer * 4); stream.Seek(initialPosition + sizeInBytes, System.IO.SeekOrigin.Begin); return(sizeInBytes); }
/// <summary> /// Sets the index and size. /// </summary> private void SetPointerAndSize(long id, long pointer, int size) { _index[id] = ShapesArray.BuildPointerAndSize(pointer, size); }