BinaryStreamWriter.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // Author:
  3. // Jb Evain (jbevain@gmail.com)
  4. //
  5. // Copyright (c) 2008 - 2015 Jb Evain
  6. // Copyright (c) 2008 - 2011 Novell, Inc.
  7. //
  8. // Licensed under the MIT/X11 license.
  9. //
  10. using System;
  11. using System.IO;
  12. #if !READ_ONLY
  13. namespace Mono.Cecil.PE {
  14. class BinaryStreamWriter : BinaryWriter {
  15. public int Position {
  16. get { return (int) BaseStream.Position; }
  17. set { BaseStream.Position = value; }
  18. }
  19. public BinaryStreamWriter (Stream stream)
  20. : base (stream)
  21. {
  22. }
  23. public void WriteByte (byte value)
  24. {
  25. Write (value);
  26. }
  27. public void WriteUInt16 (ushort value)
  28. {
  29. Write (value);
  30. }
  31. public void WriteInt16 (short value)
  32. {
  33. Write (value);
  34. }
  35. public void WriteUInt32 (uint value)
  36. {
  37. Write (value);
  38. }
  39. public void WriteInt32 (int value)
  40. {
  41. Write (value);
  42. }
  43. public void WriteUInt64 (ulong value)
  44. {
  45. Write (value);
  46. }
  47. public void WriteBytes (byte [] bytes)
  48. {
  49. Write (bytes);
  50. }
  51. public void WriteDataDirectory (DataDirectory directory)
  52. {
  53. Write (directory.VirtualAddress);
  54. Write (directory.Size);
  55. }
  56. public void WriteBuffer (ByteBuffer buffer)
  57. {
  58. Write (buffer.buffer, 0, buffer.length);
  59. }
  60. protected void Advance (int bytes)
  61. {
  62. BaseStream.Seek (bytes, SeekOrigin.Current);
  63. }
  64. public void Align (int align)
  65. {
  66. align--;
  67. var position = Position;
  68. var bytes = ((position + align) & ~align) - position;
  69. for (int i = 0; i < bytes; i++)
  70. WriteByte (0);
  71. }
  72. }
  73. }
  74. #endif