8000 Allow ObjectDatabase.CreateBlob() to limit the number of bytes to con… · freevoid/libgit2sharp@2bc52bb · GitHub
[go: up one dir, main page]

Skip to content

Commit 2bc52bb

Browse files
committed
Allow ObjectDatabase.CreateBlob() to limit the number of bytes to consume
1 parent a6714db commit 2bc52bb

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
lines changed

LibGit2Sharp.Tests/ObjectDatabaseFixture.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,35 @@ public void CanCreateABlobFromAStream(string expectedSha, string hintPath)
104104
}
105105
}
106106

107+
[Theory]
108+
[InlineData(16, 32)]
109+
[InlineData(34, 8)]
110+
[InlineData(7584, 5879)]
111+
[InlineData(7854, 1247)]
112+
[InlineData(7854, 9785)]
113+
[InlineData(8192, 4096)]
114+
[InlineData(8192, 4095)]
115+
[InlineData(8192, 4097)]
116+
public void CanCreateABlobFromAStreamWithANumberOfBytesToConsume(int contentSize, int numberOfBytesToConsume)
117+
{
118+
string path = CloneBareTestRepo();
119+
120+
var sb = new StringBuilder();
121+
for (int i = 0; i < contentSize; i++)
122+
{
123+
sb.Append(i % 10);
124+
}
125+
126+
using (var repo = new Repository(path))
127+
{
128+
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(sb.ToString())))
129+
{
130+
Blob blob = repo.ObjectDatabase.CreateBlob(stream, numberOfBytesToConsume: numberOfBytesToConsume);
131+
Assert.Equal(Math.Min(numberOfBytesToConsume, contentSize), blob.Size);
132+
}
133+
}
134+
}
135+
107136
[Fact]
108137
public void CreatingABlobFromANonReadableStreamThrows()
109138
{

LibGit2Sharp/ObjectDatabase.cs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,34 @@ public virtual void AddBackend(OdbBackend backend, int priority)
8484

8585
private class Processor
8686
{
87-
private readonly Stream _stream;
87+
private readonly Stream stream;
88+
private readonly int? numberOfBytesToConsume;
89+
private int totalNumberOfReadBytes;
8890

89-
public Processor(Stream stream)
91+
public Processor(Stream stream, int? numberOfBytesToConsume)
9092
{
91-
_stream = stream;
93+
this.stream = stream;
94+
this.numberOfBytesToConsume = numberOfBytesToConsume;
9295
}
9396

9497
public int Provider(IntPtr content, int max_length, IntPtr data)
9598
{
9699
var local = new byte[max_length];
97-
int numberOfReadBytes = _stream.Read(local, 0, max_length);
100+
101+
int bytesToRead = max_length;
102+
103+
if (numberOfBytesToConsume.HasValue)
104+
{
105+
int totalRemainingBytesToRead = numberOfBytesToConsume.Value - totalNumberOfReadBytes;
106+
107+
if (totalRemainingBytesToRead < max_length)
108+
{
109+
bytesToRead = totalRemainingBytesToRead;
110+
}
111+
}
112+
113+
int numberOfReadBytes = stream.Read(local, 0, bytesToRead);
114+
totalNumberOfReadBytes += numberOfReadBytes;
98115

99116
Marshal.Copy(local, 0, content, numberOfReadBytes);
100117

@@ -121,8 +138,9 @@ public virtual Blob CreateBlob(BinaryReader reader, string hintpath = null)
121138
/// </summary>
122139
/// <param name="stream">The stream from which will be read the content of the blob to be created.</param>
123140
/// <param name="hintpath">The hintpath is used to determine what git filters should be applied to the object before it can be placed to the object database.</param>
141+
/// <param name="numberOfBytesToConsume">The number of bytes to consume from the stream.</param>
124142
/// <returns>The created <see cref="Blob"/>.</returns>
125-
public virtual Blob CreateBlob(Stream stream, string hintpath = null)
143+
public virtual Blob CreateBlob(Stream stream, string hintpath = null, int? numberOfBytesToConsume = null)
126144
{
127145
Ensure.ArgumentNotNull(stream, "stream");
128146

@@ -131,7 +149,7 @@ public virtual Blob CreateBlob(Stream stream, string hintpath = null)
131149
throw new ArgumentException("The stream cannot be read from.", "stream");
132150
}
133151

134-
var proc = new Processor(stream);
152+
var proc = new Processor(stream, numberOfBytesToConsume);
135153
ObjectId id = Proxy.git_blob_create_fromchunks(repo.Handle, hintpath, proc.Provider);
136154

137155
return repo.Lookup<Blob>(id);

0 commit comments

Comments
 (0)
0