/Mono.Cecil.PE/BinaryStreamWriter.cs

http://github.com/jbevain/cecil · C# · 89 lines · 63 code · 17 blank · 9 comment · 1 complexity · 74a3c79beb0d2822b34d936138702a27 MD5 · raw file

  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. namespace Mono.Cecil.PE {
  13. class BinaryStreamWriter : BinaryWriter {
  14. public int Position {
  15. get { return (int) BaseStream.Position; }
  16. set { BaseStream.Position = value; }
  17. }
  18. public BinaryStreamWriter (Stream stream)
  19. : base (stream)
  20. {
  21. }
  22. public void WriteByte (byte value)
  23. {
  24. Write (value);
  25. }
  26. public void WriteUInt16 (ushort value)
  27. {
  28. Write (value);
  29. }
  30. public void WriteInt16 (short value)
  31. {
  32. Write (value);
  33. }
  34. public void WriteUInt32 (uint value)
  35. {
  36. Write (value);
  37. }
  38. public void WriteInt32 (int value)
  39. {
  40. Write (value);
  41. }
  42. public void WriteUInt64 (ulong value)
  43. {
  44. Write (value);
  45. }
  46. public void WriteBytes (byte [] bytes)
  47. {
  48. Write (bytes);
  49. }
  50. public void WriteDataDirectory (DataDirectory directory)
  51. {
  52. Write (directory.VirtualAddress);
  53. Write (directory.Size);
  54. }
  55. public void WriteBuffer (ByteBuffer buffer)
  56. {
  57. Write (buffer.buffer, 0, buffer.length);
  58. }
  59. protected void Advance (int bytes)
  60. {
  61. BaseStream.Seek (bytes, SeekOrigin.Current);
  62. }
  63. public void Align (int align)
  64. {
  65. align--;
  66. var position = Position;
  67. var bytes = ((position + align) & ~align) - position;
  68. for (int i = 0; i < bytes; i++)
  69. WriteByte (0);
  70. }
  71. }
  72. }