ReadOnlySequenceFactory.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // https://developers.google.com/protocol-buffers/
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. #endregion
  32. using System;
  33. using System.Buffers;
  34. using System.Collections.Generic;
  35. using System.Linq;
  36. using System.Text;
  37. using System.Threading.Tasks;
  38. namespace Google.Protobuf
  39. {
  40. internal static class ReadOnlySequenceFactory
  41. {
  42. /// <summary>
  43. /// Create a sequence from the specified data. The data will be divided up into segments in the sequence.
  44. /// </summary>
  45. public static ReadOnlySequence<byte> CreateWithContent(byte[] data, int segmentSize = 1, bool addEmptySegmentDelimiters = true)
  46. {
  47. var segments = new List<byte[]>();
  48. if (addEmptySegmentDelimiters)
  49. {
  50. segments.Add(new byte[0]);
  51. }
  52. var currentIndex = 0;
  53. while (currentIndex < data.Length)
  54. {
  55. var segment = new List<byte>();
  56. while (segment.Count < segmentSize && currentIndex < data.Length)
  57. {
  58. segment.Add(data[currentIndex++]);
  59. }
  60. segments.Add(segment.ToArray());
  61. if (addEmptySegmentDelimiters)
  62. {
  63. segments.Add(new byte[0]);
  64. }
  65. }
  66. return CreateSegments(segments.ToArray());
  67. }
  68. /// <summary>
  69. /// Originally from corefx, and has been contributed to Protobuf
  70. /// https://github.com/dotnet/corefx/blob/e99ec129cfd594d53f4390bf97d1d736cff6f860/src/System.Memory/tests/ReadOnlyBuffer/ReadOnlySequenceFactory.byte.cs
  71. /// </summary>
  72. private static ReadOnlySequence<byte> CreateSegments(params byte[][] inputs)
  73. {
  74. if (inputs == null || inputs.Length == 0)
  75. {
  76. throw new InvalidOperationException();
  77. }
  78. int i = 0;
  79. BufferSegment last = null;
  80. BufferSegment first = null;
  81. do
  82. {
  83. byte[] s = inputs[i];
  84. int length = s.Length;
  85. int dataOffset = length;
  86. var chars = new byte[length * 2];
  87. for (int j = 0; j < length; j++)
  88. {
  89. chars[dataOffset + j] = s[j];
  90. }
  91. // Create a segment that has offset relative to the OwnedMemory and OwnedMemory itself has offset relative to array
  92. var memory = new Memory<byte>(chars).Slice(length, length);
  93. if (first == null)
  94. {
  95. first = new BufferSegment(memory);
  96. last = first;
  97. }
  98. else
  99. {
  100. last = last.Append(memory);
  101. }
  102. i++;
  103. } while (i < inputs.Length);
  104. return new ReadOnlySequence<byte>(first, 0, last, last.Memory.Length);
  105. }
  106. private class BufferSegment : ReadOnlySequenceSegment<byte>
  107. {
  108. public BufferSegment(Memory<byte> memory)
  109. {
  110. Memory = memory;
  111. }
  112. public BufferSegment Append(Memory<byte> memory)
  113. {
  114. var segment = new BufferSegment(memory)
  115. {
  116. RunningIndex = RunningIndex + Memory.Length
  117. };
  118. Next = segment;
  119. return segment;
  120. }
  121. }
  122. }
  123. }