Runtime.cs 4.7 KB

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