PageRenderTime 51ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/source/library/Interlace/Erlang/TermWriter.cs

https://bitbucket.org/VahidN/interlace
C# | 259 lines | 186 code | 46 blank | 27 comment | 18 complexity | 402956b25f06ac55ab0c3aae0b6f7713 MD5 | raw file
  1. #region Using Directives and Copyright Notice
  2. // Copyright (c) 2007-2010, Computer Consultancy Pty Ltd
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. // * Redistributions of source code must retain the above copyright
  8. // notice, this list of conditions and the following disclaimer.
  9. // * Redistributions in binary form must reproduce the above copyright
  10. // notice, this list of conditions and the following disclaimer in the
  11. // documentation and/or other materials provided with the distribution.
  12. // * Neither the name of the Computer Consultancy Pty Ltd nor the
  13. // names of its contributors may be used to endorse or promote products
  14. // derived from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. // ARE DISCLAIMED. IN NO EVENT SHALL COMPUTER CONSULTANCY PTY LTD BE LIABLE
  20. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  26. // DAMAGE.
  27. #endregion
  28. // Portions of this code were originally developed for Bit Plantation BitLibrary.
  29. // (Portions Copyright © 2006 Bit Plantation)
  30. using System;
  31. using System.Collections;
  32. using System.Collections.Generic;
  33. using System.Text;
  34. using System.IO;
  35. using System.Net;
  36. namespace Interlace.Erlang
  37. {
  38. public class TermWriter : IDisposable
  39. {
  40. BinaryWriter _writer;
  41. public TermWriter(Stream stream)
  42. {
  43. _writer = new BinaryWriter(stream);
  44. }
  45. public void Close()
  46. {
  47. _writer.Close();
  48. }
  49. public void Dispose()
  50. {
  51. Close();
  52. }
  53. public static byte[] TermToBinary(object term)
  54. {
  55. MemoryStream stream = new MemoryStream();
  56. using (TermWriter writer = new TermWriter(stream))
  57. {
  58. writer.WriteTerm(term);
  59. return stream.ToArray();
  60. }
  61. }
  62. void WriteByte(byte value)
  63. {
  64. _writer.Write(value);
  65. }
  66. void WriteUnsignedShort(ushort value)
  67. {
  68. _writer.Write((ushort)IPAddress.HostToNetworkOrder((short)value));
  69. }
  70. void WriteUnsignedInteger(uint value)
  71. {
  72. _writer.Write((uint)IPAddress.HostToNetworkOrder((int)value));
  73. }
  74. public void WriteTerm(object term)
  75. {
  76. _writer.Write((byte)Tags.Version);
  77. WriteObject(term);
  78. _writer.Flush();
  79. }
  80. void WriteObject(object obj)
  81. {
  82. if (obj is ITermWritable)
  83. {
  84. ITermWritable writable = obj as ITermWritable;
  85. object substitutedObject = writable.SerializeToTerms();
  86. WriteObject(substitutedObject);
  87. return;
  88. }
  89. if (obj is Atom)
  90. {
  91. WriteAtom(obj as Atom);
  92. return;
  93. }
  94. if (obj is Tuple)
  95. {
  96. WriteTuple(obj as Tuple);
  97. return;
  98. }
  99. if (obj is int || obj is short)
  100. {
  101. WriteIntegerTerm((int)Convert.ToInt32(obj));
  102. return;
  103. }
  104. if (obj is uint || obj is ushort || obj is byte)
  105. {
  106. WriteIntegerTerm((int)Convert.ToUInt32(obj));
  107. return;
  108. }
  109. if (obj is string)
  110. {
  111. WriteString(obj as string);
  112. return;
  113. }
  114. if (obj is bool)
  115. {
  116. WriteObject((bool)obj ? Atom.From("true") : Atom.From("false"));
  117. return;
  118. }
  119. if (obj is byte[])
  120. {
  121. WriteBinary(obj as byte[]);
  122. return;
  123. }
  124. if (obj is ICollection)
  125. {
  126. WriteList(obj as ICollection);
  127. return;
  128. }
  129. if (obj is ErlangProcessId)
  130. {
  131. WriteProcessId(obj as ErlangProcessId);
  132. return;
  133. }
  134. throw new ErlangProtocolException(string.Format(
  135. "An object of an unsupported type ({0}) was serialized.", obj.GetType().FullName));
  136. }
  137. void WriteIntegerTerm(int integer)
  138. {
  139. if (0 <= integer && integer <= 255)
  140. {
  141. WriteByte(Tags.SmallInteger);
  142. WriteByte((byte)integer);
  143. }
  144. else
  145. {
  146. WriteByte(Tags.Integer);
  147. WriteUnsignedInteger((uint)integer);
  148. }
  149. }
  150. void WriteTuple(Tuple tuple)
  151. {
  152. if (tuple.Length < 256)
  153. {
  154. WriteByte(Tags.SmallTuple);
  155. WriteByte((byte)tuple.Length);
  156. }
  157. else
  158. {
  159. WriteByte(Tags.LargeTuple);
  160. WriteUnsignedInteger((uint)tuple.Length);
  161. }
  162. for (int i = 0; i < tuple.Length; i++)
  163. {
  164. WriteObject(tuple[i]);
  165. }
  166. }
  167. void WriteBinary(byte[] binary)
  168. {
  169. WriteByte(Tags.Binary);
  170. WriteUnsignedInteger((uint)binary.Length);
  171. _writer.Write(binary);
  172. }
  173. void WriteList(ICollection list)
  174. {
  175. if (list.Count > 0)
  176. {
  177. WriteByte(Tags.List);
  178. WriteUnsignedInteger((uint)list.Count);
  179. foreach (object element in list)
  180. {
  181. WriteObject(element);
  182. }
  183. WriteByte(Tags.Nil);
  184. }
  185. else
  186. {
  187. WriteByte(Tags.Nil);
  188. }
  189. }
  190. void WriteAtom(Atom atom)
  191. {
  192. WriteByte(Tags.Atom);
  193. byte[] symbolBytes = Encoding.ASCII.GetBytes(atom.Value);
  194. WriteUnsignedShort((ushort)symbolBytes.Length);
  195. _writer.Write(symbolBytes);
  196. }
  197. void WriteString(string stringValue)
  198. {
  199. WriteByte(Tags.String);
  200. byte[] stringBytes = Encoding.ASCII.GetBytes(stringValue);
  201. WriteUnsignedShort((ushort)stringBytes.Length);
  202. _writer.Write(stringBytes);
  203. }
  204. void WriteProcessId(ErlangProcessId processId)
  205. {
  206. WriteByte(Tags.ProcessId);
  207. WriteAtom(processId.Node);
  208. WriteUnsignedInteger(processId.Id);
  209. WriteUnsignedInteger(processId.Serial);
  210. WriteByte(processId.Creation);
  211. }
  212. }
  213. }