/// <summary> /// Starts a new segment and updates all the counters. /// </summary> /// <param name="length">The length of the new segment.</param> public void Flush(int length = 1024) { this.length += currentSegment.Length; currentSegment.next = new ManagedBinaryMemoryWriterSegment(this, length); currentSegment = currentSegment.next; }
internal void flush(ManagedBinaryMemoryWriterSegment initiator, int length = 1024) { if (initiator != currentSegment) { throw new InvalidOperationException("An insertionpoint can't increase in size."); } this.length += currentSegment.Length; currentSegment.next = new ManagedBinaryMemoryWriterSegment(this, length); currentSegment = currentSegment.next; }
/// <summary> /// Creates an insertionpoint to which you can write data AFTER you continued writing things to the writer itself. /// /// You need to call finish onto the segment after you have finished writing to the insertionpoint. /// </summary> /// <param name="length">The amount of bytes you wanna write to.</param> /// <returns>The segment for the insertion point.</returns> public ManagedBinaryMemoryWriterSegment MakeInsertionpoint(int length) { this.length += currentSegment.Length; currentSegment.next = new ManagedBinaryMemoryWriterSegment(this, length); ManagedBinaryMemoryWriterSegment result = currentSegment.next; result.next = new ManagedBinaryMemoryWriterSegment(this, 1024); currentSegment = result.next; return(result); }
/// <summary> /// Saves writer content to the given pointer. /// </summary> /// <param name="ptr">The Pointer we write to.</param> /// <returns>The amount of bytes written.</returns> public unsafe long ToPointer(byte *ptr) { long size = 0; ManagedBinaryMemoryWriterSegment segment = this.segment; while (segment != null) { size += segment.ToPointer(ref ptr); segment = segment.next; } return((int)size); }
/// <summary> /// Saves writer content to this stream. /// </summary> /// <param name="stream">The stream to save to.</param> /// <returns>The bytes written.</returns> public long ToStream(Stream stream) { long size = 0; ManagedBinaryMemoryWriterSegment segment = this.segment; while (segment != null) { size += segment.ToStream(stream); segment = segment.next; } return(size); }
/// <summary> /// Saves writer content to the given byte[] and it's offset. /// </summary> /// <param name="data">The byte[].</param> /// <param name="offset">The offset.</param> /// <returns></returns> public int ToArray(byte[] data, int offset) { long size = length + currentSegment.Length; if (data.Length < size + offset) { throw new ArgumentException("Not enough memory in data.", "data"); } int position = offset; ManagedBinaryMemoryWriterSegment segment = this.segment; while (segment != null) { segment.ToArray(data, ref position); segment = segment.next; } return((int)size); }
/// <summary> /// Generates a new array with the contents of this writer. Beware, the content shouldn't exceed .NET memory array limit of nearly 2 GiB. /// </summary> /// <returns>The array.</returns> public byte[] ToArray() { long size = length + currentSegment.Length; if (size > 2147000000) { throw new OutOfMemoryException("Array buffers can't exceed 2147000000 bytes. Write to a stream, if you generated bigger data than the near 2 GiB limit."); } byte[] data = new byte[size]; int position = 0; ManagedBinaryMemoryWriterSegment segment = this.segment; while (segment != null) { segment.ToArray(data, ref position); segment = segment.next; } return(data); }
/// <summary> /// Instantiates a managed binary memory writer. /// </summary> public ManagedBinaryMemoryWriter() { segment = new ManagedBinaryMemoryWriterSegment(this, 64); currentSegment = segment; }