Ejemplo n.º 1
0
        // We use object pooling in the following way, if the pool is empty
        // a new object is created, this is effectively just as expensive as
        // creating a new string builder, if we succeed with our operation
        // we return (possibly a new string builder) to the pool.
        // IT'S IMPORTANT THAT WE RESET THE STRING BUILDER BEFORE PUTTING IT BACK IN THE POOL.
        // If an exception occurs between the getting and putting back we simply
        // allow the object to get lost in the ether. It will eventually get garbage collected.

        public static string Encode(this BaseConverter converter, int v)
        {
            var sb = ConcurrentObjectPool <StringBuilder> .Get();

            converter.Encode(v, sb);
            var s = sb.ToString();

            // reset
            sb.Length = 0;
            ConcurrentObjectPool <StringBuilder> .Put(sb);

            return(s);
        }
Ejemplo n.º 2
0
        public static string Encode(this BaseConverter converter, byte[] bytes, int offset, int length)
        {
            var sb = ConcurrentObjectPool <StringBuilder> .Get();

            converter.Encode(bytes, offset, length, sb);
            var s = sb.ToString();

            // reset
            sb.Length = 0;
            ConcurrentObjectPool <StringBuilder> .Put(sb);

            return(s);
        }
Ejemplo n.º 3
0
        public static byte[] DecodeBytes(this BaseConverter converter, string s, int startIndex, int length)
        {
            var buffer = ConcurrentObjectPool <MemoryStream> .Get();

            converter.DecodeBytes(s, startIndex, length, buffer);
            var bytes = new byte[buffer.Length];

            buffer.Position = 0;
            buffer.Read(bytes, 0, bytes.Length);
            // reset
            buffer.SetLength(0);
            ConcurrentObjectPool <MemoryStream> .Put(buffer);

            return(bytes);
        }