Runtime.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Reflection;
  5. using System.Runtime.CompilerServices;
  6. using System.Text;
  7. using System.Threading;
  8. namespace SharpCifs.Util.Sharpen
  9. {
  10. public class Runtime
  11. {
  12. private static Runtime _instance;
  13. private List<ShutdownHook> _shutdownHooks = new List<ShutdownHook>();
  14. internal void AddShutdownHook(IRunnable r)
  15. {
  16. ShutdownHook item = new ShutdownHook();
  17. item.Runnable = r;
  18. _shutdownHooks.Add(item);
  19. }
  20. internal int AvailableProcessors()
  21. {
  22. return Environment.ProcessorCount;
  23. }
  24. public static long CurrentTimeMillis()
  25. {
  26. return DateTime.UtcNow.ToMillisecondsSinceEpoch();
  27. }
  28. static Hashtable _properties;
  29. public static Hashtable GetProperties()
  30. {
  31. if (_properties == null)
  32. {
  33. _properties = new Hashtable();
  34. _properties["jgit.fs.debug"] = "false";
  35. _properties["file.encoding"] = "UTF-8";
  36. if (Path.DirectorySeparatorChar != '\\')
  37. _properties["os.name"] = "Unix";
  38. else
  39. _properties["os.name"] = "Windows";
  40. }
  41. return _properties;
  42. }
  43. public static string GetProperty(string key)
  44. {
  45. if (GetProperties().Keys.Contains(key))
  46. {
  47. return ((string)GetProperties()[key]);
  48. }
  49. return null;
  50. }
  51. public static void SetProperty(string key, string value)
  52. {
  53. GetProperties()[key] = value;
  54. }
  55. public static Runtime GetRuntime()
  56. {
  57. if (_instance == null)
  58. {
  59. _instance = new Runtime();
  60. }
  61. return _instance;
  62. }
  63. public static int IdentityHashCode(object ob)
  64. {
  65. return RuntimeHelpers.GetHashCode(ob);
  66. }
  67. internal long MaxMemory()
  68. {
  69. return int.MaxValue;
  70. }
  71. private class ShutdownHook
  72. {
  73. public IRunnable Runnable;
  74. ~ShutdownHook()
  75. {
  76. Runnable.Run();
  77. }
  78. }
  79. public static void DeleteCharAt(StringBuilder sb, int index)
  80. {
  81. sb.Remove(index, 1);
  82. }
  83. public static byte[] GetBytesForString(string str)
  84. {
  85. return Encoding.UTF8.GetBytes(str);
  86. }
  87. public static byte[] GetBytesForString(string str, string encoding)
  88. {
  89. return Encoding.GetEncoding(encoding).GetBytes(str);
  90. }
  91. public static FieldInfo[] GetDeclaredFields(Type t)
  92. {
  93. throw new NotImplementedException("Type.GetFields not found on .NetStandard");
  94. //return t.GetFields (BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
  95. }
  96. public static void NotifyAll(object ob)
  97. {
  98. Monitor.PulseAll(ob);
  99. }
  100. public static void Notify(object obj)
  101. {
  102. Monitor.Pulse(obj);
  103. }
  104. public static void PrintStackTrace(Exception ex)
  105. {
  106. Console.WriteLine(ex);
  107. }
  108. public static void PrintStackTrace(Exception ex, TextWriter tw)
  109. {
  110. tw.WriteLine(ex);
  111. }
  112. public static string Substring(string str, int index)
  113. {
  114. return str.Substring(index);
  115. }
  116. public static string Substring(string str, int index, int endIndex)
  117. {
  118. return str.Substring(index, endIndex - index);
  119. }
  120. public static void Wait(object ob)
  121. {
  122. Monitor.Wait(ob);
  123. }
  124. public static bool Wait(object ob, long milis)
  125. {
  126. return Monitor.Wait(ob, (int)milis);
  127. }
  128. public static Type GetType(string name)
  129. {
  130. throw new NotImplementedException(
  131. "AppDomain.CurrentDomain.GetAssemblies not found on .NetStandard");
  132. //foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies ()) {
  133. // Type t = a.GetType (name);
  134. // if (t != null)
  135. // return t;
  136. //}
  137. //never used
  138. //throw new InvalidOperationException ("Type not found: " + name);
  139. }
  140. public static void SetCharAt(StringBuilder sb, int index, char c)
  141. {
  142. sb[index] = c;
  143. }
  144. public static bool EqualsIgnoreCase(string s1, string s2)
  145. {
  146. return s1.Equals(s2, StringComparison.CurrentCultureIgnoreCase);
  147. }
  148. internal static long NanoTime()
  149. {
  150. return Environment.TickCount * 1000 * 1000;
  151. }
  152. internal static int CompareOrdinal(string s1, string s2)
  153. {
  154. return string.CompareOrdinal(s1, s2);
  155. }
  156. public static string GetStringForBytes(byte[] chars)
  157. {
  158. return Encoding.UTF8.GetString(chars, 0, chars.Length);
  159. }
  160. public static string GetStringForBytes(byte[] chars, string encoding)
  161. {
  162. return GetEncoding(encoding).GetString(chars, 0, chars.Length);
  163. }
  164. public static string GetStringForBytes(byte[] chars, int start, int len)
  165. {
  166. return Encoding.UTF8.GetString(chars, start, len);
  167. }
  168. public static string GetStringForBytes(byte[] chars, int start, int len, string encoding)
  169. {
  170. return GetEncoding(encoding).Decode(chars, start, len);
  171. }
  172. public static Encoding GetEncoding(string name)
  173. {
  174. Encoding e = Encoding.GetEncoding(name.Replace('_', '-'));
  175. if (e is UTF8Encoding)
  176. return new UTF8Encoding(false, true);
  177. return e;
  178. }
  179. }
  180. }