示例#1
0
        public static string GetValueAndRelease(ReseekableStringBuilder sb)
        {
            var s = sb.ToString();

            Release(sb);
            return(s);
        }
示例#2
0
        public static string SubstringCached(this ReseekableStringBuilder str, int startIndex, int length)
        {
            var reader = reseekablestringBuilderReader ?? (reseekablestringBuilderReader = new ReseekableStringBuilderReader());

            reader.StringBuilder = str;
            string result = reader.SubstringCached(startIndex, length);

            reader.StringBuilder = null;
            return(result);
        }
示例#3
0
        public static void Release(ReseekableStringBuilder sb)
        {
            var u = _unusedReseekableStringBuilders;

            if (u != null && u.Count < 4 && ValueString.ShouldKeepReleasedStringBuilder(sb._sb))
            {
                sb._start     = 0;
                sb._sb.Length = 0;
                u.Add(sb);
            }
        }
示例#4
0
        internal static string GetFileSystemName(LazyUri url, string cacheFolder, string extension, bool createLng, bool partition = true)
        {
            var hashed  = false;
            var isazure = cacheFolder != null && cacheFolder.StartsWith("azure:");

            if (cacheFolder == null)
            {
                return(null);
            }
            if (!isazure && cacheFolder.Length > CacheFolderMaxLength)
            {
                throw new ArgumentException("The path of the file cache folder must not be longer than " + CacheFolderMaxLength + " characters.");
            }

            var str      = url.AbsoluteUri;
            var hashcode = Math.Abs((long)str.GetHashCode());
            var sb       = ReseekableStringBuilder.AcquirePooledStringBuilder();

            if (url.Scheme != "http" && url.Scheme != "https")
            {
                throw new NotSupportedException("URI scheme is not supported.");
            }
            var https = url.Scheme == "https";

            sb.Append((string)url.DnsSafeHost);
            if (!url.IsDefaultPort)
            {
                sb.Append("∴");
                sb.AppendFast((int)url.Port);
            }

            sb.Append(https ? "₰" : "ℓ");
            var abspath = url.AbsolutePath;

            sb.Append(abspath, 1, abspath.Length - 1);
            sb.Append((string)url.Query);
            sb.Append((string)url.Fragment);
            if (sb.Length <= CacheFileNameMaxLength)
            {
                FixupFabulousUrl(sb);
                foreach (var item in Path.GetInvalidFileNameChars())
                {
                    if (sb.IndexOf(item) != -1)
                    {
                        sb.Replace(item.ToString(), "℅" + ((int)item).ToString("X2"));
                    }
                }

                sb.Append(extension);
            }

            var folder = isazure ? null : partition?Path.Combine(cacheFolder, (hashcode % 1000).ToString("000")) : cacheFolder;

            if (sb.Length > CacheFileNameMaxLength)
            {
#if NET35
                sb.Length = 0;
#else
                sb.Clear();
#endif
                sb.Append(url.DnsSafeHost.TrimSize(60, 0, false));
                if (!url.IsDefaultPort)
                {
                    sb.Append("∴");
                    sb.AppendFast((int)url.Port);
                }

                sb.Append(https ? "₰" : "ℓ");
                sb.Append("↔");
                using (var hashAlgorithm =
#if NATIVE_HTTP
                           System.Security.Cryptography.SHA256.Create()
#else
                           System.Security.Cryptography.Reimpl.SHA256.Create()
#endif
                       )
                {
                    byte[] inputBytes = System.Text.Encoding.UTF8.GetBytes(str);
                    byte[] hash       = hashAlgorithm.ComputeHash(inputBytes);
                    for (int i = 0; i < hash.Length; i++)
                    {
                        sb.Append(hash[i].ToString("x2"));
                    }
                }

                // IPv6 addresses
                FixupFabulousUrl(sb);
                if (!isazure)
                {
                    sb.Append(extension);
                }
                hashed = true;
            }

            if (isazure)
            {
                sb.Length -= 4; // remove .dat
                sb.Replace("₰", "/s/");
                sb.Replace("ℓ", "/h/");
                sb.Replace("↑", "");
                sb.Replace('\u222F', '/'); // ∯
                return(ReseekableStringBuilder.GetValueAndRelease(sb));
            }

            var path = Path.Combine(folder, ReseekableStringBuilder.GetValueAndRelease(sb));
            if (createLng)
            {
                Directory.CreateDirectory(folder);
                if (hashed)
                {
                    var p = Path.ChangeExtension(path, ".lng");
                    if (!BlobStore.Exists(p))
                    {
                        BlobStore.WriteAllText(p, (string)url.AbsoluteUri, Encoding.UTF8);
                    }
                }
            }

            return(path);
        }
示例#5
0
 public static string ToStringCached(this ReseekableStringBuilder str)
 {
     return(str.SubstringCached(0, str.Length));
 }
示例#6
0
 public static string SubstringCached(this ReseekableStringBuilder str, int startIndex)
 {
     return(str.SubstringCached(startIndex, str.Length - startIndex));
 }