~ubuntu-branches/ubuntu/trusty/smuxi/trusty-proposed

« back to all changes in this revision

Viewing changes to lib/Newtonsoft.Json/Src/Newtonsoft.Json/Utilities/Base64Encoder.cs

  • Committer: Package Import Robot
  • Author(s): Mirco Bauer
  • Date: 2013-05-25 22:11:31 UTC
  • mfrom: (1.2.12)
  • Revision ID: package-import@ubuntu.com-20130525221131-nd2mc0kzubuwyx20
Tags: 0.8.11-1
* [22d13d5] Imported Upstream version 0.8.11
* [6d2b95a] Refreshed patches
* [89eb66e] Added ServiceStack libraries to smuxi-engine package
* [848ab10] Enable Campfire engine
* [c6dbdc7] Always build db4o for predictable build result
* [13ec489] Exclude OS X specific libraries from dh_clideps

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
    private readonly char[] _charsLine = new char[Base64LineSize];
12
 
    private readonly TextWriter _writer;
13
 
 
14
 
    private byte[] _leftOverBytes;
15
 
    private int _leftOverBytesCount;
16
 
 
17
 
    public Base64Encoder(TextWriter writer)
18
 
    {
19
 
      ValidationUtils.ArgumentNotNull(writer, "writer");
20
 
      _writer = writer;
21
 
    }
22
 
 
23
 
    public void Encode(byte[] buffer, int index, int count)
24
 
    {
25
 
      if (buffer == null)
26
 
        throw new ArgumentNullException("buffer");
27
 
 
28
 
      if (index < 0)
29
 
        throw new ArgumentOutOfRangeException("index");
30
 
 
31
 
      if (count < 0)
32
 
        throw new ArgumentOutOfRangeException("count");
33
 
 
34
 
      if (count > (buffer.Length - index))
35
 
        throw new ArgumentOutOfRangeException("count");
36
 
 
37
 
      if (_leftOverBytesCount > 0)
38
 
      {
39
 
        int leftOverBytesCount = _leftOverBytesCount;
40
 
        while (leftOverBytesCount < 3 && count > 0)
41
 
        {
42
 
          _leftOverBytes[leftOverBytesCount++] = buffer[index++];
43
 
          count--;
44
 
        }
45
 
        if (count == 0 && leftOverBytesCount < 3)
46
 
        {
47
 
          _leftOverBytesCount = leftOverBytesCount;
48
 
          return;
49
 
        }
50
 
        int num2 = Convert.ToBase64CharArray(_leftOverBytes, 0, 3, _charsLine, 0);
51
 
        WriteChars(_charsLine, 0, num2);
52
 
      }
53
 
      _leftOverBytesCount = count % 3;
54
 
      if (_leftOverBytesCount > 0)
55
 
      {
56
 
        count -= _leftOverBytesCount;
57
 
        if (_leftOverBytes == null)
58
 
        {
59
 
          _leftOverBytes = new byte[3];
60
 
        }
61
 
        for (int i = 0; i < _leftOverBytesCount; i++)
62
 
        {
63
 
          _leftOverBytes[i] = buffer[(index + count) + i];
64
 
        }
65
 
      }
66
 
      int num4 = index + count;
67
 
      int length = LineSizeInBytes;
68
 
      while (index < num4)
69
 
      {
70
 
        if ((index + length) > num4)
71
 
        {
72
 
          length = num4 - index;
73
 
        }
74
 
        int num6 = Convert.ToBase64CharArray(buffer, index, length, _charsLine, 0);
75
 
        WriteChars(_charsLine, 0, num6);
76
 
        index += length;
77
 
      }
78
 
    }
79
 
 
80
 
    public void Flush()
81
 
    {
82
 
      if (_leftOverBytesCount > 0)
83
 
      {
84
 
        int count = Convert.ToBase64CharArray(_leftOverBytes, 0, _leftOverBytesCount, _charsLine, 0);
85
 
        WriteChars(_charsLine, 0, count);
86
 
        _leftOverBytesCount = 0;
87
 
      }
88
 
    }
89
 
 
90
 
    private void WriteChars(char[] chars, int index, int count)
91
 
    {
92
 
      _writer.Write(chars, index, count);
93
 
    }
94
 
  }
 
1
#region License
 
2
// Copyright (c) 2007 James Newton-King
 
3
//
 
4
// Permission is hereby granted, free of charge, to any person
 
