/// <summary> /// Writes the body length in the frame and returns the frame length /// </summary> public int Close() { //Set the length in the header //MemoryStream implementation length and offset are ints, so cast is safe var frameLength = Convert.ToInt32(_stream.Length - _offset); var lengthBytes = BeConverter.GetBytes(frameLength - _version.GetHeaderSize()); //The length could start at the 4th or 5th position long lengthOffset = 4; if (_version.Uses2BytesStreamIds()) { lengthOffset = 5; } //Set the position of the stream where the frame body length should be written _stream.Position = _offset + lengthOffset; _stream.Write(lengthBytes, 0, lengthBytes.Length); _stream.Position = _stream.Length; return(frameLength); }