2 // XmlBinaryDictionaryWriter.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2005, 2007 Novell, Inc. http://www.novell.com
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections;
32 using System.Collections.Specialized;
33 using System.Collections.Generic;
34 using System.Globalization;
39 using BF = System.Xml.XmlBinaryFormat;
43 internal partial class XmlBinaryDictionaryWriter : XmlDictionaryWriter
45 class MyBinaryWriter : BinaryWriter
47 public MyBinaryWriter (Stream s)
52 public void WriteFlexibleInt (int value)
54 Write7BitEncodedInt (value);
59 MyBinaryWriter original, writer, buffer_writer;
60 IXmlDictionary dict_ext;
61 XmlDictionary dict_int = new XmlDictionary ();
62 XmlBinaryWriterSession session;
64 Encoding utf8Enc = new UTF8Encoding ();
65 MemoryStream buffer = new MemoryStream ();
67 const string XmlNamespace = "http://www.w3.org/XML/1998/namespace";
68 const string XmlnsNamespace = "http://www.w3.org/2000/xmlns/";
70 WriteState state = WriteState.Start;
71 bool open_start_element = false;
72 // transient current node info
73 List<KeyValuePair<string,object>> namespaces = new List<KeyValuePair<string,object>> ();
74 string xml_lang = null;
75 XmlSpace xml_space = XmlSpace.None;
78 Stack<int> ns_index_stack = new Stack<int> ();
79 Stack<string> xml_lang_stack = new Stack<string> ();
80 Stack<XmlSpace> xml_space_stack = new Stack<XmlSpace> ();
81 Stack<string> element_ns_stack = new Stack<string> ();
82 string element_ns = String.Empty;
84 string element_prefix; // only meaningful at Element state
85 // current attribute info
87 string current_attr_prefix;
88 object current_attr_name, current_attr_ns;
89 bool attr_typed_value;
90 SaveTarget save_target;
99 // XmlWriterSettings support
105 public XmlBinaryDictionaryWriter (Stream stream,
106 IXmlDictionary dictionary,
107 XmlBinaryWriterSession session, bool ownsStream)
109 if (dictionary == null)
110 dictionary = new XmlDictionary ();
112 session = new XmlBinaryWriterSession ();
114 original = new MyBinaryWriter (stream);
115 this.writer = original;
116 buffer_writer = new MyBinaryWriter (buffer);
117 this.dict_ext = dictionary;
118 this.session = session;
119 owns_stream = ownsStream;
121 AddNamespace ("xml", "http://www.w3.org/XML/1998/namespace");
122 AddNamespace ("xml", "http://www.w3.org/2000/xmlns/");
130 public override WriteState WriteState {
131 get { return state; }
134 public override string XmlLang {
135 get { return xml_lang; }
138 public override XmlSpace XmlSpace {
139 get { return xml_space; }
146 private void AddMissingElementXmlns ()
148 // push new namespaces to manager.
149 for (int i = ns_index; i < namespaces.Count; i++) {
150 var ent = namespaces [i];
151 string prefix = (string) ent.Key;
152 string ns = ent.Value as string;
153 XmlDictionaryString dns = ent.Value as XmlDictionaryString;
155 if (prefix.Length > 0) {
156 writer.Write (BF.PrefixNSString);
157 writer.Write (prefix);
160 writer.Write (BF.DefaultNSString);
163 if (prefix.Length > 0) {
164 writer.Write (BF.PrefixNSIndex);
165 writer.Write (prefix);
168 writer.Write (BF.DefaultNSIndex);
169 WriteDictionaryIndex (dns);
172 ns_index = namespaces.Count;
175 private void CheckState ()
177 if (state == WriteState.Closed) {
178 throw new InvalidOperationException ("The Writer is closed.");
182 void ProcessStateForContent ()
186 if (state == WriteState.Element)
187 CloseStartElement ();
189 ProcessPendingBuffer (false, false);
190 if (state != WriteState.Attribute)
191 writer = buffer_writer;
194 void ProcessTypedValue ()
196 ProcessStateForContent ();
197 if (state == WriteState.Attribute) {
198 if (attr_typed_value)
199 throw new InvalidOperationException (String.Format ("A typed value for the attribute '{0}' in namespace '{1}' was already written", current_attr_name, current_attr_ns));
200 attr_typed_value = true;
204 void ProcessPendingBuffer (bool last, bool endElement)
206 if (buffer.Position > 0) {
207 byte [] arr = buffer.GetBuffer ();
210 original.Write (arr, 0, (int) buffer.Position);
211 buffer.SetLength (0);
217 public override void Close ()
219 CloseOpenAttributeAndElements ();
223 else if (state != WriteState.Closed)
225 state = WriteState.Closed;
228 private void CloseOpenAttributeAndElements ()
230 CloseStartElement ();
232 while (element_count > 0)
236 private void CloseStartElement ()
238 if (!open_start_element)
241 if (state == WriteState.Attribute)
242 WriteEndAttribute ();
244 AddMissingElementXmlns ();
246 state = WriteState.Content;
247 open_start_element = false;
250 public override void Flush ()
255 public override string LookupPrefix (string ns)
257 if (ns == null || ns == String.Empty)
258 throw new ArgumentException ("The Namespace cannot be empty.");
260 var de = namespaces.LastOrDefault (i => i.Value.ToString () == ns);
261 return de.Key; // de is KeyValuePair and its default key is null.
264 public override void WriteBase64 (byte[] buffer, int index, int count)
267 throw new IndexOutOfRangeException ("Negative count");
268 ProcessStateForContent ();
271 writer.Write (BF.Bytes8);
272 writer.Write ((byte) count);
273 writer.Write (buffer, index, count);
274 } else if (count < 0x10000) {
275 writer.Write (BF.Bytes8);
276 writer.Write ((ushort) count);
277 writer.Write (buffer, index, count);
279 writer.Write (BF.Bytes32);
280 writer.Write (count);
281 writer.Write (buffer, index, count);
285 public override void WriteCData (string text)
287 if (text.IndexOf ("]]>") >= 0)
288 throw new ArgumentException ("CDATA section cannot contain text \"]]>\".");
290 ProcessStateForContent ();
292 WriteTextBinary (text);
295 public override void WriteCharEntity (char ch)
297 WriteChars (new char [] {ch}, 0, 1);
300 public override void WriteChars (char[] buffer, int index, int count)
302 ProcessStateForContent ();
304 int blen = Encoding.UTF8.GetByteCount (buffer, index, count);
307 writer.Write (BF.EmptyText);
308 else if (blen < 0x100) {
309 writer.Write (BF.Chars8);
310 writer.Write ((byte) blen);
311 writer.Write (buffer, index, count);
312 } else if (blen < 0x10000) {
313 writer.Write (BF.Chars16);
314 writer.Write ((ushort) blen);
315 writer.Write (buffer, index, count);
317 writer.Write (BF.Chars32);
319 writer.Write (buffer, index, count);
323 public override void WriteComment (string text)
325 if (text.EndsWith("-"))
326 throw new ArgumentException ("An XML comment cannot contain \"--\" inside.");
327 else if (text.IndexOf("--") > 0)
328 throw new ArgumentException ("An XML comment cannot end with \"-\".");
330 ProcessStateForContent ();
332 if (state == WriteState.Attribute)
333 throw new InvalidOperationException ("Comment node is not allowed inside an attribute");
335 writer.Write (BF.Comment);
339 public override void WriteDocType (string name, string pubid, string sysid, string subset)
341 throw new NotSupportedException ("This XmlWriter implementation does not support document type.");
344 public override void WriteEndAttribute ()
346 if (state != WriteState.Attribute)
347 throw new InvalidOperationException("Token EndAttribute in state Start would result in an invalid XML document.");
351 if (attr_value == null)
352 attr_value = String.Empty;
354 switch (save_target) {
355 case SaveTarget.XmlLang:
356 xml_lang = attr_value;
358 case SaveTarget.XmlSpace:
359 switch (attr_value) {
361 xml_space = XmlSpace.Preserve;
364 xml_space = XmlSpace.Default;
367 throw new ArgumentException (String.Format ("Invalid xml:space value: '{0}'", attr_value));
370 case SaveTarget.Namespaces:
371 if (current_attr_name.ToString ().Length > 0 && attr_value.Length == 0)
372 throw new ArgumentException ("Cannot use prefix with an empty namespace.");
374 AddNamespaceChecked (current_attr_name.ToString (), attr_value);
377 if (!attr_typed_value)
378 WriteTextBinary (attr_value);
382 if (current_attr_prefix.Length > 0 && save_target != SaveTarget.Namespaces)
383 AddNamespaceChecked (current_attr_prefix, current_attr_ns);
385 state = WriteState.Element;
386 current_attr_prefix = null;
387 current_attr_name = null;
388 current_attr_ns = null;
390 attr_typed_value = false;
393 public override void WriteEndDocument ()
395 CloseOpenAttributeAndElements ();
398 case WriteState.Start:
399 throw new InvalidOperationException ("Document has not started.");
400 case WriteState.Prolog:
401 throw new ArgumentException ("This document does not have a root element.");
404 state = WriteState.Start;
407 bool SupportsCombinedEndElementSupport (byte operation)
416 public override void WriteEndElement ()
418 if (element_count-- == 0)
419 throw new InvalidOperationException("There was no XML start tag open.");
421 if (state == WriteState.Attribute)
422 WriteEndAttribute ();
424 // Comment+EndElement does not exist
425 bool needExplicitEndElement = buffer.Position == 0 || !SupportsCombinedEndElementSupport (buffer.GetBuffer () [0]);
426 ProcessPendingBuffer (true, !needExplicitEndElement);
428 AddMissingElementXmlns ();
430 if (needExplicitEndElement)
431 writer.Write (BF.EndElement);
433 element_ns = element_ns_stack.Pop ();
434 xml_lang = xml_lang_stack.Pop ();
435 xml_space = xml_space_stack.Pop ();
436 int cur = namespaces.Count;
437 ns_index = ns_index_stack.Pop ();
438 namespaces.RemoveRange (ns_index, cur - ns_index);
439 open_start_element = false;
444 public override void WriteEntityRef (string name)
446 throw new NotSupportedException ("This XmlWriter implementation does not support entity references.");
449 public override void WriteFullEndElement ()
454 public override void WriteProcessingInstruction (string name, string text)
457 throw new ArgumentException ("Processing instructions are not supported. ('xml' is allowed for XmlDeclaration; this is because of design problem of ECMA XmlWriter)");
458 // Otherwise, silently ignored. WriteStartDocument()
459 // is still callable after this method(!)
462 public override void WriteQualifiedName (XmlDictionaryString local, XmlDictionaryString ns)
464 string prefix = namespaces.LastOrDefault (i => i.Value.ToString () == ns.ToString ()).Key;
465 bool use_dic = prefix != null;
467 prefix = LookupPrefix (ns.Value);
469 throw new ArgumentException (String.Format ("Namespace URI '{0}' is not bound to any of the prefixes", ns));
471 ProcessTypedValue ();
473 if (use_dic && prefix.Length == 1) {
474 writer.Write (BF.QNameIndex);
475 writer.Write ((byte) (prefix [0] - 'a'));
476 WriteDictionaryIndex (local);
478 // QNameIndex is not applicable.
479 WriteString (prefix);
485 public override void WriteRaw (string data)
490 public override void WriteRaw (char[] buffer, int index, int count)
492 WriteChars (buffer, index, count);
495 void CheckStateForAttribute ()
499 if (state != WriteState.Element)
500 throw new InvalidOperationException ("Token StartAttribute in state " + WriteState + " would result in an invalid XML document.");
503 string CreateNewPrefix ()
505 return CreateNewPrefix (String.Empty);
508 string CreateNewPrefix (string p)
510 for (char c = 'a'; c <= 'z'; c++)
511 if (!namespaces.Any (iter => iter.Key == p + c))
513 for (char c = 'a'; c <= 'z'; c++) {
514 var s = CreateNewPrefix (c.ToString ());
518 throw new InvalidOperationException ("too many prefix population");
521 bool CollectionContains (ICollection col, string value)
523 foreach (string v in col)
529 void ProcessStartAttributeCommon (ref string prefix, string localName, string ns, object nameObj, object nsObj)
531 // dummy prefix is created here, while the caller
532 // still uses empty string as the prefix there.
533 if (prefix.Length == 0 && ns.Length > 0) {
534 prefix = LookupPrefix (ns);
535 // Not only null but also ""; when the returned
536 // string is empty, then it still needs a dummy
537 // prefix, since default namespace does not
538 // apply to attribute
539 if (String.IsNullOrEmpty (prefix))
540 prefix = CreateNewPrefix ();
542 else if (prefix.Length > 0 && ns.Length == 0)
543 throw new ArgumentException ("Cannot use prefix with an empty namespace.");
544 // here we omit such cases that it is used for writing
545 // namespace-less xml, unlike XmlTextWriter.
546 if (prefix == "xmlns" && ns != XmlnsNamespace)
547 throw new ArgumentException (String.Format ("The 'xmlns' attribute is bound to the reserved namespace '{0}'", XmlnsNamespace));
549 CheckStateForAttribute ();
551 state = WriteState.Attribute;
553 save_target = SaveTarget.None;
556 // MS.NET looks to allow other names than
557 // lang and space (e.g. xml:link, xml:hack).
561 save_target = SaveTarget.XmlLang;
564 save_target = SaveTarget.XmlSpace;
569 save_target = SaveTarget.Namespaces;
573 current_attr_prefix = prefix;
574 current_attr_name = nameObj;
575 current_attr_ns = nsObj;
578 public override void WriteStartAttribute (string prefix, string localName, string ns)
581 prefix = String.Empty;
584 if (localName == "xmlns" && prefix.Length == 0) {
586 localName = String.Empty;
589 ProcessStartAttributeCommon (ref prefix, localName, ns, localName, ns);
591 // for namespace nodes we don't write attribute node here.
592 if (save_target == SaveTarget.Namespaces)
595 byte op = prefix.Length == 1 && 'a' <= prefix [0] && prefix [0] <= 'z' ?
596 (byte) (prefix [0] - 'a' + BF.PrefixNAttrStringStart) :
597 prefix.Length == 0 ? BF.AttrString :
600 if (BF.PrefixNAttrStringStart <= op && op <= BF.PrefixNAttrStringEnd) {
602 writer.Write (localName);
605 if (prefix.Length > 0)
606 writer.Write (prefix);
607 writer.Write (localName);
611 public override void WriteStartDocument ()
613 WriteStartDocument (false);
616 public override void WriteStartDocument (bool standalone)
618 if (state != WriteState.Start)
619 throw new InvalidOperationException("WriteStartDocument should be the first call.");
623 // write nothing to stream.
625 state = WriteState.Prolog;
628 void PrepareStartElement ()
630 ProcessPendingBuffer (true, false);
632 CloseStartElement ();
636 element_ns_stack.Push (element_ns);
637 xml_lang_stack.Push (xml_lang);
638 xml_space_stack.Push (xml_space);
639 ns_index_stack.Push (ns_index);
642 public override void WriteStartElement (string prefix, string localName, string ns)
644 PrepareStartElement ();
646 if ((prefix != null && prefix != String.Empty) && ((ns == null) || (ns == String.Empty)))
647 throw new ArgumentException ("Cannot use a prefix with an empty namespace.");
651 if (ns == String.Empty)
652 prefix = String.Empty;
654 prefix = String.Empty;
656 byte op = prefix.Length == 1 && 'a' <= prefix [0] && prefix [0] <= 'z' ?
657 (byte) (prefix [0] - 'a' + BF.PrefixNElemStringStart) :
658 prefix.Length == 0 ? BF.ElemString :
661 if (BF.PrefixNElemStringStart <= op && op <= BF.PrefixNElemStringEnd) {
663 writer.Write (localName);
666 if (prefix.Length > 0)
667 writer.Write (prefix);
668 writer.Write (localName);
671 OpenElement (prefix, ns);
674 void OpenElement (string prefix, object nsobj)
676 string ns = nsobj.ToString ();
678 state = WriteState.Element;
679 open_start_element = true;
680 element_prefix = prefix;
682 element_ns = nsobj.ToString ();
684 if (element_ns != String.Empty && LookupPrefix (element_ns) != prefix)
685 AddNamespace (prefix, nsobj);
688 void AddNamespace (string prefix, object nsobj)
690 namespaces.Add (new KeyValuePair<string,object> (prefix, nsobj));
693 void CheckIfTextAllowed ()
696 case WriteState.Start:
697 case WriteState.Prolog:
698 throw new InvalidOperationException ("Token content in state Prolog would result in an invalid XML document.");
702 public override void WriteString (string text)
705 throw new ArgumentNullException ("text");
706 CheckIfTextAllowed ();
711 ProcessStateForContent ();
713 if (state == WriteState.Attribute)
716 WriteTextBinary (text);
719 public override void WriteString (XmlDictionaryString text)
722 throw new ArgumentNullException ("text");
723 CheckIfTextAllowed ();
726 text = XmlDictionaryString.Empty;
728 ProcessStateForContent ();
730 if (state == WriteState.Attribute)
731 attr_value += text.Value;
732 else if (text.Equals (XmlDictionary.Empty))
733 writer.Write (BF.EmptyText);
735 writer.Write (BF.TextIndex);
736 WriteDictionaryIndex (text);
740 public override void WriteSurrogateCharEntity (char lowChar, char highChar)
742 WriteChars (new char [] {highChar, lowChar}, 0, 2);
745 public override void WriteWhitespace (string ws)
747 for (int i = 0; i < ws.Length; i++) {
749 case ' ': case '\t': case '\r': case '\n':
752 throw new ArgumentException ("Invalid Whitespace");
756 ProcessStateForContent ();
758 WriteTextBinary (ws);
761 public override void WriteXmlnsAttribute (string prefix, string namespaceUri)
763 if (namespaceUri == null)
764 throw new ArgumentNullException ("namespaceUri");
766 if (String.IsNullOrEmpty (prefix))
767 prefix = CreateNewPrefix ();
769 CheckStateForAttribute ();
771 AddNamespaceChecked (prefix, namespaceUri);
773 state = WriteState.Element;
776 void AddNamespaceChecked (string prefix, object ns)
778 switch (ns.ToString ()) {
785 throw new InvalidOperationException ();
786 var o = namespaces.FirstOrDefault (i => i.Key == prefix);
787 if (o.Key != null) { // i.e. exists
788 if (o.Value.ToString () != ns.ToString ())
789 throw new ArgumentException (String.Format ("The prefix '{0}' is already mapped to another namespace URI '{1}' in this element scope", prefix ?? "(null)", o.Value ?? "(null)"));
792 AddNamespace (prefix, ns);
795 #region DictionaryString
797 void WriteDictionaryIndex (XmlDictionaryString ds)
799 XmlDictionaryString ds2;
800 bool isSession = false;
802 if (ds.Dictionary != dict_ext) {
804 if (dict_int.TryLookup (ds.Value, out ds2))
806 if (!session.TryLookup (ds, out idx))
807 session.TryAdd (dict_int.Add (ds.Value), out idx);
810 writer.Write ((byte) (0x80 + ((idx % 0x80) << 1) + (isSession ? 1 : 0)));
811 writer.Write ((byte) ((byte) (idx / 0x80) << 1));
814 writer.Write ((byte) (((idx % 0x80) << 1) + (isSession ? 1 : 0)));
817 public override void WriteStartElement (string prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri)
819 PrepareStartElement ();
822 prefix = String.Empty;
824 byte op = prefix.Length == 1 && 'a' <= prefix [0] && prefix [0] <= 'z' ?
825 (byte) (prefix [0] - 'a' + BF.PrefixNElemIndexStart) :
826 prefix.Length == 0 ? BF.ElemIndex :
829 if (BF.PrefixNElemIndexStart <= op && op <= BF.PrefixNElemIndexEnd) {
831 WriteDictionaryIndex (localName);
834 if (prefix.Length > 0)
835 writer.Write (prefix);
836 WriteDictionaryIndex (localName);
839 OpenElement (prefix, namespaceUri);
842 public override void WriteStartAttribute (string prefix, XmlDictionaryString localName, XmlDictionaryString ns)
844 if (localName == null)
845 throw new ArgumentNullException ("localName");
847 prefix = String.Empty;
849 ns = XmlDictionaryString.Empty;
850 if (localName.Value == "xmlns" && prefix.Length == 0) {
852 localName = XmlDictionaryString.Empty;
855 ProcessStartAttributeCommon (ref prefix, localName.Value, ns.Value, localName, ns);
857 if (save_target == SaveTarget.Namespaces)
860 if (prefix.Length == 1 && 'a' <= prefix [0] && prefix [0] <= 'z') {
861 writer.Write ((byte) (prefix [0] - 'a' + BF.PrefixNAttrIndexStart));
862 WriteDictionaryIndex (localName);
864 byte op = ns.Value.Length == 0 ? BF.AttrIndex :BF.AttrIndexPrefix;
867 if (prefix.Length > 0)
868 writer.Write (prefix);
869 WriteDictionaryIndex (localName);
873 public override void WriteXmlnsAttribute (string prefix, XmlDictionaryString namespaceUri)
875 if (namespaceUri == null)
876 throw new ArgumentNullException ("namespaceUri");
878 if (String.IsNullOrEmpty (prefix))
879 prefix = CreateNewPrefix ();
881 CheckStateForAttribute ();
883 AddNamespaceChecked (prefix, namespaceUri);
885 state = WriteState.Element;
890 public override void WriteValue (bool value)
892 ProcessTypedValue ();
893 writer.Write ((byte) (value ? BF.BoolTrue : BF.BoolFalse));
896 public override void WriteValue (int value)
898 WriteValue ((long) value);
901 public override void WriteValue (long value)
903 ProcessTypedValue ();
906 writer.Write (BF.Zero);
908 writer.Write (BF.One);
909 else if (value < 0 || value > uint.MaxValue) {
910 writer.Write (BF.Int64);
911 for (int i = 0; i < 8; i++) {
912 writer.Write ((byte) (value & 0xFF));
915 } else if (value <= byte.MaxValue) {
916 writer.Write (BF.Int8);
917 writer.Write ((byte) value);
918 } else if (value <= short.MaxValue) {
919 writer.Write (BF.Int16);
920 writer.Write ((byte) (value & 0xFF));
921 writer.Write ((byte) (value >> 8));
922 } else if (value <= int.MaxValue) {
923 writer.Write (BF.Int32);
924 for (int i = 0; i < 4; i++) {
925 writer.Write ((byte) (value & 0xFF));
931 public override void WriteValue (float value)
933 ProcessTypedValue ();
934 writer.Write (BF.Single);
935 WriteValueContent (value);
938 void WriteValueContent (float value)
940 writer.Write (value);
943 public override void WriteValue (double value)
945 ProcessTypedValue ();
946 writer.Write (BF.Double);
947 WriteValueContent (value);
950 void WriteValueContent (double value)
952 writer.Write (value);
955 public override void WriteValue (decimal value)
957 ProcessTypedValue ();
958 writer.Write (BF.Decimal);
959 WriteValueContent (value);
962 void WriteValueContent (decimal value)
964 int [] bits = Decimal.GetBits (value);
965 // so, looks like it is saved as its internal form,
966 // not the returned order.
967 // BinaryWriter.Write(Decimal) is useless here.
968 writer.Write (bits [3]);
969 writer.Write (bits [2]);
970 writer.Write (bits [0]);
971 writer.Write (bits [1]);
974 public override void WriteValue (DateTime value)
976 ProcessTypedValue ();
977 writer.Write (BF.DateTime);
978 WriteValueContent (value);
981 void WriteValueContent (DateTime value)
983 writer.Write (value.Ticks);
986 public override void WriteValue (Guid value)
988 ProcessTypedValue ();
989 writer.Write (BF.Guid);
990 WriteValueContent (value);
993 void WriteValueContent (Guid value)
995 byte [] bytes = value.ToByteArray ();
996 writer.Write (bytes, 0, bytes.Length);
999 public override void WriteValue (UniqueId value)
1002 throw new ArgumentNullException ("value");
1005 if (value.TryGetGuid (out guid)) {
1006 // this conditional branching is required for
1007 // attr_typed_value not being true.
1008 ProcessTypedValue ();
1010 writer.Write (BF.UniqueId);
1011 byte [] bytes = guid.ToByteArray ();
1012 writer.Write (bytes, 0, bytes.Length);
1014 WriteValue (value.ToString ());
1018 public override void WriteValue (TimeSpan value)
1020 ProcessTypedValue ();
1022 writer.Write (BF.TimeSpan);
1023 WriteValueContent (value);
1026 void WriteValueContent (TimeSpan value)
1028 WriteBigEndian (value.Ticks, 8);
1032 private void WriteBigEndian (long value, int digits)
1035 for (int i = 0; i < digits; i++) {
1036 v = (v << 8) + (value & 0xFF);
1039 for (int i = 0; i < digits; i++) {
1040 writer.Write ((byte) (v & 0xFF));
1045 private void WriteTextBinary (string text)
1047 if (text.Length == 0)
1048 writer.Write (BF.EmptyText);
1050 char [] arr = text.ToCharArray ();
1051 WriteChars (arr, 0, arr.Length);
1057 #region Write typed array content
1059 // they are packed in WriteValue(), so we cannot reuse
1060 // them for array content.
1062 void WriteValueContent (bool value)
1064 writer.Write (value ? (byte) 1 : (byte) 0);
1067 void WriteValueContent (short value)
1069 writer.Write (value);
1072 void WriteValueContent (int value)
1074 writer.Write (value);
1077 void WriteValueContent (long value)
1079 writer.Write (value);