/// <summary> /// If this return it will read exactly length bytes, and unlike the /// regular read method fails if it cannot. /// </summary> /// <param name="s">The stream</param> /// <param name="buff">The buffer into which to write the data.</param> /// <param name="offset">The offset of the output array into which to write.</param> /// <param name="length">The number of bytes to read.</param> public static void ReadBlock(this Stream s, byte[] buff, int offset, int length) { int pos = 0; int read; while (pos != length) { read = s.Read(buff, offset + pos, length - pos); Contracts.CheckIO(read > 0, "Unexpected failure to read"); pos += read; } }
// Utilities for writing. /// <summary> /// Initialize the header and writer for writing. The value of fpMin and header /// should be passed to the other utility methods here. /// </summary> public static void BeginWrite(BinaryWriter writer, out long fpMin, out ModelHeader header) { Contracts.Assert(Marshal.SizeOf(typeof(ModelHeader)) == Size); Contracts.CheckValue(writer, nameof(writer)); fpMin = writer.FpCur(); header = default(ModelHeader); header.Signature = SignatureValue; header.VerWritten = VerWrittenCur; header.VerReadable = VerReadableCur; header.FpModel = ModelHeader.Size; // Write a blank header - the correct information is written by WriteHeaderAndTail. byte[] headerBytes = new byte[ModelHeader.Size]; writer.Write(headerBytes); Contracts.CheckIO(writer.FpCur() == fpMin + ModelHeader.Size); }