summary refs log tree commit diff
path: root/crypto/src/util/io/BinaryWriters.cs
blob: 6650dcda59692202df50c00b1968a02dff069797 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;
using System.IO;

namespace Org.BouncyCastle.Utilities.IO
{
    public static class BinaryWriters
    {
        public static void WriteInt16BigEndian(BinaryWriter binaryWriter, short n)
        {
            short bigEndian = BitConverter.IsLittleEndian ? Shorts.ReverseBytes(n) : n;
            binaryWriter.Write(bigEndian);
        }

        public static void WriteInt16LittleEndian(BinaryWriter binaryWriter, short n)
        {
            short littleEndian = BitConverter.IsLittleEndian ? n : Shorts.ReverseBytes(n);
            binaryWriter.Write(littleEndian);
        }

        public static void WriteInt32BigEndian(BinaryWriter binaryWriter, int n)
        {
            int bigEndian = BitConverter.IsLittleEndian ? Integers.ReverseBytes(n) : n;
            binaryWriter.Write(bigEndian);
        }

        public static void WriteInt32LittleEndian(BinaryWriter binaryWriter, int n)
        {
            int littleEndian = BitConverter.IsLittleEndian ? n : Integers.ReverseBytes(n);
            binaryWriter.Write(littleEndian);
        }

        public static void WriteInt64BigEndian(BinaryWriter binaryWriter, long n)
        {
            long bigEndian = BitConverter.IsLittleEndian ? Longs.ReverseBytes(n) : n;
            binaryWriter.Write(bigEndian);
        }

        public static void WriteInt64LittleEndian(BinaryWriter binaryWriter, long n)
        {
            long littleEndian = BitConverter.IsLittleEndian ? n : Longs.ReverseBytes(n);
            binaryWriter.Write(littleEndian);
        }

        [CLSCompliant(false)]
        public static void WriteUInt16BigEndian(BinaryWriter binaryWriter, ushort n)
        {
            ushort bigEndian = BitConverter.IsLittleEndian ? Shorts.ReverseBytes(n) : n;
            binaryWriter.Write(bigEndian);
        }

        [CLSCompliant(false)]
        public static void WriteUInt16LittleEndian(BinaryWriter binaryWriter, ushort n)
        {
            ushort littleEndian = BitConverter.IsLittleEndian ? n : Shorts.ReverseBytes(n);
            binaryWriter.Write(littleEndian);
        }

        [CLSCompliant(false)]
        public static void WriteUInt32BigEndian(BinaryWriter binaryWriter, uint n)
        {
            uint bigEndian = BitConverter.IsLittleEndian ? Integers.ReverseBytes(n) : n;
            binaryWriter.Write(bigEndian);
        }

        [CLSCompliant(false)]
        public static void WriteUInt32LittleEndian(BinaryWriter binaryWriter, uint n)
        {
            uint littleEndian = BitConverter.IsLittleEndian ? n : Integers.ReverseBytes(n);
            binaryWriter.Write(littleEndian);
        }

        [CLSCompliant(false)]
        public static void WriteUInt64BigEndian(BinaryWriter binaryWriter, ulong n)
        {
            ulong bigEndian = BitConverter.IsLittleEndian ? Longs.ReverseBytes(n) : n;
            binaryWriter.Write(bigEndian);
        }

        [CLSCompliant(false)]
        public static void WriteUInt64LittleEndian(BinaryWriter binaryWriter, ulong n)
        {
            ulong littleEndian = BitConverter.IsLittleEndian ? n : Longs.ReverseBytes(n);
            binaryWriter.Write(littleEndian);
        }
    }
}