public void Create_WorksWithOwnedMemory() { var memory = new OwnedArray <byte>(new byte[] { 1, 2, 3, 4, 5 }); var readableBuffer = new ReadOnlyBuffer(memory, 2, 3); Assert.Equal(new byte[] { 3, 4, 5 }, readableBuffer.ToArray()); }
/// <summary> /// Decodes the utf8 encoded bytes in the <see cref="ReadOnlyBuffer"/> into a <see cref="string"/> /// </summary> /// <param name="buffer">The buffer to decode</param> public static string GetUtf8Span(this ReadOnlyBuffer buffer) { if (buffer.IsEmpty) { return(null); } // Assign 'textSpan' to something formally stack-referring. // The default classification is "returnable, not referring to stack", we want the opposite in this case. ReadOnlySpan <byte> textSpan = stackalloc byte[0]; if (buffer.IsSingleSegment) { textSpan = buffer.First.Span; } else if (buffer.Length < 128) // REVIEW: What's a good number { Span <byte> destination = stackalloc byte[128]; buffer.CopyTo(destination); // We are able to cast because buffer.Length < 128 textSpan = destination.Slice(0, (int)buffer.Length); } else { // Heap allocated copy to parse into array (should be rare) textSpan = new ReadOnlySpan <byte>(buffer.ToArray()); } return(new Utf8Span(textSpan).ToString()); }
public void Create_WorksWithMemory() { var memory = new Memory <byte>(new byte[] { 1, 2, 3, 4, 5 }); var readableBuffer = new ReadOnlyBuffer <byte>(memory.Slice(2, 3)); Assert.Equal(new byte[] { 3, 4, 5 }, readableBuffer.ToArray()); }
public void Create_WorksWithIEnumerableOfMemory() { var memories = new Memory <byte>[] { new byte[] { 1, 2, 3 }, new byte[] { 4, 5, 6 } }; var readableBuffer = new ReadOnlyBuffer <byte>(memories); Assert.Equal(new byte[] { 1, 2, 3, 4, 5, 6 }, readableBuffer.ToArray()); }
public static ReadOnlySpan <byte> ToSpan(this ReadOnlyBuffer <byte> buffer) { if (buffer.IsSingleSegment) { return(buffer.First.Span); } return(buffer.ToArray()); }
public bool ReadMessages(ReadOnlyBuffer <byte> buffer, IInvocationBinder binder, out IList <HubMessage> messages, out SequencePosition consumed, out SequencePosition examined) { // TODO: Fix this implementation to be incremental consumed = buffer.End; examined = consumed; return(ReadMessages(buffer.ToArray(), binder, out messages)); }
private ReadOnlySpan <byte> ConvertBufferToSpan(ReadOnlyBuffer buffer) { if (buffer.IsSingleSpan) { return(buffer.First.Span); } return(buffer.ToArray()); }
public void SetHeader(ref ReadOnlyBuffer key, ref ReadOnlyBuffer value) { string headerKey = GetHeaderKey(ref key); _headers[headerKey] = new HeaderValue { Raw = value.ToArray() }; }
public static void SequenceEqual <T>(ReadOnlyBuffer <T> buffer, params T[] expected) { T[] bufferArray = buffer.ToArray(); Assert.Equal(buffer.Length, expected.Length); for (int i = 0; i < expected.Length; i++) { T actual = bufferArray[i]; Assert.Same(expected[i], actual); } }
public void Allocate_UploadBuffer_Copy_Range(Device device, int destinationOffset, int bufferOffset, int count) { using UploadBuffer <int> uploadBuffer = device.Get().AllocateUploadBuffer <int>(4096); new Random(42).NextBytes(uploadBuffer.Span.AsBytes()); using ReadOnlyBuffer <int> readOnlyBuffer = device.Get().AllocateReadOnlyBuffer <int>(uploadBuffer.Length); uploadBuffer.CopyTo(readOnlyBuffer, destinationOffset, bufferOffset, count); int[] result = readOnlyBuffer.ToArray(destinationOffset, count); Assert.AreEqual(result.Length, count); Assert.IsTrue(uploadBuffer.Span.Slice(bufferOffset, count).SequenceEqual(result)); }
public void Allocate_UploadBuffer_Copy_Full(Device device) { using UploadBuffer <int> uploadBuffer = device.Get().AllocateUploadBuffer <int>(4096); new Random(42).NextBytes(uploadBuffer.Span.AsBytes()); using ReadOnlyBuffer <int> readOnlyBuffer = device.Get().AllocateReadOnlyBuffer <int>(uploadBuffer.Length); uploadBuffer.CopyTo(readOnlyBuffer); int[] result = readOnlyBuffer.ToArray(); Assert.AreEqual(uploadBuffer.Length, result.Length); Assert.IsTrue(uploadBuffer.Span.SequenceEqual(result)); }
public async Task WriteLargeDataBinary(int length) { var data = new byte[length]; new Random(length).NextBytes(data); PipeWriter output = Pipe.Writer; output.Write(data); await output.FlushAsync(); ReadResult result = await Pipe.Reader.ReadAsync(); ReadOnlyBuffer <byte> input = result.Buffer; Assert.Equal(data, input.ToArray()); Pipe.Reader.AdvanceTo(input.End); }
static void BufferLifetime(ReadOnlyBuffer <byte> buffer) { var array = buffer.ToArray(); using (var pinned = buffer.Retain(pin: true)) { unsafe { var p = (byte *)pinned.PinnedPointer; Assert.True(null != p); for (int i = 0; i < buffer.Length; i++) { Assert.Equal(array[i], p[i]); } } } }
public static bool TryParseMessage(ReadOnlyBuffer <byte> buffer, out NegotiationMessage negotiationMessage, out SequencePosition consumed, out SequencePosition examined) { var separator = buffer.PositionOf(TextMessageFormatter.RecordSeparator); if (separator == null) { // Haven't seen the entire negotiate message so bail consumed = buffer.Start; examined = buffer.End; negotiationMessage = null; return(false); } else { consumed = buffer.GetPosition(separator.Value, 1); examined = consumed; } var memory = buffer.IsSingleSegment ? buffer.First : buffer.ToArray(); return(TryParseMessage(memory.Span, out negotiationMessage)); }
public async Task ReaderShouldNotGetUnflushedBytes() { // Write 10 and flush PipeWriter buffer = _pipe.Writer; buffer.Write(new byte[] { 0, 0, 0, 10 }); await buffer.FlushAsync(); // Write 9 buffer = _pipe.Writer; buffer.Write(new byte[] { 0, 0, 0, 9 }); // Write 8 buffer.Write(new byte[] { 0, 0, 0, 8 }); // Make sure we don't see it yet ReadResult result = await _pipe.Reader.ReadAsync(); ReadOnlyBuffer <byte> reader = result.Buffer; Assert.Equal(4, reader.Length); Assert.Equal(new byte[] { 0, 0, 0, 10 }, reader.ToArray()); // Don't move _pipe.Reader.AdvanceTo(reader.Start); // Now flush await buffer.FlushAsync(); reader = (await _pipe.Reader.ReadAsync()).Buffer; Assert.Equal(12, reader.Length); Assert.Equal(new byte[] { 0, 0, 0, 10 }, reader.Slice(0, 4).ToArray()); Assert.Equal(new byte[] { 0, 0, 0, 9 }, reader.Slice(4, 4).ToArray()); Assert.Equal(new byte[] { 0, 0, 0, 8 }, reader.Slice(8, 4).ToArray()); _pipe.Reader.AdvanceTo(reader.Start, reader.Start); }
static void BufferBasics(ReadOnlyBuffer <byte> buffer) { var span = buffer.Span; Assert.Equal(buffer.Length, span.Length); Assert.True(buffer.IsEmpty || buffer.Length != 0); Assert.True(!buffer.IsEmpty || buffer.Length == 0); var array = buffer.ToArray(); for (int i = 0; i < array.Length; i++) { Assert.Equal(array[i], span[i]); } if (buffer.Length > 0) { var slice = buffer.Slice(1); for (int i = 0; i < slice.Length; i++) { Assert.Equal(slice.Span[i], span[i + 1]); } } }
public void Create_WorksWithArray() { var readableBuffer = new ReadOnlyBuffer <byte>(new byte[] { 1, 2, 3, 4, 5 }, 2, 3); Assert.Equal(readableBuffer.ToArray(), new byte[] { 3, 4, 5 }); }