private void Initialize() { _stream = File.OpenWrite(_filePath); _writer = new TrackingPipeWriter(PipeWriter.Create(_stream)); BlockHelpers.WriteInitialFileContext(_writer); _initialized = true; }
public ValueTask ProcessBlockAsync(NettraceBlock block, CancellationToken token = default) { if (!_initialized) { Initialize(); } Debug.Assert(_writer is not null); return(new (BlockHelpers.ProcessBlock(_writer, block, token).AsTask())); }
public static bool WriteBlockAndPreamble(TrackingPipeWriter writer, NettraceBlock block) { if (block.Type.Name != KnownTypeNames.EventBlockCompressed && block.Type.Name != KnownTypeNames.StackBlockCompressed) { return(false); } var padding = BlockHelpers.GetPadding(writer, block); var prefixLength = padding + sizeof(int); using var decoder = new BrotliDecoder(); var source = ArrayPool <byte> .Shared.Rent((int)block.BlockBody.Length); block.BlockBody.CopyTo(source); var written = 0; Memory <byte> memory; try { var length = prefixLength + (int)block.BlockBody.Length; memory = writer.GetMemory(length); while (!BrotliDecoder.TryDecompress(source.AsSpan(0, (int)block.BlockBody.Length), memory.Slice(prefixLength).Span, out written)) { length = memory.Length * 2; memory = writer.GetMemory(length); } } finally { ArrayPool <byte> .Shared.Return(source); } // Write size BitConverter.TryWriteBytes(memory.Span, written); // clear padding bits memory.Slice(sizeof(int), padding).Span.Clear(); writer.Advance(written + prefixLength); return(true); }