DtWriter.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. Recast4J Copyright (c) 2015 Piotr Piastucki piotr@jtilia.org
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely, subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not
  10. claim that you wrote the original software. If you use this software
  11. in a product, an acknowledgment in the product documentation would be
  12. appreciated but is not required.
  13. 2. Altered source versions must be plainly marked as such, and must not be
  14. misrepresented as being the original software.
  15. 3. This notice may not be removed or altered from any source distribution.
  16. */
  17. using System;
  18. using System.IO;
  19. using DotRecast.Core;
  20. namespace DotRecast.Detour.Io
  21. {
  22. public abstract class DtWriter
  23. {
  24. protected void Write(BinaryWriter stream, float value, RcByteOrder order)
  25. {
  26. byte[] bytes = BitConverter.GetBytes(value);
  27. int i = BitConverter.ToInt32(bytes, 0);
  28. Write(stream, i, order);
  29. }
  30. protected void Write(BinaryWriter stream, short value, RcByteOrder order)
  31. {
  32. if (order == RcByteOrder.BIG_ENDIAN)
  33. {
  34. stream.Write((byte)((value >> 8) & 0xFF));
  35. stream.Write((byte)(value & 0xFF));
  36. }
  37. else
  38. {
  39. stream.Write((byte)(value & 0xFF));
  40. stream.Write((byte)((value >> 8) & 0xFF));
  41. }
  42. }
  43. protected void Write(BinaryWriter stream, long value, RcByteOrder order)
  44. {
  45. if (order == RcByteOrder.BIG_ENDIAN)
  46. {
  47. Write(stream, (int)((ulong)value >> 32), order);
  48. Write(stream, (int)(value & 0xFFFFFFFF), order);
  49. }
  50. else
  51. {
  52. Write(stream, (int)(value & 0xFFFFFFFF), order);
  53. Write(stream, (int)((ulong)value >> 32), order);
  54. }
  55. }
  56. protected void Write(BinaryWriter stream, int value, RcByteOrder order)
  57. {
  58. if (order == RcByteOrder.BIG_ENDIAN)
  59. {
  60. stream.Write((byte)((value >> 24) & 0xFF));
  61. stream.Write((byte)((value >> 16) & 0xFF));
  62. stream.Write((byte)((value >> 8) & 0xFF));
  63. stream.Write((byte)(value & 0xFF));
  64. }
  65. else
  66. {
  67. stream.Write((byte)(value & 0xFF));
  68. stream.Write((byte)((value >> 8) & 0xFF));
  69. stream.Write((byte)((value >> 16) & 0xFF));
  70. stream.Write((byte)((value >> 24) & 0xFF));
  71. }
  72. }
  73. protected void Write(BinaryWriter stream, bool @bool)
  74. {
  75. Write(stream, (byte)(@bool ? 1 : 0));
  76. }
  77. protected void Write(BinaryWriter stream, byte value)
  78. {
  79. stream.Write(value);
  80. }
  81. protected void Write(BinaryWriter stream, MemoryStream data)
  82. {
  83. data.Position = 0;
  84. byte[] buffer = new byte[data.Length];
  85. data.Read(buffer, 0, buffer.Length);
  86. stream.Write(buffer);
  87. }
  88. }
  89. }