private void ConsolidateIfNeeded() { // Consolidate if the number of components will exceed the allowed maximum by the current // operation. int numComponents = _components.Count; if (numComponents > _maxNumComponents) { int capacity = _components[numComponents - 1]._endOffset; ByteBuf consolidated = AllocBuffer(capacity); // We're not using foreach to avoid creating an iterator. // noinspection ForLoopReplaceableByForEach for (int i = 0; i < numComponents; i++) { Component comp = _components[i]; ByteBuf b = comp._buf; consolidated.WriteBytes(b); //comp.FreeIfNecessary(); } var c = new Component(consolidated); c._endOffset = c._length; _components.Clear(); _components.Add(c); } }