public static byte[] Utf8StringToBytes(this IBytesToUtf8StringConverter converter, ReadOnlySpan <byte> text)
        {
            using var hub = new BytesHub();
            converter.TryDecode(text, hub.Writer);

            var result = new byte[hub.Writer.WrittenBytes];

            hub.Reader.GetSequence().CopyTo(result);

            return(result);
        }
Esempio n. 2
0
        public static byte[] StringToBytes(this IBytesToUtf8StringConverter converter, string text)
        {
            using (var hub = new Hub())
            {
                converter.TryDecode(text, hub.Writer);
                hub.Writer.Complete();

                var result = new byte[hub.Writer.BytesWritten];
                hub.Reader.GetSequence().CopyTo(result);
                hub.Reader.Complete();

                return(result);
            }
        }
Esempio n. 3
0
        public static bool TryDecode(this IBytesToUtf8StringConverter converter, string text, IBufferWriter <byte> bufferWriter)
        {
            using (var recyclableMemory = BufferPool.Shared.Rent(_utf8Encoding.Value.GetMaxByteCount(text.Length)))
            {
                var length = _utf8Encoding.Value.GetBytes(text, recyclableMemory.Memory.Span);

                if (!converter.TryDecode(recyclableMemory.Memory.Span.Slice(0, length), bufferWriter))
                {
                    throw new FormatException(nameof(text));
                }
            }

            return(true);
        }