/// <summary> /// Performs an operation using <see cref="BerkeleyDbStorage"/>'s built in /// pool of resizable buffers. If the operation returns a needed length greater /// than the initial buffer length, then the operation is repeated with an /// adequately sized buffer. /// </summary> /// <param name="requestedCapacity">The initial minimum buffer length.</param> /// <param name="dlg">The operation as a <see cref="BufferUse"/>.</param> /// <returns>An allocated <see cref="Byte"/> array containing the results of the /// operation, copied from the buffer and sized by the return length from /// <paramref name="dlg"/>.</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="dlg"/> is null. /// </exception> /// <exception cref="ArgumentOutOfRangeException"> /// <paramref name="requestedCapacity"/> is negative. /// </exception> public byte[] UsingMemoryPool(int requestedCapacity, BufferUse dlg) { if (requestedCapacity <= 0) { throw new ArgumentOutOfRangeException("requestedCapacity"); } if (dlg == null) { throw new ArgumentNullException("dlg"); } using (var itm = memoryPoolStream.GetItem()) { var stm = itm.Item; if (stm.Capacity < requestedCapacity) { stm.Capacity = requestedCapacity; } int size; byte[] buffer; while (true) { buffer = stm.GetBuffer(); try { size = dlg(buffer); } catch (BufferSmallException exc) { size = (int)exc.RecordLength; } if (size > buffer.Length) { stm.Capacity = size; } else { break; } } if (size < 0) { return(null); } var ret = new byte[size]; if (size > 0) { Array.Copy(buffer, ret, size); } return(ret); } }
/// <summary> /// Performs an operation using <see cref="BerkeleyDbStorage"/>'s built in /// pool of resizable buffers. If the operation returns a needed length greater /// than the initial buffer length, then the operation is repeated with an /// adequately sized buffer. /// </summary> /// <param name="requestedCapacity">The initial minimum buffer length.</param> /// <param name="dlg">The operation as a <see cref="BufferUse"/>.</param> /// <returns>An allocated <see cref="Byte"/> array containing the results of the /// operation, copied from the buffer and sized by the return length from /// <paramref name="dlg"/>.</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="dlg"/> is null. /// </exception> /// <exception cref="ArgumentOutOfRangeException"> /// <paramref name="requestedCapacity"/> is negative. /// </exception> public byte[] UsingMemoryPool(int requestedCapacity, BufferUse dlg) { if (requestedCapacity <= 0) throw new ArgumentOutOfRangeException("requestedCapacity"); if (dlg == null) throw new ArgumentNullException("dlg"); using (var itm = memoryPoolStream.GetItem()) { var stm = itm.Item; if (stm.Capacity < requestedCapacity) { stm.Capacity = requestedCapacity; } int size; byte[] buffer; while(true) { buffer = stm.GetBuffer(); try { size = dlg(buffer); } catch (BufferSmallException exc) { size = (int)exc.RecordLength; } if (size > buffer.Length) { stm.Capacity = size; } else { break; } } if (size < 0) return null; var ret = new byte[size]; if (size > 0) { Array.Copy(buffer, ret, size); } return ret; } }
/// <summary> /// Performs an operation using <see cref="BerkeleyDbStorage"/>'s built in /// pool of resizable buffers. If the operation returns a needed length greater /// than the initial buffer length, then the operation is repeated with an /// adequately sized buffer. /// </summary> /// <param name="dlg">The operation as a <see cref="BufferUse"/>.</param> /// <returns>An allocated <see cref="Byte"/> array containing the results of the /// operation, copied from the buffer and sized by the return length from /// <paramref name="dlg"/>.</returns> /// <remarks>The initial buffer size is at least as big as the /// default buffer size.</remarks> public byte[] UsingMemoryPool(BufferUse dlg) { return UsingMemoryPool(initialBufferSize, dlg); }
/// <summary> /// Performs an operation using <see cref="BerkeleyDbStorage"/>'s built in /// pool of resizable buffers. If the operation returns a needed length greater /// than the initial buffer length, then the operation is repeated with an /// adequately sized buffer. /// </summary> /// <param name="dlg">The operation as a <see cref="BufferUse"/>.</param> /// <returns>An allocated <see cref="Byte"/> array containing the results of the /// operation, copied from the buffer and sized by the return length from /// <paramref name="dlg"/>.</returns> /// <remarks>The initial buffer size is at least as big as the /// default buffer size.</remarks> public byte[] UsingMemoryPool(BufferUse dlg) { return(UsingMemoryPool(initialBufferSize, dlg)); }