예제 #1
0
        /// <summary>
        /// Creates a buffer containing the error message from the given exception. If the cause is
        /// <c>null</c> returns an empty buffer.
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="cause"></param>
        /// <returns></returns>
        public static IByteBuffer ToByteBuf(IChannelHandlerContext ctx, Exception cause)
        {
            if (cause is null || cause.Message is null)
            {
                return(Unpooled.Empty);
            }

            return(ByteBufferUtil.WriteUtf8(ctx.Allocator, cause.Message));
        }
예제 #2
0
        static void WriteString(IByteBufferAllocator allocator, RedisMessageType type, string content, List <object> output)
        {
            IByteBuffer buf = allocator.Buffer(type.Length + ByteBufferUtil.Utf8MaxBytes(content) +
                                               RedisConstants.EndOfLineLength);

            type.WriteTo(buf);
            ByteBufferUtil.WriteUtf8(buf, content);
            buf.WriteShort(RedisConstants.EndOfLineShort);
            output.Add(buf);
        }
예제 #3
0
        private static void CheckUtf8Bytes(string charSequence)
        {
            var buf = Unpooled.Buffer(ByteBufferUtil.Utf8MaxBytes(charSequence));

            try
            {
                int writtenBytes = ByteBufferUtil.WriteUtf8(buf, charSequence);
                int utf8Bytes    = ByteBufferUtil.Utf8Bytes(charSequence);
                Assert.Equal(writtenBytes, utf8Bytes);
            }
            finally
            {
                buf.Release();
            }
        }
예제 #4
0
        public void WriteUtf8Subsequence()
        {
            String      usAscii = "Some UTF-8 like äÄ∏ŒŒ";
            IByteBuffer buf     = Unpooled.Buffer(16);

            buf.WriteBytes(TextEncodings.UTF8NoBOM.GetBytes(usAscii.Substring(5, 18 - 5)));
            IByteBuffer buf2 = Unpooled.Buffer(16);

            ByteBufferUtil.WriteUtf8(buf2, usAscii.AsSpan(5, 18 - 5));

            Assert.Equal(buf, buf2);

            buf.Release();
            buf2.Release();
        }
예제 #5
0
        public void WriteUtf8()
        {
            string usAscii = "Some UTF-8 like äÄ∏ŒŒ";
            var    buf     = Unpooled.Buffer(16);

            buf.WriteBytes(TextEncodings.UTF8NoBOM.GetBytes(usAscii));
            var buf2 = Unpooled.Buffer(16);

            ByteBufferUtil.WriteUtf8(buf2, usAscii);

            Assert.Equal(buf, buf2);

            buf.Release();
            buf2.Release();
        }
예제 #6
0
        public void WriteUtf8SubsequenceSplitSurrogate()
        {
            string      usAscii = "\uD800\uDC00"; // surrogate pair: one code point, two chars
            IByteBuffer buf     = Unpooled.Buffer(16);

            buf.WriteBytes(Encoding.UTF8.GetBytes(usAscii.Substring(0, 1)));
            IByteBuffer buf2 = Unpooled.Buffer(16);
            var         sb   = new StringBuilder(usAscii);

            ByteBufferUtil.WriteUtf8(buf2, new StringBuilderCharSequence(sb), 0, 1);
            //ByteBufferUtil.WriteUtf8(buf2, new StringCharSequence(usAscii), 0, 1);

            Assert.Equal(buf, buf2);

            buf.Release();
            buf2.Release();
        }
예제 #7
0
        public void WriteUtf8InvalidEndOnTrailingSurrogate()
        {
            string surrogateString = new StringBuilder(2)
                                     .Append('\uDC00')
                                     .ToString();
            var buf = Unpooled.Buffer(16);

            buf.WriteBytes(TextEncodings.UTF8NoBOM.GetBytes(surrogateString));
            var buf2 = Unpooled.Buffer(16);

            ByteBufferUtil.WriteUtf8(buf2, surrogateString);

            Assert.Equal(buf, buf2);
            Assert.Equal(buf.ReadableBytes, ByteBufferUtil.Utf8Bytes(surrogateString));

            buf.Release();
            buf2.Release();
        }
예제 #8
0
        public void WriteUtf8InvalidSubsequences(int start, int end)
        {
            IByteBuffer buf = Unpooled.Buffer(16);

            try
            {
                ByteBufferUtil.WriteUtf8(buf, (AsciiString)"Some UTF-8 like äÄ∏ŒŒ", start, end);
                Assert.False(true, "Did not throw IndexOutOfBoundsException for range (" + start + ", " + end + ")");
            }
            catch (IndexOutOfRangeException)
            {
                // expected
            }
            finally
            {
                Assert.False(buf.IsReadable());
                buf.Release();
            }
        }
예제 #9
0
        public void WriteUtf8CompositeWrapped()
        {
            string utf8 = "Some UTF-8 like äÄ∏ŒŒ";
            var    buf  = Unpooled.Buffer(16);

            buf.WriteBytes(TextEncodings.UTF8NoBOM.GetBytes(utf8));
            var buf2 = new WrappedCompositeByteBuffer(Unpooled.CompositeBuffer().AddComponent(
                                                          Unpooled.Buffer(8)).AddComponent(Unpooled.Buffer(24)));

            // write some byte so we start writing with an offset.
            buf2.WriteByte(1);
            ByteBufferUtil.WriteUtf8(buf2, utf8);

            // Skip the previously written byte.
            Assert.Equal(buf, buf2.SkipBytes(1));

            buf.Release();
            buf2.Release();
        }
예제 #10
0
        static void SendHttpResponse(IChannelHandlerContext ctx, IFullHttpRequest req, IFullHttpResponse res)
        {
            // Generate an error page if response getStatus code is not OK (200).
            HttpResponseStatus responseStatus = res.Status;

            if (responseStatus.Code != 200)
            {
                ByteBufferUtil.WriteUtf8(res.Content, responseStatus.ToString());
                HttpUtil.SetContentLength(res, res.Content.ReadableBytes);
            }

            // Send the response and close the connection if necessary.
            var keepAlive = HttpUtil.IsKeepAlive(req) && responseStatus.Code == 200;

            HttpUtil.SetKeepAlive(res, keepAlive);
            var future = ctx.WriteAndFlushAsync(res);

            if (!keepAlive)
            {
                future.CloseOnComplete(ctx.Channel);
            }
        }
예제 #11
0
 public static IByteBuffer BB(string s)
 {
     return(ByteBufferUtil.WriteUtf8(UnpooledByteBufferAllocator.Default, s));
 }