Repository navigation
Expand file tree
/
Copy pathByteStream.cs
More file actions
99 lines (83 loc) · 1.79 KB
/
Copy pathByteStream.cs
File metadata and controls
99 lines (83 loc) · 1.79 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace NetcodeIO.NET.Utils.IO
{
/// <summary>
/// A simple stream implementation for reading/writing from/to byte arrays which can be reused
/// </summary>
public class ByteStream : Stream
{
protected byte[] srcByteArray;
public override long Position
{
get; set;
}
public override long Length
{
get
{
return srcByteArray.Length;
}
}
public override bool CanRead
{
get { return true; }
}
public override bool CanWrite
{
get { return true; }
}
public override bool CanSeek
{
get { return true; }
}
/// <summary>
/// Set a new byte array for this stream to read from
/// </summary>
public void SetStreamSource(byte[] sourceBuffer)
{
this.srcByteArray = sourceBuffer;
this.Position = 0;
}
public override int Read(byte[] buffer, int offset, int count)
{
int readBytes = 0;
long pos = this.Position;
long len = this.Length;
for (int i = 0; i < count && pos < len; i++)
{
buffer[i + offset] = srcByteArray[pos++];
readBytes++;
}
this.Position = pos;
return readBytes;
}
public override void Write(byte[] buffer, int offset, int count)
{
long pos = this.Position;
for (int i = 0; i < count; i++)
srcByteArray[pos++] = buffer[i + offset];
this.Position = pos;
}
public override void Flush()
{
}
public override long Seek(long offset, SeekOrigin origin)
{
if (origin == SeekOrigin.Begin)
this.Position = offset;
else if (origin == SeekOrigin.Current)
this.Position += offset;
else
this.Position = this.Length - offset - 1;
return this.Position;
}
public override void SetLength(long value)
{
throw new NotImplementedException();
}
}
}