Skip to content
22 changes: 20 additions & 2 deletions MailKit/ByteArrayBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,37 @@
using System;
using System.Text;
using System.Buffers;
using System.Runtime.CompilerServices;

namespace MailKit
{
class ByteArrayBuilder : IDisposable
{
readonly int initialCapacity;
byte[] buffer;
int length;

public ByteArrayBuilder (int initialCapacity)
public ByteArrayBuilder (int capacity)
{
buffer = ArrayPool<byte>.Shared.Rent (initialCapacity);
buffer = ArrayPool<byte>.Shared.Rent (capacity);
initialCapacity = capacity;
length = 0;
}

public int Length {
get { return length; }
}

public byte this[int index] {
get { return buffer[index]; }
}

public byte[] GetBuffer ()
{
return buffer;
}

[MethodImpl (MethodImplOptions.AggressiveInlining)]
void EnsureCapacity (int capacity)
{
if (capacity > buffer.Length) {
Expand All @@ -70,6 +83,11 @@ public void Append (byte[] text, int startIndex, int count)

public void Clear ()
{
if (buffer.Length > initialCapacity * 4) {
ArrayPool<byte>.Shared.Return (buffer);
buffer = ArrayPool<byte>.Shared.Rent (initialCapacity);
}

length = 0;
}

Expand Down
2 changes: 2 additions & 0 deletions MailKit/MailKit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@

<ItemGroup>
<Compile Include="Net\Imap\AsyncImapClient.cs" />
<Compile Include="Net\Imap\HashCode.cs" Condition=" $(TargetFramework.StartsWith('net4')) Or '$(TargetFramework)' == 'netstandard2.0' " />
<Compile Include="Net\Imap\IImapClient.cs" />
<Compile Include="Net\Imap\IImapFolder.cs" />
<Compile Include="Net\Imap\ImapAuthenticationSecretDetector.cs" />
Expand Down Expand Up @@ -99,6 +100,7 @@
<Compile Include="Net\Imap\ImapSearchQueryOptimizer.cs" />
<Compile Include="Net\Imap\ImapStream.cs" />
<Compile Include="Net\Imap\ImapToken.cs" />
<Compile Include="Net\Imap\ImapTokenCache.cs" />
<Compile Include="Net\Imap\ImapUtils.cs" />
<Compile Include="Net\Pop3\AsyncPop3Client.cs" />
<Compile Include="Net\Pop3\IPop3Client.cs" />
Expand Down
2 changes: 2 additions & 0 deletions MailKit/MailKitLite.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@

<ItemGroup>
<Compile Include="Net\Imap\AsyncImapClient.cs" />
<Compile Include="Net\Imap\HashCode.cs" Condition=" $(TargetFramework.StartsWith('net4')) Or '$(TargetFramework)' == 'netstandard2.0' " />
<Compile Include="Net\Imap\IImapClient.cs" />
<Compile Include="Net\Imap\IImapFolder.cs" />
<Compile Include="Net\Imap\ImapAuthenticationSecretDetector.cs" />
Expand Down Expand Up @@ -100,6 +101,7 @@
<Compile Include="Net\Imap\ImapSearchQueryOptimizer.cs" />
<Compile Include="Net\Imap\ImapStream.cs" />
<Compile Include="Net\Imap\ImapToken.cs" />
<Compile Include="Net\Imap\ImapTokenCache.cs" />
<Compile Include="Net\Imap\ImapUtils.cs" />
<Compile Include="Net\Pop3\AsyncPop3Client.cs" />
<Compile Include="Net\Pop3\IPop3Client.cs" />
Expand Down
Loading
Loading