-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathStringStream.cs
More file actions
50 lines (38 loc) · 1.63 KB
/
StringStream.cs
File metadata and controls
50 lines (38 loc) · 1.63 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
using System;
using System.IO;
namespace ManagedCode.Storage.Core
{
public class StringStream(string str) : Stream
{
private readonly string _string = str ?? throw new ArgumentNullException(nameof(str));
public override bool CanRead => true;
public override bool CanSeek => true;
public override bool CanWrite => false;
public override long Length => _string.Length * 2;
public override long Position { get; set; }
private byte this[int i] => (byte)(_string[i / 2] >> ((i & 1) * 8));
public override long Seek(long offset, SeekOrigin origin)
{
Position = origin switch
{
SeekOrigin.Begin => offset,
SeekOrigin.Current => Position + offset,
SeekOrigin.End => Length + offset,
_ => throw new ArgumentOutOfRangeException(nameof(origin), origin, null)
};
return Position;
}
public override int Read(byte[] buffer, int offset, int count)
{
var length = Math.Min(count, Length - Position);
for (var i = 0; i < length; i++)
buffer[offset + i] = this[(int)Position++];
return (int)length;
}
public override int ReadByte() => Position < Length ? this[(int)Position++] : -1;
public override void Flush() { }
public override void SetLength(long value) => throw new NotSupportedException();
public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();
public override string ToString() => _string;
}
}