/// <summary>Save to a stream</summary> /// <param name="reference">reference to save</param> /// <param name="writer">The BinaryWriter to persist this object to. /// This method will alter the stream pointer of the underlying stream as a side effect. /// Passing null simply calculates how many bytes would be written.</param> /// <returns>number of bytes written including any padding</returns> static internal int Save(CompoundFileReference reference, BinaryWriter writer) { int bytes = 0; // NOTE: Our RefComponentType must be written by our caller bool calcOnly = (writer == null); // what are we dealing with here? CompoundFileStreamReference streamReference = reference as CompoundFileStreamReference; if ((streamReference == null) && (!(reference is CompoundFileStorageReference))) { throw new ArgumentException(SR.Get(SRID.UnknownReferenceSerialize), "reference"); } // first parse the path into strings string[] segments = ContainerUtilities.ConvertBackSlashPathToStringArrayPath(reference.FullName); int entries = segments.Length; // write the count if (!calcOnly) { writer.Write(entries); } bytes += ContainerUtilities.Int32Size; // write the segments - if we are dealing with a stream entry, don't write the last "segment" // because it is in fact a stream name for (int i = 0; i < segments.Length - (streamReference == null ? 0 : 1); i++) { if (!calcOnly) { writer.Write((Int32)RefComponentType.Storage); } bytes += ContainerUtilities.Int32Size; bytes += ContainerUtilities.WriteByteLengthPrefixedDWordPaddedUnicodeString(writer, segments[i]); } if (streamReference != null) { // we are responsible for the prefix if (!calcOnly) { writer.Write((Int32)RefComponentType.Stream); } bytes += ContainerUtilities.Int32Size; // write the stream name bytes += ContainerUtilities.WriteByteLengthPrefixedDWordPaddedUnicodeString(writer, segments[segments.Length - 1]); } return(bytes); }
/// <summary> /// Persist format version to the given stream /// </summary> /// <param name="stream">the stream to be written</param> /// <remarks> /// This operation will change the stream pointer /// stream can be null and will still return the number of bytes to be written /// </remarks> public int SaveToStream(Stream stream) { checked { // Suppress 56518 Local IDisposable object not disposed: // Reason: The stream is not owned by the BlockManager, therefore we can // close the BinaryWriter as it will Close the stream underneath. #pragma warning disable 6518 int len = 0; BinaryWriter binarywriter = null; #pragma warning restore 6518 if (stream != null) { binarywriter = new BinaryWriter(stream, System.Text.Encoding.Unicode); } // ************ // feature ID // ************ len += ContainerUtilities.WriteByteLengthPrefixedDWordPaddedUnicodeString(binarywriter, _featureIdentifier); // **************** // Reader Version // **************** if (stream != null) { binarywriter.Write(_reader.Major); // Major number binarywriter.Write(_reader.Minor); // Minor number } len += ContainerUtilities.Int16Size; len += ContainerUtilities.Int16Size; // ***************** // Updater Version // ***************** if (stream != null) { binarywriter.Write(_updater.Major); // Major number binarywriter.Write(_updater.Minor); // Minor number } len += ContainerUtilities.Int16Size; len += ContainerUtilities.Int16Size; // **************** // Writer Version // **************** if (stream != null) { binarywriter.Write(_writer.Major); // Major number binarywriter.Write(_writer.Minor); // Minor number } len += ContainerUtilities.Int16Size; len += ContainerUtilities.Int16Size; return(len); } }