static TeaFile <T> Append(Stream stream, bool ownsStream, Stream headerReadStream, ItemDescriptionElements elementsToValidate = ItemDescriptionElements.All) { var tf = new TeaFile <T>(); try { TeaFileDescription description; tf.core = new TeaFileCore(stream, ownsStream); // A file opened for appending, cannot be read. Therefore we create a second file, read it and assign its description to the current TeaFile<> instance. using (var tfheader = OpenRead(headerReadStream, false, elementsToValidate)) // we pass headerStream ownership to tfheader, so it will be closed after header reaeding. { description = tfheader.Description; if (!tfheader.core.CanAppend) { throw new IOException("Cannot append to file because it has preallocated space between the end of the item area and the end of the file."); } tf.core.Assign(tfheader.ItemAreaStart, tfheader.core.ItemAreaEndMarker); } // if the stream is a filestream that was opened in FileMode.Append, this call is redundant. // this line is here for the allowed case were the stream and headerstream point to the same stream. tf.SetFilePointerToEnd(); tf.core.Description = description; tf.buffer = new SafeBuffer <T>(stream); return(tf); } catch (Exception) { tf.Dispose(); throw; } }