Code Coverage Statistics for Source File
Newtonsoft.Json\Utilities\Base64Encoder.cs
Symbol Coverage: 72.92% (35 of 48)
Branch Coverage: 62.50% (20 of 32)
Cyclomatic Complexity Avg: 4.75 Max:15
Code Lines: 46
Symbol Coverage Trend
View:
L | V | Source |
---|---|---|
1 |
using System; |
|
2 |
using System.IO; |
|
3 |
||
4 |
namespace Newtonsoft.Json.Utilities |
|
5 |
{ |
|
6 |
internal class Base64Encoder |
|
7 |
{ |
|
8 |
private const int Base64LineSize = 76; |
|
9 |
private const int LineSizeInBytes = 57; |
|
10 |
||
11 |
207 |
private readonly char[] _charsLine = new char[Base64LineSize];
|
12 |
private readonly TextWriter _writer; |
|
13 |
||
14 |
private byte[] _leftOverBytes; |
|
15 |
private int _leftOverBytesCount; |
|
16 |
||
17 |
207 |
public Base64Encoder(TextWriter writer)
|
18 |
{ |
|
19 |
207 |
ValidationUtils.ArgumentNotNull(writer, "writer");
|
20 |
207 |
_writer = writer;
|
21 |
207 |
}
|
22 |
||
23 |
public void Encode(byte[] buffer, int index, int count) |
|
24 |
{ |
|
25 |
210 |
if (buffer == null)
|
26 |
0 |
throw new ArgumentNullException("buffer");
|
27 |
||
28 |
210 |
if (index < 0)
|
29 |
0 |
throw new ArgumentOutOfRangeException("index");
|
30 |
||
31 |
210 |
if (count < 0)
|
32 |
0 |
throw new ArgumentOutOfRangeException("count");
|
33 |
||
34 |
210 |
if (count > (buffer.Length - index))
|
35 |
0 |
throw new ArgumentOutOfRangeException("count");
|
36 |
||
37 |
210 |
if (_leftOverBytesCount > 0)
|
38 |
{ |
|
39 |
0 |
int leftOverBytesCount = _leftOverBytesCount;
|
40 |
0 |
while (leftOverBytesCount < 3 && count > 0)
|
41 |
{ |
|
42 |
0 |
_leftOverBytes[leftOverBytesCount++] = buffer[index++];
|
43 |
0 |
count--;
|
44 |
} |
|
45 |
0 |
if (count == 0 && leftOverBytesCount < 3)
|
46 |
{ |
|
47 |
0 |
_leftOverBytesCount = leftOverBytesCount;
|
48 |
0 |
return;
|
49 |
} |
|
50 |
0 |
int num2 = Convert.ToBase64CharArray(_leftOverBytes, 0, 3, _charsLine, 0);
|
51 |
0 |
WriteChars(_charsLine, 0, num2);
|
52 |
} |
|
53 |
210 |
_leftOverBytesCount = count % 3;
|
54 |
210 |
if (_leftOverBytesCount > 0)
|
55 |
{ |
|
56 |
205 |
count -= _leftOverBytesCount;
|
57 |
205 |
if (_leftOverBytes == null)
|
58 |
{ |
|
59 |
204 |
_leftOverBytes = new byte[3];
|
60 |
} |
|
61 |
205 |
for (int i = 0; i < _leftOverBytesCount; i++) |
62 |
{ |
|
63 |
405 |
_leftOverBytes[i] = buffer[(index + count) + i];
|
64 |
} |
|
65 |
} |
|
66 |
210 |
int num4 = index + count;
|
67 |
210 |
int length = LineSizeInBytes;
|
68 |
65219 |
while (index < num4)
|
69 |
{ |
|
70 |
65009 |
if ((index + length) > num4)
|
71 |
{ |
|
72 |
209 |
length = num4 - index;
|
73 |
} |
|
74 |
65009 |
int num6 = Convert.ToBase64CharArray(buffer, index, length, _charsLine, 0);
|
75 |
65009 |
WriteChars(_charsLine, 0, num6);
|
76 |
65009 |
index += length;
|
77 |
} |
|
78 |
210 |
}
|
79 |
||
80 |
public void Flush() |
|
81 |
{ |
|
82 |
210 |
if (_leftOverBytesCount > 0)
|
83 |
{ |
|
84 |
205 |
int count = Convert.ToBase64CharArray(_leftOverBytes, 0, _leftOverBytesCount, _charsLine, 0);
|
85 |
205 |
WriteChars(_charsLine, 0, count);
|
86 |
205 |
_leftOverBytesCount = 0;
|
87 |
} |
|
88 |
210 |
}
|
89 |
||
90 |
private void WriteChars(char[] chars, int index, int count) |
|
91 |
{ |
|
92 |
65214 |
_writer.Write(chars, index, count);
|
93 |
65214 |
}
|
94 |
} |
|
95 |
} |