示例#1
0
        public void Write(string str)
        {
            byte[] bytes = str.GetASCIIBytes();

            this.Write(bytes, 0, bytes.Length);
            VariableSizedBufferPool.Release(bytes);
        }
        public WriteOnlyBufferedStream(Stream stream, int bufferSize)
        {
            this.stream = stream;

            this.buffer    = VariableSizedBufferPool.Get(bufferSize, true);
            this._position = 0;
        }
示例#3
0
        public static void WriteLine(this Stream fs, string format, params object[] values)
        {
            var buff = string.Format(format, values).GetASCIIBytes();

            fs.Write(buff, 0, buff.Length);
            fs.WriteLine();
            VariableSizedBufferPool.Release(buff);
        }
示例#4
0
        public static void WriteLine(this Stream fs, string line)
        {
            var buff = line.GetASCIIBytes();

            fs.Write(buff, 0, buff.Length);
            fs.WriteLine();
            VariableSizedBufferPool.Release(buff);
        }
示例#5
0
        public static string CalculateMD5Hash(this string input)
        {
            byte[] ascii = input.GetASCIIBytes();
            var    hash  = ascii.CalculateMD5Hash();

            VariableSizedBufferPool.Release(ascii);
            return(hash);
        }
 public void Dispose()
 {
     if (this.Data != null)
     {
         VariableSizedBufferPool.Release(this.Data);
     }
     this.Data = null;
 }
示例#7
0
        public static void WriteString(this BufferPoolMemoryStream ms, string str)
        {
            var byteCount = Encoding.UTF8.GetByteCount(str);

            byte[] buffer = VariableSizedBufferPool.Get(byteCount, true);
            Encoding.UTF8.GetBytes(str, 0, str.Length, buffer, 0);
            ms.Write(buffer, 0, byteCount);
            VariableSizedBufferPool.Release(buffer);
        }
示例#8
0
        protected override void Dispose(bool disposing)
        {
            if (buf != null)
            {
                VariableSizedBufferPool.Release(buf);
            }

            buf = null;
        }
 protected override void Dispose(bool disposing)
 {
     streamClosed = true;
     expandable   = false;
     if (internalBuffer != null)
     {
         VariableSizedBufferPool.Release(internalBuffer);
     }
     internalBuffer = null;
 }
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);

            if (this.buffer != null)
            {
                VariableSizedBufferPool.Release(this.buffer);
            }
            this.buffer = null;
        }
示例#11
0
        /// <summary>
        /// On WP8 platform there are no ASCII encoding.
        /// </summary>
        public static byte[] GetASCIIBytes(this string str)
        {
            byte[] result = VariableSizedBufferPool.Get(str.Length, false);
            for (int i = 0; i < str.Length; ++i)
            {
                char ch = str[i];
                result[i] = (byte)((ch < (char)0x80) ? ch : '?');
            }

            return(result);
        }
        public byte[] ToArray(bool canBeLarger)
        {
            int l = length - initialIndex;

            byte[] outBuffer = l > 0 ? VariableSizedBufferPool.Get(l, canBeLarger) : VariableSizedBufferPool.NoData;

            if (internalBuffer != null)
            {
                Buffer.BlockCopy(internalBuffer, initialIndex, outBuffer, 0, l);
            }
            return(outBuffer);
        }
        /// <summary>
        /// Resize a byte array. It will release the old one to the pool, and the new one is from the pool too.
        /// </summary>
        public static byte[] Resize(ref byte[] buffer, int newSize, bool canBeLarger)
        {
            if (!_isEnabled)
            {
                Array.Resize <byte>(ref buffer, newSize);
                return(buffer);
            }

            byte[] newBuf = VariableSizedBufferPool.Get(newSize, canBeLarger);
            Array.Copy(buffer, 0, newBuf, 0, Math.Min(newBuf.Length, buffer.Length));
            VariableSizedBufferPool.Release(buffer);
            return(buffer = newBuf);
        }
        public BufferPoolMemoryStream(int capacity)
        {
            if (capacity < 0)
            {
                throw new ArgumentOutOfRangeException("capacity");
            }

            canWrite = true;

            internalBuffer = capacity > 0 ? VariableSizedBufferPool.Get(capacity, true) : VariableSizedBufferPool.NoData;
            this.capacity  = internalBuffer.Length;

            expandable     = true;
            allowGetBuffer = true;
        }
示例#15
0
        public static string CalculateMD5Hash(this byte[] input)
        {
#if NETFX_CORE
            var     alg    = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
            IBuffer buff   = CryptographicBuffer.CreateFromByteArray(input);
            var     hashed = alg.HashData(buff);
            var     res    = CryptographicBuffer.EncodeToHexString(hashed);
            return(res);
#else
            using (var md5 = Cryptography.MD5.Create()) {
                var hash = md5.ComputeHash(input);
                var sb   = new StringBuilder(hash.Length);
                for (int i = 0; i < hash.Length; ++i)
                {
                    sb.Append(hash[i].ToString("x2"));
                }
                VariableSizedBufferPool.Release(hash);
                return(sb.ToString());
            }
#endif
        }
示例#16
0
 public ReadOnlyBufferedStream(Stream nstream, int bufferSize)
 {
     stream = nstream;
     buf    = VariableSizedBufferPool.Get(bufferSize, true);
 }
示例#17
0
 public ReadOnlyBufferedStream(Stream nstream)
 {
     stream = nstream;
     buf    = VariableSizedBufferPool.Get(READBUFFER, true);
 }