PageRenderTime 11ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

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