Utility.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using System.Linq;
  6. namespace FsDb
  7. {
  8. internal static class Utility
  9. {
  10. public static string CombineLocalPath(this string me, params string[] nexts)
  11. {
  12. if (me == null || nexts.Length == 0)
  13. throw new ArgumentNullException();
  14. string dsc = Path.DirectorySeparatorChar.ToString();
  15. var lst = nexts.ToList();// new List<string>();
  16. lst.Insert(0, me);
  17. return string.Join(
  18. dsc
  19. , lst.ToArray()
  20. )
  21. ;
  22. }
  23. public static IEnumerable<T> DoEach<T>(this IEnumerable<T> source, Action<T> actionToDo)
  24. {
  25. foreach (var item in source)
  26. {
  27. actionToDo(item);
  28. }
  29. return source;
  30. }
  31. public static byte[] ReadToEnd(this System.IO.Stream stream)
  32. {
  33. long originalPosition = 0;
  34. if (stream.CanSeek)
  35. {
  36. originalPosition = stream.Position;
  37. stream.Position = 0;
  38. }
  39. try
  40. {
  41. byte[] readBuffer = new byte[4096];
  42. int totalBytesRead = 0;
  43. int bytesRead;
  44. while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0)
  45. {
  46. totalBytesRead += bytesRead;
  47. if (totalBytesRead == readBuffer.Length)
  48. {
  49. int nextByte = stream.ReadByte();
  50. if (nextByte != -1)
  51. {
  52. byte[] temp = new byte[readBuffer.Length * 2];
  53. Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
  54. Buffer.SetByte(temp, totalBytesRead, (byte)nextByte);
  55. readBuffer = temp;
  56. totalBytesRead++;
  57. }
  58. }
  59. }
  60. byte[] buffer = readBuffer;
  61. if (readBuffer.Length != totalBytesRead)
  62. {
  63. buffer = new byte[totalBytesRead];
  64. Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);
  65. }
  66. return buffer;
  67. }
  68. finally
  69. {
  70. if (stream.CanSeek)
  71. {
  72. stream.Position = originalPosition;
  73. }
  74. }
  75. }
  76. }
  77. internal static class TypeParser<T> where T : struct
  78. {
  79. private delegate bool conv(string value, out T result);
  80. private static readonly Type type;
  81. private static conv del;
  82. static TypeParser()
  83. {
  84. type = typeof(T);
  85. if (type.Name == "Nullable`1")
  86. throw new Exception();
  87. var mi = type.GetMethods().First(p => p.Name == "TryParse" && p.GetParameters().Length == 2);
  88. del = (conv)Delegate.CreateDelegate(typeof(conv), mi);
  89. }
  90. public static T Convert(object value)
  91. {
  92. if (value == null || value == DBNull.Value)
  93. return default(T);
  94. T result = default(T);
  95. del(value.ToString(), out result);
  96. return result;
  97. }
  98. public static T? ConvertNullable(object value)
  99. {
  100. if (value == null || value == DBNull.Value)
  101. return null;
  102. T result;
  103. if (del(value.ToString(), out result))
  104. return result;
  105. return null;
  106. }
  107. }
  108. }