BytesReader.cs
1.88 KB
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
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace UniGLTF
{
public class BytesReader
{
Byte[] m_bytes;
int m_pos;
public BytesReader(Byte[] bytes, int pos=0)
{
m_bytes = bytes;
m_pos = pos;
}
public string ReadString(int count, Encoding encoding)
{
var s = encoding.GetString(m_bytes, m_pos, count);
m_pos += count;
return s;
}
public float ReadSingle()
{
var n = BitConverter.ToSingle(m_bytes, m_pos);
m_pos += 4;
return n;
}
public byte ReadUInt8()
{
return m_bytes[m_pos++];
}
public UInt16 ReadUInt16()
{
var n = BitConverter.ToUInt16(m_bytes, m_pos);
m_pos += 2;
return n;
}
public sbyte ReadInt8()
{
return (sbyte)m_bytes[m_pos++];
}
public Int16 ReadInt16()
{
var n = BitConverter.ToInt16(m_bytes, m_pos);
m_pos += 2;
return n;
}
public int ReadInt32()
{
var n = BitConverter.ToInt32(m_bytes, m_pos);
m_pos += 4;
return n;
}
public void ReadToArray<T>(T[] dst) where T : struct
{
var size = new ArraySegment<Byte>(m_bytes, m_pos, m_bytes.Length - m_pos).MarshalCoyTo(dst);
m_pos += size;
}
public T ReadStruct<T>() where T : struct
{
var size = Marshal.SizeOf(typeof(T));
using (var pin = Pin.Create(new ArraySegment<Byte>(m_bytes, m_pos, m_bytes.Length - m_pos)))
{
var s = (T)Marshal.PtrToStructure(pin.Ptr, typeof(T));
m_pos += size;
return s;
}
}
}
}