// Asynchronously returns a stream that represents the data section of the current header. // If copyData is true, then a total number of _size bytes will be copied to a new MemoryStream, which is then returned. // Otherwise, if the archive stream is seekable, returns a seekable wrapper stream. // Otherwise, it returns an unseekable wrapper stream. private static async ValueTask <Stream?> GetDataStreamAsync(Stream archiveStream, bool copyData, long size, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (size == 0) { return(null); } if (copyData) { MemoryStream copiedData = new MemoryStream(); await TarHelpers.CopyBytesAsync(archiveStream, copiedData, size, cancellationToken).ConfigureAwait(false); return(copiedData); } return(archiveStream.CanSeek ? new SeekableSubReadStream(archiveStream, archiveStream.Position, size) : new SubReadStream(archiveStream, 0, size)); }