private void reserve(int n) { Debug.Assert(_capacity == b.capacity()); if (n > _capacity) { _capacity = System.Math.Max(n, 2 * _capacity); _capacity = System.Math.Max(240, _capacity); } else if (n < _capacity) { _capacity = n; } else { return; } try { ByteBuffer buf = ByteBuffer.allocate(_capacity); if (b == _emptyBuffer) { b = buf; } else { int pos = b.position(); b.position(0); b.limit(System.Math.Min(_capacity, b.capacity())); buf.put(b); b = buf; b.limit(b.capacity()); b.position(pos); } b.order(_order); } catch (System.OutOfMemoryException) { _capacity = b.capacity(); // Restore the previous capacity throw; } catch (System.Exception ex) { _capacity = b.capacity(); // Restore the previous capacity. Ice.MarshalException e = new Ice.MarshalException(ex); e.reason = "unexpected exception while trying to allocate a ByteBuffer:\n" + ex; throw e; } finally { Debug.Assert(_capacity == b.capacity()); } }