ObjectStateFormatter.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. //
  2. // System.Web.UI.ObjectStateFormatter
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. // Gonzalo Paniagua ([email protected])
  7. //
  8. // (C) 2003 Ben Maurer
  9. // (c) Copyright 2004 Novell, Inc. (http://www.novell.com)
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. //#define TRACE
  32. using System.Collections;
  33. using System.ComponentModel;
  34. using System.Globalization;
  35. using System.Drawing;
  36. using System.IO;
  37. using System.Reflection;
  38. using System.Runtime.Serialization.Formatters.Binary;
  39. using System.Runtime.Serialization;
  40. using System.Text;
  41. using System.Web.UI.WebControls;
  42. using System.Web.Util;
  43. using System.Diagnostics;
  44. namespace System.Web.UI {
  45. #if NET_2_0
  46. public
  47. #else
  48. internal
  49. #endif
  50. sealed class ObjectStateFormatter : IFormatter
  51. #if NET_2_0
  52. , IStateFormatter
  53. #endif
  54. {
  55. public object Deserialize (Stream inputStream)
  56. {
  57. if (inputStream == null)
  58. throw new ArgumentNullException ("inputStream");
  59. return DeserializeObject (new BinaryReader (inputStream));
  60. }
  61. public object Deserialize (string inputString)
  62. {
  63. if (inputString == null)
  64. throw new ArgumentNullException ("inputString");
  65. if (inputString == "")
  66. return null;
  67. return Deserialize (new MemoryStream (Convert.FromBase64String (inputString)));
  68. }
  69. public string Serialize (object stateGraph)
  70. {
  71. if (stateGraph == null)
  72. return "";
  73. MemoryStream ms = new MemoryStream ();
  74. Serialize (ms, stateGraph);
  75. #if TRACE
  76. ms.WriteTo (File.OpenWrite (Path.GetTempFileName ()));
  77. #endif
  78. return Convert.ToBase64String (ms.GetBuffer (), 0, (int) ms.Length);
  79. }
  80. public void Serialize (Stream outputStream, object stateGraph)
  81. {
  82. if (outputStream == null)
  83. throw new ArgumentNullException ("outputStream");
  84. if (stateGraph == null)
  85. throw new ArgumentNullException ("stateGraph");
  86. SerializeValue (new BinaryWriter (outputStream), stateGraph);
  87. }
  88. void SerializeValue (BinaryWriter w, object o)
  89. {
  90. ObjectFormatter.WriteObject (w, o, new WriterContext ());
  91. }
  92. object DeserializeObject (BinaryReader r)
  93. {
  94. return ObjectFormatter.ReadObject (r, new ReaderContext ());
  95. }
  96. #region IFormatter
  97. object IFormatter.Deserialize (Stream serializationStream)
  98. {
  99. return Deserialize (serializationStream);
  100. }
  101. void IFormatter.Serialize (Stream serializationStream, object stateGraph)
  102. {
  103. Serialize (serializationStream, stateGraph);
  104. }
  105. SerializationBinder IFormatter.Binder {
  106. get { return null; }
  107. set { }
  108. }
  109. StreamingContext IFormatter.Context {
  110. get { return new StreamingContext (StreamingContextStates.All); }
  111. set { }
  112. }
  113. ISurrogateSelector IFormatter.SurrogateSelector {
  114. get { return null; }
  115. set { }
  116. }
  117. #endregion
  118. #region Object Readers/Writers
  119. class WriterContext {
  120. Hashtable cache;
  121. short nextKey = 0;
  122. public bool RegisterCache (object o, out short key)
  123. {
  124. if (cache == null) {
  125. cache = new Hashtable ();
  126. cache.Add (o, key = nextKey++);
  127. return false;
  128. }
  129. object posKey = cache [o];
  130. if (posKey == null) {
  131. cache.Add (o, key = nextKey++);
  132. return false;
  133. }
  134. key = (short) posKey;
  135. return true;
  136. }
  137. }
  138. class ReaderContext {
  139. ArrayList cache;
  140. public void CacheItem (object o)
  141. {
  142. if (cache == null)
  143. cache = new ArrayList ();
  144. cache.Add (o);
  145. }
  146. public object GetCache (short key)
  147. {
  148. return cache [key];
  149. }
  150. }
  151. abstract class ObjectFormatter {
  152. static readonly Hashtable writeMap = new Hashtable ();
  153. static ObjectFormatter [] readMap = new ObjectFormatter [256];
  154. static BinaryObjectFormatter binaryObjectFormatter;
  155. static TypeFormatter typeFormatter;
  156. static EnumFormatter enumFormatter;
  157. static SingleRankArrayFormatter singleRankArrayFormatter;
  158. static TypeConverterFormatter typeConverterFormatter;
  159. static ObjectFormatter ()
  160. {
  161. new StringFormatter ().Register ();
  162. new Int64Formatter ().Register ();
  163. new Int32Formatter ().Register ();
  164. new Int16Formatter ().Register ();
  165. new ByteFormatter ().Register ();
  166. new BooleanFormatter ().Register ();
  167. new CharFormatter ().Register ();
  168. new DateTimeFormatter ().Register ();
  169. new PairFormatter ().Register ();
  170. new TripletFormatter ().Register ();
  171. new ArrayListFormatter ().Register ();
  172. new HashtableFormatter ().Register ();
  173. new ObjectArrayFormatter ().Register ();
  174. new UnitFormatter ().Register ();
  175. new FontUnitFormatter ().Register ();
  176. new ColorFormatter ().Register ();
  177. enumFormatter = new EnumFormatter ();
  178. enumFormatter.Register ();
  179. typeFormatter = new TypeFormatter ();
  180. typeFormatter.Register ();
  181. singleRankArrayFormatter = new SingleRankArrayFormatter ();
  182. singleRankArrayFormatter.Register ();
  183. typeConverterFormatter = new TypeConverterFormatter ();
  184. typeConverterFormatter.Register ();
  185. binaryObjectFormatter = new BinaryObjectFormatter ();
  186. binaryObjectFormatter.Register ();
  187. }
  188. // 0 == null
  189. static byte nextId = 1;
  190. public ObjectFormatter ()
  191. {
  192. PrimaryId = nextId ++;
  193. if (NumberOfIds == 1)
  194. return;
  195. SecondaryId = nextId ++;
  196. if (NumberOfIds == 2)
  197. return;
  198. TertiaryId = nextId ++;
  199. if (NumberOfIds == 3)
  200. return;
  201. throw new Exception ();
  202. }
  203. protected readonly byte PrimaryId, SecondaryId = 255, TertiaryId = 255;
  204. protected abstract void Write (BinaryWriter w, object o, WriterContext ctx);
  205. protected abstract object Read (byte token, BinaryReader r, ReaderContext ctx);
  206. protected abstract Type Type { get; }
  207. protected virtual int NumberOfIds { get { return 1; } }
  208. public virtual void Register ()
  209. {
  210. writeMap [Type] = this;
  211. readMap [PrimaryId] = this;
  212. if (SecondaryId != 255) {
  213. readMap [SecondaryId] = this;
  214. if (TertiaryId != 255)
  215. readMap [TertiaryId] = this;
  216. }
  217. }
  218. public static void WriteObject (BinaryWriter w, object o, WriterContext ctx)
  219. {
  220. #if TRACE
  221. if (o != null) {
  222. Trace.WriteLine (String.Format ("Writing {0} (type: {1})", o, o.GetType ()));
  223. Trace.Indent ();
  224. } else {
  225. Trace.WriteLine ("Writing null");
  226. }
  227. long pos = w.BaseStream.Position;
  228. #endif
  229. if (o == null) {
  230. w.Write ((byte) 0);
  231. return;
  232. }
  233. Type t = o.GetType ();
  234. ObjectFormatter fmt = writeMap [t] as ObjectFormatter;
  235. if (fmt == null) {
  236. // Handle abstract types here
  237. if (o is Type)
  238. fmt = typeFormatter;
  239. else if (t.IsEnum)
  240. fmt = enumFormatter;
  241. else if (t.IsArray && ((Array) o).Rank == 1)
  242. fmt = singleRankArrayFormatter;
  243. else {
  244. TypeConverter converter;
  245. converter = TypeDescriptor.GetConverter (o);
  246. if (converter == null ||
  247. !converter.CanConvertTo (typeof (string)) ||
  248. !converter.CanConvertFrom (typeof (string))) {
  249. fmt = binaryObjectFormatter;
  250. } else {
  251. typeConverterFormatter.Converter = converter;
  252. fmt = typeConverterFormatter;
  253. }
  254. }
  255. }
  256. fmt.Write (w, o, ctx);
  257. #if TRACE
  258. Trace.Unindent ();
  259. Trace.WriteLine (String.Format ("Wrote {0} (type: {1}) {2} bytes", o, o.GetType (), w.BaseStream.Position - pos));
  260. #endif
  261. }
  262. public static object ReadObject (BinaryReader r, ReaderContext ctx)
  263. {
  264. byte sig = r.ReadByte ();
  265. if (sig == 0)
  266. return null;
  267. return readMap [sig].Read (sig, r, ctx);
  268. }
  269. protected void Write7BitEncodedInt (BinaryWriter w, int value)
  270. {
  271. do {
  272. int high = (value >> 7) & 0x01ffffff;
  273. byte b = (byte)(value & 0x7f);
  274. if (high != 0) {
  275. b = (byte)(b | 0x80);
  276. }
  277. w.Write(b);
  278. value = high;
  279. } while(value != 0);
  280. }
  281. protected int Read7BitEncodedInt (BinaryReader r)
  282. {
  283. int ret = 0;
  284. int shift = 0;
  285. byte b;
  286. do {
  287. b = r.ReadByte();
  288. ret = ret | ((b & 0x7f) << shift);
  289. shift += 7;
  290. } while ((b & 0x80) == 0x80);
  291. return ret;
  292. }
  293. }
  294. #region Primitive Formatters
  295. class StringFormatter : ObjectFormatter {
  296. protected override void Write (BinaryWriter w, object o, WriterContext ctx)
  297. {
  298. short key;
  299. if (ctx.RegisterCache (o, out key)) {
  300. w.Write (SecondaryId);
  301. w.Write (key);
  302. } else {
  303. w.Write (PrimaryId);
  304. w.Write ((string)o);
  305. }
  306. }
  307. protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
  308. {
  309. if (token == PrimaryId) {
  310. string s = r.ReadString ();
  311. ctx.CacheItem (s);
  312. return s;
  313. } else {
  314. return ctx.GetCache (r.ReadInt16 ());
  315. }
  316. }
  317. protected override Type Type {
  318. get { return typeof (string); }
  319. }
  320. protected override int NumberOfIds {
  321. get { return 2; }
  322. }
  323. }
  324. class Int64Formatter : ObjectFormatter {
  325. protected override void Write (BinaryWriter w, object o, WriterContext ctx)
  326. {
  327. w.Write (PrimaryId);
  328. w.Write ((long)o);
  329. }
  330. protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
  331. {
  332. return r.ReadInt64 ();
  333. }
  334. protected override Type Type {
  335. get { return typeof (long); }
  336. }
  337. }
  338. class Int32Formatter : ObjectFormatter {
  339. protected override void Write (BinaryWriter w, object o, WriterContext ctx)
  340. {
  341. int i = (int) o;
  342. if ((int)(byte) i == i) {
  343. w.Write (SecondaryId);
  344. w.Write ((byte) i);
  345. } else {
  346. w.Write (PrimaryId);
  347. w.Write (i);
  348. }
  349. }
  350. protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
  351. {
  352. if (token == PrimaryId)
  353. return r.ReadInt32 ();
  354. else
  355. return (int) r.ReadByte ();
  356. }
  357. protected override Type Type {
  358. get { return typeof (int); }
  359. }
  360. protected override int NumberOfIds {
  361. get { return 2; }
  362. }
  363. }
  364. class Int16Formatter : ObjectFormatter {
  365. protected override void Write (BinaryWriter w, object o, WriterContext ctx)
  366. {
  367. w.Write (PrimaryId);
  368. w.Write ((short)o);
  369. }
  370. protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
  371. {
  372. return r.ReadInt16 ();
  373. }
  374. protected override Type Type {
  375. get { return typeof (short); }
  376. }
  377. }
  378. class ByteFormatter : ObjectFormatter {
  379. protected override void Write (BinaryWriter w, object o, WriterContext ctx)
  380. {
  381. w.Write (PrimaryId);
  382. w.Write ((byte)o);
  383. }
  384. protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
  385. {
  386. return r.ReadByte ();
  387. }
  388. protected override Type Type {
  389. get { return typeof (byte); }
  390. }
  391. }
  392. class BooleanFormatter : ObjectFormatter {
  393. protected override void Write (BinaryWriter w, object o, WriterContext ctx)
  394. {
  395. if ((bool)o == true)
  396. w.Write (PrimaryId);
  397. else
  398. w.Write (SecondaryId);
  399. }
  400. protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
  401. {
  402. return token == PrimaryId;
  403. }
  404. protected override Type Type {
  405. get { return typeof (bool); }
  406. }
  407. protected override int NumberOfIds {
  408. get { return 2; }
  409. }
  410. }
  411. class CharFormatter : ObjectFormatter {
  412. protected override void Write (BinaryWriter w, object o, WriterContext ctx)
  413. {
  414. w.Write (PrimaryId);
  415. w.Write ((char) o);
  416. }
  417. protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
  418. {
  419. return r.ReadChar ();
  420. }
  421. protected override Type Type {
  422. get { return typeof (char); }
  423. }
  424. }
  425. class DateTimeFormatter : ObjectFormatter {
  426. protected override void Write (BinaryWriter w, object o, WriterContext ctx)
  427. {
  428. w.Write (PrimaryId);
  429. w.Write (((DateTime) o).Ticks);
  430. }
  431. protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
  432. {
  433. return new DateTime (r.ReadInt64 ());
  434. }
  435. protected override Type Type {
  436. get { return typeof (DateTime); }
  437. }
  438. }
  439. class PairFormatter : ObjectFormatter {
  440. protected override void Write (BinaryWriter w, object o, WriterContext ctx)
  441. {
  442. Pair p = (Pair) o;
  443. w.Write (PrimaryId);
  444. WriteObject (w, p.First, ctx);
  445. WriteObject (w, p.Second, ctx);
  446. }
  447. protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
  448. {
  449. Pair p = new Pair ();
  450. p.First = ReadObject (r, ctx);
  451. p.Second = ReadObject (r, ctx);
  452. return p;
  453. }
  454. protected override Type Type {
  455. get { return typeof (Pair); }
  456. }
  457. }
  458. class TripletFormatter : ObjectFormatter {
  459. protected override void Write (BinaryWriter w, object o, WriterContext ctx)
  460. {
  461. Triplet t = (Triplet) o;
  462. w.Write (PrimaryId);
  463. WriteObject (w, t.First, ctx);
  464. WriteObject (w, t.Second, ctx);
  465. WriteObject (w, t.Third, ctx);
  466. }
  467. protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
  468. {
  469. Triplet t = new Triplet ();
  470. t.First = ReadObject (r, ctx);
  471. t.Second = ReadObject (r, ctx);
  472. t.Third = ReadObject (r, ctx);
  473. return t;
  474. }
  475. protected override Type Type {
  476. get { return typeof (Triplet); }
  477. }
  478. }
  479. class ArrayListFormatter : ObjectFormatter {
  480. protected override void Write (BinaryWriter w, object o, WriterContext ctx)
  481. {
  482. ArrayList l = (ArrayList) o;
  483. w.Write (PrimaryId);
  484. Write7BitEncodedInt (w, l.Count);
  485. foreach (object i in l)
  486. WriteObject (w, i, ctx);
  487. }
  488. protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
  489. {
  490. int len = Read7BitEncodedInt (r);
  491. ArrayList l = new ArrayList (len);
  492. for (int i = 0; i < len; i++)
  493. l.Add (ReadObject (r, ctx));
  494. return l;
  495. }
  496. protected override Type Type {
  497. get { return typeof (ArrayList); }
  498. }
  499. }
  500. class HashtableFormatter : ObjectFormatter {
  501. protected override void Write (BinaryWriter w, object o, WriterContext ctx)
  502. {
  503. Hashtable ht = (Hashtable) o;
  504. w.Write (PrimaryId);
  505. Write7BitEncodedInt (w, ht.Count);
  506. foreach (DictionaryEntry de in ht) {
  507. WriteObject (w, de.Key, ctx);
  508. WriteObject (w, de.Value, ctx);
  509. }
  510. }
  511. protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
  512. {
  513. int len = Read7BitEncodedInt (r);
  514. Hashtable ht = new Hashtable (len);
  515. for (int i = 0; i < len; i++) {
  516. object key = ReadObject (r, ctx);
  517. object val = ReadObject (r, ctx);
  518. ht.Add (key, val);
  519. }
  520. return ht;
  521. }
  522. protected override Type Type {
  523. get { return typeof (Hashtable); }
  524. }
  525. }
  526. class ObjectArrayFormatter : ObjectFormatter {
  527. protected override void Write (BinaryWriter w, object o, WriterContext ctx)
  528. {
  529. object [] val = (object []) o;
  530. w.Write (PrimaryId);
  531. Write7BitEncodedInt (w, val.Length);
  532. foreach (object i in val)
  533. WriteObject (w, i, ctx);
  534. }
  535. protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
  536. {
  537. int len = Read7BitEncodedInt (r);
  538. object [] ret = new object [len];
  539. for (int i = 0; i < len; i++)
  540. ret [i] = ReadObject (r, ctx);
  541. return ret;
  542. }
  543. protected override Type Type {
  544. get { return typeof (object []); }
  545. }
  546. }
  547. #endregion
  548. #region System.Web Optimizations
  549. class ColorFormatter : ObjectFormatter {
  550. protected override void Write (BinaryWriter w, object o, WriterContext ctx)
  551. {
  552. Color c = (Color) o;
  553. if (c.IsEmpty || c.IsKnownColor) {
  554. w.Write (SecondaryId);
  555. if (c.IsEmpty)
  556. w.Write (-1); //isempty marker
  557. else
  558. w.Write ((int) c.ToKnownColor ());
  559. }
  560. else {
  561. w.Write (PrimaryId);
  562. w.Write (c.ToArgb ());
  563. }
  564. }
  565. protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
  566. {
  567. int value = r.ReadInt32 ();
  568. if (token == PrimaryId)
  569. return Color.FromArgb (value);
  570. else {
  571. if (value == -1) //isempty marker
  572. return Color.Empty;
  573. return Color.FromKnownColor ((KnownColor)value);
  574. }
  575. }
  576. protected override Type Type {
  577. get { return typeof (Color); }
  578. }
  579. protected override int NumberOfIds {
  580. get { return 2; }
  581. }
  582. }
  583. #endregion
  584. #region Special Formatters
  585. class EnumFormatter : ObjectFormatter {
  586. protected override void Write (BinaryWriter w, object o, WriterContext ctx)
  587. {
  588. object value = Convert.ChangeType (o, ((Enum) o).GetTypeCode ());
  589. w.Write (PrimaryId);
  590. WriteObject (w, o.GetType (), ctx);
  591. WriteObject (w, value, ctx);
  592. }
  593. protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
  594. {
  595. Type t = (Type) ReadObject (r, ctx);
  596. object value = ReadObject (r, ctx);
  597. return Enum.ToObject (t, value);
  598. }
  599. protected override Type Type {
  600. get { return typeof (Enum); }
  601. }
  602. }
  603. class TypeFormatter : ObjectFormatter {
  604. protected override void Write (BinaryWriter w, object o, WriterContext ctx)
  605. {
  606. short key;
  607. if (ctx.RegisterCache (o, out key)) {
  608. w.Write (SecondaryId);
  609. w.Write (key);
  610. } else {
  611. w.Write (PrimaryId);
  612. w.Write (((Type) o).FullName);
  613. // We should cache the name of the assembly
  614. w.Write (((Type) o).Assembly.FullName);
  615. }
  616. }
  617. protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
  618. {
  619. if (token == PrimaryId) {
  620. string type = r.ReadString ();
  621. string assembly = r.ReadString ();
  622. Type t = Assembly.Load (assembly).GetType (type);
  623. ctx.CacheItem (t);
  624. return t;
  625. } else {
  626. return ctx.GetCache (r.ReadInt16 ());
  627. }
  628. }
  629. protected override Type Type {
  630. get { return typeof (Type); }
  631. }
  632. protected override int NumberOfIds {
  633. get { return 2; }
  634. }
  635. }
  636. class SingleRankArrayFormatter : ObjectFormatter {
  637. protected override void Write (BinaryWriter w, object o, WriterContext ctx)
  638. {
  639. Array val = (Array) o;
  640. w.Write (PrimaryId);
  641. WriteObject (w, val.GetType ().GetElementType (), ctx);
  642. Write7BitEncodedInt (w, val.Length);
  643. foreach (object i in val)
  644. WriteObject (w, i, ctx);
  645. }
  646. protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
  647. {
  648. Type t = (Type) ReadObject (r, ctx);
  649. int len = Read7BitEncodedInt (r);
  650. Array val = Array.CreateInstance (t, len);
  651. for (int i = 0; i < len; i++)
  652. val.SetValue (ReadObject (r, ctx), i);
  653. return val;
  654. }
  655. protected override Type Type {
  656. get { return typeof (Array); }
  657. }
  658. }
  659. class FontUnitFormatter : StringFormatter {
  660. protected override void Write (BinaryWriter w, object o, WriterContext ctx)
  661. {
  662. base.Write (w, o.ToString (), ctx);
  663. }
  664. protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
  665. {
  666. return FontUnit.Parse ((string) base.Read (token, r, ctx));
  667. }
  668. protected override Type Type {
  669. get { return typeof (FontUnit); }
  670. }
  671. }
  672. class UnitFormatter : StringFormatter {
  673. protected override void Write (BinaryWriter w, object o, WriterContext ctx)
  674. {
  675. base.Write (w, o.ToString (), ctx);
  676. }
  677. protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
  678. {
  679. return Unit.Parse ((string) base.Read (token, r, ctx));
  680. }
  681. protected override Type Type {
  682. get { return typeof (Unit); }
  683. }
  684. }
  685. class TypeConverterFormatter : StringFormatter {
  686. TypeConverter converter;
  687. protected override void Write (BinaryWriter w, object o, WriterContext ctx)
  688. {
  689. w.Write (PrimaryId);
  690. ObjectFormatter.WriteObject (w, o.GetType (), ctx);
  691. string v = (string) converter.ConvertTo (null, CultureInfo.InvariantCulture,
  692. o, typeof (string));
  693. base.Write (w, v, ctx);
  694. }
  695. protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
  696. {
  697. Type t = (Type) ObjectFormatter.ReadObject (r, ctx);
  698. converter = TypeDescriptor.GetConverter (t);
  699. token = r.ReadByte ();
  700. string v = (string) base.Read (token, r, ctx);
  701. return converter.ConvertFrom (null, CultureInfo.InvariantCulture, v);
  702. }
  703. protected override Type Type {
  704. get { return typeof (TypeConverter); }
  705. }
  706. public TypeConverter Converter {
  707. set { converter = value; }
  708. }
  709. }
  710. class BinaryObjectFormatter : ObjectFormatter {
  711. protected override void Write (BinaryWriter w, object o, WriterContext ctx)
  712. {
  713. w.Write (PrimaryId);
  714. MemoryStream ms = new MemoryStream (128);
  715. new BinaryFormatter ().Serialize (ms, o);
  716. byte [] buf = ms.GetBuffer ();
  717. Write7BitEncodedInt (w, buf.Length);
  718. w.Write (buf, 0, buf.Length);
  719. }
  720. protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
  721. {
  722. int len = Read7BitEncodedInt (r);
  723. byte [] buf = r.ReadBytes (len);
  724. if (buf.Length != len)
  725. throw new Exception ();
  726. return new BinaryFormatter ().Deserialize (new MemoryStream (buf));
  727. }
  728. protected override Type Type {
  729. get { return typeof (object); }
  730. }
  731. }
  732. #endregion
  733. #endregion
  734. }
  735. }