internal void Write() { var localDiagnostics = new DiagnosticBag(); ArArchiveFile.UpdateLayout(localDiagnostics); if (localDiagnostics.HasErrors) { throw new ObjectFileException("Invalid ar file", localDiagnostics); } // Copy for warnings localDiagnostics.CopyTo(Diagnostics); _startStreamOffset = Stream.Position; Stream.Write(ArArchiveFile.Magic); Span <byte> entryBuffer = stackalloc byte[ArFile.FileEntrySizeInBytes]; var headers = ArArchiveFile.LongNamesTable; // Serialize all file entries for (var i = 0; i < ArArchiveFile.Files.Count; i++) { var file = ArArchiveFile.Files[i]; // Serialize the headers at the correct position only if they are required if (headers != null && headers.Index == i && headers.Size > 0) { WriteFileEntry(entryBuffer, headers); if (Diagnostics.HasErrors) { break; } } WriteFileEntry(entryBuffer, file); if (Diagnostics.HasErrors) { break; } } if (Diagnostics.HasErrors) { throw new ObjectFileException("Unexpected error while writing ar file", Diagnostics); } }
/// <summary> /// Tries to reads an 'ar' archive file from the specified stream. /// </summary> /// <param name="stream">The stream to read 'ar' a archive file from.</param> /// <param name="options">The options used for reading this 'ar' file.</param> /// <param name="arArchiveFile">The output 'ar' archive file if the read was successful.</param> /// <param name="diagnostics">The output associated diagnostics after reading the archive.</param> /// <returns><c>true</c> An instance of <see cref="ArArchiveFile"/> if the read was successful.</returns> public static bool TryRead(Stream stream, ArArchiveFileReaderOptions options, out ArArchiveFile arArchiveFile, out DiagnosticBag diagnostics) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } if (options == null) { throw new ArgumentNullException(nameof(options)); } arArchiveFile = new ArArchiveFile { Kind = options.ArchiveKind }; var reader = new ArArchiveFileReader(arArchiveFile, stream, options); diagnostics = reader.Diagnostics; reader.Read(); return(!reader.Diagnostics.HasErrors); }
internal ArArchiveFileWriter(ArArchiveFile archiveFile, Stream stream) : base(stream) { ArArchiveFile = archiveFile; IsReadOnly = false; }
internal ArArchiveFileReader(ArArchiveFile arArchiveFile, Stream stream, ArArchiveFileReaderOptions options) : base(stream) { ArArchiveFile = arArchiveFile; Options = options; IsReadOnly = options.IsReadOnly; }
/// <summary> /// Tries to reads an 'ar' archive file from the specified stream. /// </summary> /// <param name="stream">The stream to read 'ar' a archive file from.</param> /// <param name="archiveKind">The type of 'ar' archive to read.</param> /// <param name="arArchiveFile">The output 'ar' archive file if the read was successful.</param> /// <param name="diagnostics">The output associated diagnostics after reading the archive.</param> /// <returns><c>true</c> An instance of <see cref="ArArchiveFile"/> if the read was successful.</returns> public static bool TryRead(Stream stream, ArArchiveKind archiveKind, out ArArchiveFile arArchiveFile, out DiagnosticBag diagnostics) { return(TryRead(stream, new ArArchiveFileReaderOptions(archiveKind), out arArchiveFile, out diagnostics)); }