public void ToBase64_Stackalloc() { byte[] data = Encoding.UTF8.GetBytes("Hello world"); Span <byte> s = stackalloc byte[data.Length]; data.CopyTo(s); using var manager = new UnmanagedMemoryManager <byte>(s); ByteString bs = ByteString.AttachBytes(manager.Memory); Assert.AreEqual("SGVsbG8gd29ybGQ=", bs.ToBase64()); }
public void ToString_Stackalloc() { byte[] data = Encoding.UTF8.GetBytes("Hello world"); Span <byte> s = stackalloc byte[data.Length]; data.CopyTo(s); using (UnmanagedMemoryManager <byte> manager = new UnmanagedMemoryManager <byte>(s)) { ByteString bs = ByteString.AttachBytes(manager.Memory); Assert.AreEqual("Hello world", bs.ToString(Encoding.UTF8)); } }
internal static async Task <ByteString> FromStreamAsyncCore(Stream stream, CancellationToken cancellationToken) { int capacity = stream.CanSeek ? checked ((int)(stream.Length - stream.Position)) : 0; var memoryStream = new MemoryStream(capacity); // We have to specify the buffer size here, as there's no overload accepting the cancellation token // alone. But it's documented to use 81920 by default if not specified. await stream.CopyToAsync(memoryStream, 81920, cancellationToken); #if NETSTANDARD1_1 byte[] bytes = memoryStream.ToArray(); #else // Avoid an extra copy if we can. byte[] bytes = memoryStream.Length == memoryStream.Capacity ? memoryStream.GetBuffer() : memoryStream.ToArray(); #endif return(ByteString.AttachBytes(bytes)); }
/// <summary> /// Reads a bytes field value from the stream. /// </summary> public ByteString ReadBytes() { int length = ReadLength(); if (length <= bufferSize - bufferPos && length > 0) { // Fast path: We already have the bytes in a contiguous buffer, so // just copy directly from it. ByteString result = ByteString.CopyFrom(buffer, bufferPos, length); bufferPos += length; return(result); } else { // Slow path: Build a byte array and attach it to a new ByteString. return(ByteString.AttachBytes(ReadRawBytes(length))); } }
public void WriteToStream_Stackalloc() { byte[] data = Encoding.UTF8.GetBytes("Hello world"); Span <byte> s = stackalloc byte[data.Length]; data.CopyTo(s); MemoryStream ms = new MemoryStream(); using (UnmanagedMemoryManager <byte> manager = new UnmanagedMemoryManager <byte>(s)) { ByteString bs = ByteString.AttachBytes(manager.Memory); bs.WriteTo(ms); } CollectionAssert.AreEqual(data, ms.ToArray()); }
/// <summary> /// Constructs a new <see cref="ByteString" /> from the given bytes. The bytes are not copied, /// and must not be modified while the <see cref="ByteString" /> is in use. /// This API is experimental and subject to change. /// </summary> public static ByteString UnsafeWrap(ReadOnlyMemory <byte> bytes) { return(ByteString.AttachBytes(bytes)); }
/// <summary> /// Converts the given message into a byte string in protobuf encoding. /// </summary> /// <param name="message">The message to convert.</param> /// <returns>The message data as a byte string.</returns> public static ByteString ToByteString(this IMessage message) { ProtoPreconditions.CheckNotNull(message, "message"); return(ByteString.AttachBytes(message.ToByteArray())); }
public static ByteString ReadBytes(ref ReadOnlySpan <byte> buffer, ref ParserInternalState state) { int length = ParsingPrimitives.ParseLength(ref buffer, ref state); return(ByteString.AttachBytes(ParsingPrimitives.ReadRawBytes(ref buffer, ref state, length))); }
public static ByteString ToByteString(this IMessage message) { Preconditions.CheckNotNull <IMessage>(message, Module.smethod_35 <string>(2616444679u)); return(ByteString.AttachBytes(message.ToByteArray())); }