5
// obtaining a copy of this software and associated documentation
 
6
// files (the "Software"), to deal in the Software without
 
7
// restriction, including without limitation the rights to use,
 
8
// copy, modify, merge, publish, distribute, sublicense, and/or sell
 
9
// copies of the Software, and to permit persons to whom the
 
10
// Software is furnished to do so, subject to the following
 
11
// conditions:
 
12
//
 
13
// The above copyright notice and this permission notice shall be
 
14
// included in all copies or substantial portions of the Software.
 
15
//
 
16
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
17
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 
18
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
19
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 
20
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 
21
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 
22
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 
23
// OTHER DEALINGS IN THE SOFTWARE.
 
24
#endregion
 
25
 
 
26
using System;
 
27
using System.IO;
 
28
 
 
29
namespace Newtonsoft.Json.Utilities
 
30
{
 
31
  internal class Base64Encoder
 
32
  {
 
33
    private const int Base64LineSize = 76;
 
34
    private const int LineSizeInBytes = 57;
 
35
 
 
36
    private readonly char[] _charsLine = new char[Base64LineSize];
 
37
    private readonly TextWriter _writer;
 
38
 
 
39
    private byte[] _leftOverBytes;
 
40
    private int _leftOverBytesCount;
 
41
 
 
42
    public Base64Encoder(TextWriter writer)
 
43
    {
 
44
      ValidationUtils.ArgumentNotNull(writer, "writer");
 
45
      _writer = writer;
 
46
    }
 
47
 
 
48
    public void Encode(byte[] buffer, int index, int count)
 
49
    {
 
50
      if (buffer == null)
 
51
        throw new ArgumentNullException("buffer");
 
52
 
 
53
      if (index < 0)
 
54
        throw new ArgumentOutOfRangeException("index");
 
55
 
 
56
      if (count < 0)
 
57
        throw new ArgumentOutOfRangeException("count");
 
58
 
 
59
      if (count > (buffer.Length - index))
 
60
        throw new ArgumentOutOfRangeException("count");
 
61
 
 
62
      if (_leftOverBytesCount > 0)
 
63
      {
 
64
        int leftOverBytesCount = _leftOverBytesCount;
 
65
        while (leftOverBytesCount < 3 && count > 0)
 
66
        {
 
67
          _leftOverBytes[leftOverBytesCount++] = buffer[index++];
 
68
          count--;
 
69
        }
 
70
        if (count == 0 && leftOverBytesCount < 3)
 
71
        {
 
72
          _leftOverBytesCount = leftOverBytesCount;
 
73
          return;
 
74
        }
 
75
        int num2 = Convert.ToBase64CharArray(_leftOverBytes, 0, 3, _charsLine, 0);
 
76
        WriteChars(_charsLine, 0, num2);
 
77
      }
 
78
      _leftOverBytesCount = count % 3;
 
79
      if (_leftOverBytesCount > 0)
 
80
      {
 
81
        count -= _leftOverBytesCount;
 
82
        if (_leftOverBytes == null)
 
83
        {
 
84
          _leftOverBytes = new byte[3];
 
85
        }
 
86
        for (int i = 0; i < _leftOverBytesCount; i++)
 
87
        {
 
88
          _leftOverBytes[i] = buffer[(index + count) + i];
 
89
        }
 
90
      }
 
91
      int num4 = index + count;
 
92
      int length = LineSizeInBytes;
 
93
      while (index < num4)
 
94
      {
 
95
        if ((index + length) > num4)
 
96
        {
 
97
          length = num4 - index;
 
98
        }
 
99
        int num6 = Convert.ToBase64CharArray(buffer, index, length, _charsLine, 0);
 
100
        WriteChars(_charsLine, 0, num6);
 
101
        index += length;
 
102
      }
 
103
    }
 
104
 
 
105
    public void Flush()
 
106
    {
 
107
      if (_leftOverBytesCount > 0)
 
108
      {
 
109
        int count = Convert.ToBase64CharArray(_leftOverBytes, 0, _leftOverBytesCount, _charsLine, 0);
 
110
        WriteChars(_charsLine, 0, count);
 
111
        _leftOverBytesCount = 0;
 
112
      }
 
113
    }
 
114
 
 
115
    private void WriteChars(char[] chars, int index, int count)
 
116
    {
 
117
      _writer.Write(chars, index, count);
 
118
    }
 
119
  }
95
120
}
 
 
b'\\ No newline at end of file'