forked from microsoft/perfview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSegmentedMemoryStreamWriter.cs
More file actions
128 lines (116 loc) · 4.12 KB
/
SegmentedMemoryStreamWriter.cs
File metadata and controls
128 lines (116 loc) · 4.12 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
namespace FastSerialization
{
public class SegmentedMemoryStreamWriter
{
public SegmentedMemoryStreamWriter(SerializationSettings settings) : this(64, settings) { }
public SegmentedMemoryStreamWriter(long initialSize, SerializationSettings settings)
{
Settings = settings ?? throw new ArgumentNullException(nameof(settings));
if (Settings.StreamLabelWidth == StreamLabelWidth.FourBytes)
{
writeLabel = (value) =>
{
Debug.Assert((long)value <= int.MaxValue, "The StreamLabel overflowed, it should not be treated as a 32bit value.");
Write((int)value);
};
}
else
{
writeLabel = (value) =>
{
Write((long)value);
};
}
bytes = new SegmentedList<byte>(65_536, initialSize);
}
public virtual long Length { get { return bytes.Count; } }
public virtual void Clear() { bytes = new SegmentedList<byte>(131_072); }
public void Write(byte value)
{
bytes.Add(value);
}
public void Write(short value)
{
int intValue = value;
bytes.Add((byte)intValue); intValue = intValue >> 8;
bytes.Add((byte)intValue); intValue = intValue >> 8;
}
public void Write(int value)
{
bytes.Add((byte)value); value = value >> 8;
bytes.Add((byte)value); value = value >> 8;
bytes.Add((byte)value); value = value >> 8;
bytes.Add((byte)value); value = value >> 8;
}
public void Write(long value)
{
bytes.Add((byte)value); value = value >> 8;
bytes.Add((byte)value); value = value >> 8;
bytes.Add((byte)value); value = value >> 8;
bytes.Add((byte)value); value = value >> 8;
bytes.Add((byte)value); value = value >> 8;
bytes.Add((byte)value); value = value >> 8;
bytes.Add((byte)value); value = value >> 8;
bytes.Add((byte)value); value = value >> 8;
}
public void Write(StreamLabel value) => writeLabel(value);
public void Write(string value)
{
if (value == null)
{
Write(-1); // negative charCount means null.
}
else
{
Write(value.Length);
for (int i = 0; i < value.Length; i++)
{
// TODO do actual UTF8
Write((byte)value[i]);
}
}
}
public virtual StreamLabel GetLabel()
{
return (StreamLabel)Length;
}
public void WriteSuffixLabel(StreamLabel value)
{
// This is guaranteed to be uncompressed, but since we are not compressing anything, we can
// simply write the value.
Write(value);
}
public void WriteToStream(Stream outputStream)
{
// TODO really big streams will overflow;
outputStream.Write(bytes.ToArray(), 0, (int)Length);
}
// Note that the returned IMemoryStreamReader is not valid if more writes are done.
public SegmentedMemoryStreamReader GetReader()
{
var readerBytes = bytes;
return new SegmentedMemoryStreamReader(readerBytes, 0, readerBytes.Count, Settings);
}
public void Dispose() { }
/// <summary>
/// Returns the SerializerSettings for this stream writer.
/// </summary>
internal SerializationSettings Settings { get; private set; }
#region private
protected virtual void MakeSpace()
{
// Not necessary
}
public byte[] GetBytes()
{
return bytes.ToArray();
}
protected SegmentedList<byte> bytes;
private Action<StreamLabel> writeLabel;
#endregion
}
}