-
Notifications
You must be signed in to change notification settings - Fork 926
Expand file tree
/
Copy pathBufferedStream.cs
More file actions
96 lines (79 loc) · 2.55 KB
/
BufferedStream.cs
File metadata and controls
96 lines (79 loc) · 2.55 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
#if NETSTANDARD1_3
namespace LibGit2Sharp
{
using Core;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/// <summary>
/// A cheap substitute for the .NET BufferedStream class that isn't available on portable profiles.
/// </summary>
internal class BufferedStream : Stream
{
private int bufferSize;
private Stream targetStream;
private MemoryStream bufferStream;
public BufferedStream(Stream targetStream, int bufferSize)
{
Ensure.ArgumentNotNull(targetStream, nameof(targetStream));
this.targetStream = targetStream;
this.bufferSize = bufferSize;
this.bufferStream = new MemoryStream(bufferSize);
}
public override bool CanRead => false; // this implementation only supports writing.
public override bool CanSeek => false;
public override bool CanWrite => this.targetStream.CanWrite;
public override long Length
{
get { throw new NotImplementedException(); }
}
public override long Position
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
public override void Flush()
{
if (this.bufferStream.Length > 0)
{
this.bufferStream.Position = 0;
this.bufferStream.CopyTo(this.targetStream, this.bufferSize);
this.bufferStream.Position = 0;
this.bufferStream.SetLength(0);
}
}
public override int Read(byte[] buffer, int offset, int count)
{
throw new NotImplementedException();
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotSupportedException();
}
public override void SetLength(long value)
{
throw new NotSupportedException();
}
public override void Write(byte[] buffer, int offset, int count)
{
this.bufferStream.Write(buffer, offset, count);
if (this.bufferStream.Length > this.bufferSize)
{
this.Flush();
}
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
this.Flush();
this.targetStream.Dispose();
}
base.Dispose(disposing);
}
}
}
#endif