public BinaryBufferReservation Reserve(int size) { Verify(); RequestSpace(size); if (!HasReservations) { // If less than half of the buffer size is left if (Size - Position < size / 2) { // Preemptive flush to ensure we have space to write // the data the reservation is about Flush(); } } var reservation = new BinaryBufferReservation(Position, size); for (var i = 0; i < size; i++) { Buffer[Position++] = 0; } _reservations.Add(reservation); if (_firstReservation == null) { _firstReservation = reservation; } return(reservation); }
public void Use(BinaryBufferReservation reservation) { Verify(); var value = (UInt32)(Position - reservation.Position); var buffer = BinaryInformation.UInt32.Converter.Convert(value); Use(reservation, buffer, 0, buffer.Length); }
public void Use(BinaryBufferReservation reservation, byte[] buffer, int offset, int length) { Verify(); if (buffer.Length > reservation.Size) { throw new ArgumentException("The supplied buffer can not exceed the reservation size"); } System.Buffer.BlockCopy(buffer, offset, Buffer, reservation.Position, buffer.Length); if (_firstReservation == reservation) { _reservations.RemoveAt(0); _firstReservation = _reservations.FirstOrDefault(); return; } _reservations.Remove(reservation); }