Memory-Struktur für Speichergrössen

    • VB.NET

    Es gibt 9 Antworten in diesem Thema. Der letzte Beitrag () ist von Higlav.

      Memory-Struktur für Speichergrössen

      Hallo liebe Community,

      Beim Programmieren kommt man viel nicht darum herum, mit Dateien und deren Grössen zu hantieren. Es konnte teilweise sogar mühsam werden, die Dateigrössen richtig darzustellen. Ich setzte mich also hin, um eine Struktur zu schreiben, die einem diese Arbeit abnimmt.
      Hier möchte ich euch meine Lösung präsentieren:

      Memory(Velocity)-Struktur

      Quellcode

      1. Imports System.Text
      2. Imports System.Text.RegularExpressions
      3. Imports System.IO
      4. Imports System.Net
      5. ''' <summary>
      6. ''' Bietet Formatierungsmöglichkeiten für Byte-Grössen.
      7. ''' </summary>
      8. Public Structure Memory
      9. Implements IComparable, ICloneable
      10. #Region "Initializers"
      11. ''' <summary>
      12. ''' Initialisiert die Struktur mit einem Wert, der den angegebenen Details entspricht.
      13. ''' </summary>
      14. ''' <param name="unit">Die Einheit.</param>
      15. ''' <param name="value">Der Wert</param>
      16. ''' <remarks></remarks>
      17. Public Sub New(unit As MemoryUnit, value#)
      18. If unit = MemoryUnit.Bestbinary OrElse unit = MemoryUnit.Bestdecimal Then Throw New ArgumentOutOfRangeException("unit")
      19. VarBytes = CLng(value * unit)
      20. End Sub
      21. ''' <summary>
      22. ''' Initialisiert die Struktur mit einem Byte-Wert.
      23. ''' </summary>
      24. ''' <param name="amount">Der Wert</param>
      25. ''' <remarks></remarks>
      26. Public Sub New(amount&)
      27. VarBytes = amount
      28. End Sub
      29. #End Region
      30. #Region "Variables"
      31. Private VarBytes&
      32. Public Shared ReadOnly Zero As New Memory(0)
      33. Private Shared ReadOnly VarEndings As New List(Of String)({"{0} B{2}", "{0} K{1}B{2}", "{0} M{1}B{2}", "{0} G{1}B{2}", "{0} T{1}B{2}", "{0} P{1}B{2}", "{0} E{1}B{2}"})
      34. #End Region
      35. #Region "Properties"
      36. ''' <summary>
      37. ''' Gibt die gespeicherte Grösse im angegebenen Format aus oder legt sie fest.
      38. ''' </summary>
      39. ''' <param name="unit">Die Einheit.</param>
      40. ''' <value></value>
      41. ''' <returns></returns>
      42. ''' <remarks></remarks>
      43. Default Public Property Value(unit As MemoryUnit) As Double
      44. Get
      45. If unit = MemoryUnit.Bestbinary OrElse unit = MemoryUnit.Bestdecimal Then Return VarBytes
      46. Return VarBytes / unit
      47. End Get
      48. Set(value As Double)
      49. VarBytes = CLng(value * unit)
      50. End Set
      51. End Property
      52. #End Region
      53. #Region "Functions"
      54. ''' <summary>
      55. ''' Bildet die gespeicherte Grösse in einem geeigneten Byte-basierenden Format ab.
      56. ''' </summary>
      57. ''' <returns></returns>
      58. ''' <remarks></remarks>
      59. Public Overrides Function ToString() As String
      60. Return ToString(MemoryUnit.Bestdecimal)
      61. End Function
      62. ''' <summary>
      63. ''' Bildet die gespeicherte Grösse nach bestimmten Parametern ab.
      64. ''' </summary>
      65. ''' <param name="unit">Die Einheit.</param>
      66. ''' <returns></returns>
      67. ''' <remarks></remarks>
      68. Public Overloads Function ToString(unit As MemoryUnit) As String
      69. Dim First = String.Empty
      70. Dim Second = String.Empty
      71. Dim Border = 1000
      72. If unit = MemoryUnit.Bestbinary OrElse unit.ToString.Contains("bib") Then First = "i" : Border = 1024
      73. If unit.ToString.Contains("bit") Then Second = "it"
      74. If Not unit = MemoryUnit.Bestbinary AndAlso Not unit = MemoryUnit.Bestdecimal Then
      75. Dim S = VarEndings.First(Function(x) x.Substring(4, 1) = Regex.Match(unit.ToString, "^[A-Z]").Value)
      76. Return String.Format(S, Value(unit), First, Second)
      77. End If
      78. Dim D# = VarBytes
      79. For i = 0 To [Enum].GetValues(GetType(MemoryUnit)).Length - 1
      80. If D < Border Then
      81. Return String.Format(VarEndings(i), D.ToString("###0.0#"), First, Second)
      82. End If
      83. D /= Border
      84. Next
      85. Return String.Empty
      86. End Function
      87. ''' <summary>
      88. ''' Initialisiert die Struktur mit einer Dateigrösse.
      89. ''' </summary>
      90. ''' <param name="path">Der Pfad, der auf eine Datei zeigt.</param>
      91. ''' <returns></returns>
      92. ''' <remarks></remarks>
      93. Public Shared Function FromFile(path$) As Memory
      94. Return New FileInfo(path).Length
      95. End Function
      96. ''' <summary>
      97. ''' Initialisiert die Struktur mit der Grösse einer Internetressource.
      98. ''' </summary>
      99. ''' <param name="address">Ein Uri-Objekt, das auf eine Internetressource zeigt.</param>
      100. ''' <returns></returns>
      101. ''' <remarks></remarks>
      102. Public Shared Function FromUri(address As Uri) As Memory
      103. Dim Req = HttpWebRequest.CreateHttp(address)
      104. Using Resp = DirectCast(Req.GetResponse, HttpWebResponse)
      105. Return Resp.ContentLength
      106. End Using
      107. End Function
      108. ''' <summary>
      109. ''' Parst den Wert und die Einheit aus einem String.
      110. ''' </summary>
      111. ''' <param name="str">String, der die Daten zur Grösse beinhaltet</param>
      112. ''' <returns></returns>
      113. ''' <remarks></remarks>
      114. Public Shared Function Parse(str$) As Memory
      115. Dim Dic = GetDic()
      116. Dim Pattern = "(^|[^0-9])(?<Value>[0-9]+?((,|\.)[0-9]+?|))[^0-9,\.]*?(?<Unit>(" & String.Join("|", Dic.ToList.ConvertAll(Function(x) x.Key)) & "))"
      117. If Regex.IsMatch(str, Pattern) Then
      118. Dim M = Regex.Match(str, Pattern, RegexOptions.IgnoreCase)
      119. Dim Value = Double.Parse(M.Groups("Value").Value.Replace(","c, "."c))
      120. Dim Unit = Dic(M.Groups("Unit").Value)
      121. Return New Memory(Unit, Value)
      122. Else
      123. Return Nothing
      124. End If
      125. End Function
      126. Private Shared Function GetDic() As Dictionary(Of String, MemoryUnit)
      127. Dim Dic As New Dictionary(Of String, MemoryUnit)
      128. Dic.Add("(b|byte)", MemoryUnit.Byte)
      129. Dic.Add("(kibit|kibibit)", MemoryUnit.Kibibit)
      130. Dic.Add("(kib|kibibyte)", MemoryUnit.Kibibyte)
      131. Dic.Add("(kbit|kilobit)", MemoryUnit.Kilobit)
      132. Dic.Add("(kb|kbyte|kilobyte)", MemoryUnit.Kilobyte)
      133. Dic.Add("(mebit|mebibit)", MemoryUnit.Mebibit)
      134. Dic.Add("(meb|mebibyte)", MemoryUnit.Mebibyte)
      135. Dic.Add("(mbit|megabit)", MemoryUnit.Megabit)
      136. Dic.Add("(mb|megabyte)", MemoryUnit.Megabyte)
      137. Dic.Add("(gibit|gibibit)", MemoryUnit.Gibibit)
      138. Dic.Add("(gib|gibibyte)", MemoryUnit.Gibibyte)
      139. Dic.Add("(gbit|gigabit)", MemoryUnit.Gigabit)
      140. Dic.Add("(gb|gigabyte)", MemoryUnit.Gigabyte)
      141. Dic.Add("(tebbit|tebibit)", MemoryUnit.Tebibit)
      142. Dic.Add("(tebb|tebibyte)", MemoryUnit.Tebibyte)
      143. Dic.Add("(tbit|terabit)", MemoryUnit.Terabit)
      144. Dic.Add("(tb|terabyte)", MemoryUnit.Terabyte)
      145. Dic.Add("(pbibit|pebibit)", MemoryUnit.Pebibit)
      146. Dic.Add("(pbib|pebibyte)", MemoryUnit.Pebibyte)
      147. Dic.Add("(pbit|petabit)", MemoryUnit.Petabit)
      148. Dic.Add("(pb|petabyte)", MemoryUnit.Petabyte)
      149. Dic.Add("(exbibit)", MemoryUnit.Exbibit)
      150. Dic.Add("(exbib|exbibyte)", MemoryUnit.Exbibyte)
      151. Dic.Add("(ebit|exabit)", MemoryUnit.Exabit)
      152. Dic.Add("(eb|exab|exabyte)", MemoryUnit.Exabyte)
      153. Return Dic
      154. End Function
      155. ''' <summary>
      156. ''' Generiert einen leeren Buffer.
      157. ''' </summary>
      158. ''' <returns></returns>
      159. ''' <remarks></remarks>
      160. Public Function CreateBuffer() As Byte()
      161. Dim Buf(CInt(VarBytes)) As Byte
      162. Return Buf
      163. End Function
      164. ''' <summary>
      165. ''' Klont die aktuelle Instanz.
      166. ''' </summary>
      167. ''' <returns></returns>
      168. ''' <remarks></remarks>
      169. Public Function Clone() As Object Implements ICloneable.Clone
      170. Return New Memory(VarBytes)
      171. End Function
      172. ''' <summary>
      173. ''' Vergleicht zwei Memory-Instanzen und gibt so die Sortierreihenfolge an.
      174. ''' </summary>
      175. ''' <param name="obj">Ein zu vergleichendes Memory-Objekt</param>
      176. ''' <returns></returns>
      177. ''' <remarks></remarks>
      178. Public Function CompareTo(obj As Object) As Integer Implements IComparable.CompareTo
      179. Dim M = DirectCast(obj, Memory)
      180. If Value(MemoryUnit.Byte) < M.Value(MemoryUnit.Byte) Then Return 1
      181. If Me = M Then Return 0
      182. Return - 1
      183. End Function
      184. #End Region
      185. #Region "Operators"
      186. Public Shared Operator +(m1 As Memory, m2 As Memory) As Memory
      187. Return New Memory(m1.VarBytes + m2.VarBytes)
      188. End Operator
      189. Public Shared Operator -(m1 As Memory, m2 As Memory) As Memory
      190. Return New Memory(m1.VarBytes - m2.VarBytes)
      191. End Operator
      192. Public Shared Operator =(m1 As Memory, m2 As Memory) As Boolean
      193. Return m1.VarBytes = m2.VarBytes
      194. End Operator
      195. Public Shared Operator <>(m1 As Memory, m2 As Memory) As Boolean
      196. Return m1.VarBytes <> m2.VarBytes
      197. End Operator
      198. Public Shared Operator <(m1 As Memory, m2 As Memory) As Boolean
      199. Return m1.VarBytes < m2.VarBytes
      200. End Operator
      201. Public Shared Operator >(m1 As Memory, m2 As Memory) As Boolean
      202. Return m1.VarBytes > m2.VarBytes
      203. End Operator
      204. Public Shared Operator /(m1 As Memory, m2 As Memory) As Double
      205. Return m1.VarBytes / m2.VarBytes
      206. End Operator
      207. Public Shared Operator \(m1 As Memory, m2 As Memory) As Long
      208. Return m1.VarBytes \ m2.VarBytes
      209. End Operator
      210. Public Shared Operator <=(m1 As Memory, m2 As Memory) As Boolean
      211. Return m1.VarBytes <= m2.VarBytes
      212. End Operator
      213. Public Shared Operator >=(m1 As Memory, m2 As Memory) As Boolean
      214. Return m1.VarBytes >= m2.VarBytes
      215. End Operator
      216. Public Shared Operator Mod(m1 As Memory, m2 As Memory) As Memory
      217. Return New Memory(m1.VarBytes Mod m2.VarBytes)
      218. End Operator
      219. Public Shared Widening Operator CType(m As Memory) As Long
      220. Return m.VarBytes
      221. End Operator
      222. Public Shared Widening Operator CType(d&) As Memory
      223. Return New Memory(d)
      224. End Operator
      225. Public Shared Operator +(m As Memory, d&) As Memory
      226. Return New Memory(m.VarBytes + d)
      227. End Operator
      228. Public Shared Operator -(m As Memory, d&) As Memory
      229. Return New Memory(m.VarBytes - d)
      230. End Operator
      231. Public Shared Operator =(m As Memory, d&) As Boolean
      232. Return m.VarBytes = d
      233. End Operator
      234. Public Shared Operator <>(m As Memory, d&) As Boolean
      235. Return Not m = d
      236. End Operator
      237. Public Shared Operator <(m As Memory, d&) As Boolean
      238. Return m.VarBytes < d
      239. End Operator
      240. Public Shared Operator >(m As Memory, d&) As Boolean
      241. Return m.VarBytes > d
      242. End Operator
      243. Public Shared Operator /(m As Memory, d#) As Memory
      244. Return New Memory(CLng(m.VarBytes / d))
      245. End Operator
      246. Public Shared Operator \(m As Memory, d&) As Memory
      247. Return New Memory(CLng(m.VarBytes \ d))
      248. End Operator
      249. Public Shared Operator *(m As Memory, d#) As Memory
      250. Return New Memory(CLng(m.VarBytes * d))
      251. End Operator
      252. Public Shared Operator <=(m As Memory, d&) As Boolean
      253. Return m.VarBytes <= d
      254. End Operator
      255. Public Shared Operator >=(m As Memory, d&) As Boolean
      256. Return m.VarBytes >= d
      257. End Operator
      258. Public Shared Operator Mod(m As Memory, d#) As Memory
      259. Return New Memory(CLng(m.VarBytes Mod d))
      260. End Operator
      261. Public Shared Operator ^(m As Memory, d#) As Memory
      262. Return New Memory(CLng(m.VarBytes ^ d))
      263. End Operator
      264. Public Shared Operator /(m As Memory, t As TimeSpan) As MemoryVelocity
      265. Return New MemoryVelocity(m, t)
      266. End Operator
      267. Public Shared Operator /(m As Memory, v As MemoryVelocity) As TimeSpan
      268. Return TimeSpan.FromSeconds(m.VarBytes / v.Value(MemoryUnit.Byte, TimeUnit.Second))
      269. End Operator
      270. #End Region
      271. End Structure
      272. ''' <summary>
      273. ''' Gibt eine Auswahl an Einheiten für die Grösse.
      274. ''' </summary>
      275. ''' <remarks>
      276. ''' <see cref="MemoryUnit.Bestbinary">Bestbinary</see> und
      277. ''' <see cref="MemoryUnit.Bestdecimal">Bestdecimal</see> werden nur für die <see cref="Memory.ToString">ToString</see>-Methode verwendet. Wird bei falscher Handhabung als
      278. ''' <see
      279. ''' cref="MemoryUnit.[Byte]">
      280. ''' Byte
      281. ''' </see>
      282. ''' behandelt.
      283. ''' </remarks>
      284. Public Enum MemoryUnit As Long
      285. [Byte] = 1
      286. Kilobyte = 1000
      287. Kilobit = 1000 \ 8
      288. Kibibyte = CLng(2 ^ 10)
      289. Kibibit = CLng(2 ^ 10) \ 8
      290. Megabyte = 1000000
      291. Megabit = 1000000 \ 8
      292. Mebibyte = CLng(2 ^ 20)
      293. Mebibit = CLng(2 ^ 20) \ 8
      294. Gigabyte = 1000000000
      295. Gigabit = 1000000000 \ 8
      296. Gibibyte = CLng(2 ^ 30)
      297. Gibibit = CLng(2 ^ 30) \ 8
      298. Terabyte = 1000000000000
      299. Terabit = 1000000000000 \ 8
      300. Tebibyte = CLng(2 ^ 40)
      301. Tebibit = CLng(2 ^ 40) \ 8
      302. Petabyte = 1000000000000000
      303. Petabit = 1000000000000000 \ 8
      304. Pebibyte = CLng(2 ^ 50)
      305. Pebibit = CLng(2 ^ 50) \ 8
      306. Exabyte = 1000000000000000000
      307. Exabit = 1000000000000000000 \ 8
      308. Exbibyte = CLng(2 ^ 60)
      309. Exbibit = CLng(2 ^ 60) \ 8
      310. ''' <summary>
      311. ''' Sucht bei Anwendung bei der <seealso cref="Memory.ToString">ToString</seealso>-Methode die optisch beste Einheit(1'024er-Schritte).
      312. ''' </summary>
      313. ''' <remarks>Nicht für die Initialisierung zu verwenden.</remarks>
      314. Bestbinary
      315. ''' <summary>
      316. ''' Sucht bei Anwendung bei der <seealso cref="Memory.ToString">ToString</seealso>-Methode die optisch beste Einheit(1'000er-Schritte).
      317. ''' </summary>
      318. ''' <remarks>Nicht für die Initialisierung zu verwenden.</remarks>
      319. Bestdecimal
      320. End Enum
      321. ''' <summary>
      322. ''' Bietet Formatierungsmöglichkeiten für die Geschwindigkeit bei Byte-basierenden Grössen.
      323. ''' </summary>
      324. Public Structure MemoryVelocity
      325. Implements IComparable, ICloneable
      326. #Region "Initializers"
      327. ''' <summary>
      328. ''' Initialisiert die Struktur mit der Anzahl Bytes und eines Zeitspanne.
      329. ''' </summary>
      330. ''' <param name="memory">
      331. ''' Das <seealso cref="Memory">Memory</seealso>-Objekt, das in einer gewissen Zeit abgearbeitet wurde.
      332. ''' </param>
      333. ''' <param name="time">
      334. ''' Die Zeit, in der das <seealso cref="Memory">Memory</seealso>-Objekt abgearbeitet wurde.
      335. ''' </param>
      336. Public Sub New(memory As Memory, time As TimeSpan)
      337. VarMemory = memory
      338. VarTime = time
      339. End Sub
      340. ''' <summary>
      341. ''' Initialisiert die Struktur mit der Anzahl Bytes und eines Zeitspanne.
      342. ''' </summary>
      343. ''' <param name="memUnit">Die Memory-Einheit</param>
      344. ''' <param name="timeUnit">Die Zeiteinheit </param>
      345. ''' <param name="value">Wert der Geschwindigkeit</param>
      346. Public Sub New(memUnit As MemoryUnit, timeUnit As TimeUnit, value#)
      347. Me.New(0, TimeSpan.FromSeconds(1))
      348. Me.Value(memUnit, timeUnit) = value
      349. End Sub
      350. #End Region
      351. #Region "Variables"
      352. Private VarMemory As Memory
      353. Private VarTime As TimeSpan
      354. #End Region
      355. #Region "Properties"
      356. ''' <summary>
      357. ''' Gibt die gespeicherte Geschwindigkeit im angegebenen Format zurück oder legt sie fest.
      358. ''' </summary>
      359. ''' <param name="memUnit">Das Memory-Format</param>
      360. ''' <param name="timeUnit">Das Zeit-Format</param>
      361. Default Public Property Value(memUnit As MemoryUnit, timeUnit As TimeUnit) As Double
      362. Get
      363. Select Case timeUnit
      364. Case Is = timeUnit.Second
      365. Return VarMemory(memUnit) / VarTime.TotalSeconds
      366. Case Is = timeUnit.Minute
      367. Return VarMemory(memUnit) / VarTime.TotalMinutes
      368. Case Else
      369. Return VarMemory(memUnit) / VarTime.TotalHours
      370. End Select
      371. End Get
      372. Set(value As Double)
      373. Select Case timeUnit
      374. Case Is = timeUnit.Second
      375. VarMemory = New Memory(memUnit, value * VarTime.TotalSeconds)
      376. Case Is = timeUnit.Minute
      377. VarMemory = New Memory(memUnit, value * VarTime.TotalMinutes)
      378. Case Else
      379. VarMemory = New Memory(memUnit, value * VarTime.TotalHours)
      380. End Select
      381. End Set
      382. End Property
      383. #End Region
      384. #Region "Functions"
      385. ''' <summary>
      386. ''' Bildet die gespeicherte Geschwindigkeit in einem geeigneten Format ab(B/s).
      387. ''' </summary>
      388. Public Overrides Function ToString() As String
      389. Return ToString(MemoryUnit.Bestdecimal, TimeUnit.Second)
      390. End Function
      391. ''' <summary>
      392. ''' Bildet die gespeicherte Geschwindigkeit nach bestimmten Parametern ab(X/s).
      393. ''' </summary>
      394. ''' <param name="unit">Das Memory-Format</param>
      395. Public Overloads Function ToString(unit As MemoryUnit) As String
      396. Return ToString(unit, TimeUnit.Second)
      397. End Function
      398. ''' <summary>
      399. ''' Bildet die gespeicherte Geschwindigkeit nach bestimmten Parametern ab(B/x).
      400. ''' </summary>
      401. ''' <param name="unit">Das Zeit-Format</param>
      402. Public Overloads Function ToString(unit As TimeUnit) As String
      403. Return ToString(MemoryUnit.Bestdecimal, unit)
      404. End Function
      405. ''' <summary>
      406. ''' Bildet die gespeicherte Geschwindigkeit nach bestimmten Parametern ab(X/y).
      407. ''' </summary>
      408. ''' <param name="memUnit">Das Memory-Format</param>
      409. ''' <param name="timeUnit">Das Zeit-Format</param>
      410. Public Overloads Function ToString(memUnit As MemoryUnit, timeUnit As TimeUnit) As String
      411. Select Case timeUnit
      412. Case Is = timeUnit.Second
      413. Return CalculateProcessedMemory(TimeSpan.FromSeconds(1)).ToString(memUnit) & "/s"
      414. Case Is = timeUnit.Minute
      415. Return CalculateProcessedMemory(TimeSpan.FromMinutes(1)).ToString(memUnit) & "/m"
      416. Case Else
      417. Return CalculateProcessedMemory(TimeSpan.FromHours(1)).ToString(memUnit) & "/h"
      418. End Select
      419. End Function
      420. ''' <summary>
      421. ''' Initialisiert die Struktur mit einer festgelegten Geschwindigkeit.
      422. ''' </summary>
      423. ''' <param name="memUnit">Das Memory-Format</param>
      424. ''' <param name="timeUnit">Das Zeit-Format</param>
      425. ''' <param name="value">Der Wert der Geschwindigkeit</param>
      426. Public Shared Function FromValue(memUnit As MemoryUnit, timeUnit As TimeUnit, value#) As MemoryVelocity
      427. Dim MV As New MemoryVelocity(New Memory(memUnit, 10), New TimeSpan(0, 0, 10))
      428. MV.Value(memUnit, timeUnit) = value
      429. Return MV
      430. End Function
      431. ''' <summary>
      432. ''' Parst den Wert und die Einheit aus einem String.
      433. ''' </summary>
      434. ''' <param name="str">String, der die Daten zur Grösse beinhaltet</param>
      435. Public Shared Function Parse(str$) As MemoryVelocity
      436. Dim MemUnitDic = GetMemDic()
      437. Dim TimeDic = GetTimeDic()
      438. Dim Pattern = "(^|[^0-9])(?<Value>[0-9]+?((,|\.)[0-9]+?|))[^0-9,\.]*?(?<Memoryunit>({0}))(\/|\s*?per\s*?|\s*?pro\s*?|\s*?in\s*?)(?<Timeunit>({1}))"
      439. Pattern = String.Format(Pattern, String.Join("|", MemUnitDic.ToList.ConvertAll(Function(x) x.Key)), String.Join("|", TimeDic.ToList.ConvertAll(Function(x) x.Key)))
      440. If Regex.IsMatch(str, Pattern) Then
      441. Dim M = Regex.Match(str, Pattern, RegexOptions.IgnoreCase)
      442. Dim Value = Double.Parse(M.Groups("Value").Value.Replace(","c, "."c))
      443. Dim MemUnitstr = M.Groups("Memoryunit").Value
      444. Dim TimeUnitstr = M.Groups("Timeunit").Value
      445. Dim MemUnit = MemUnitDic.ToList.First(Function(x) Regex.IsMatch(MemUnitstr, "^" & x.Key & "$", RegexOptions.IgnoreCase)).Value
      446. Dim TimeUnit = TimeDic.ToList.First(Function(x) Regex.IsMatch(TimeUnitstr, "^" & x.Key & "$", RegexOptions.IgnoreCase)).Value
      447. Return FromValue(MemUnit, TimeUnit, Value)
      448. Else
      449. Return Nothing
      450. End If
      451. End Function
      452. Private Shared Function GetMemDic() As Dictionary(Of String, MemoryUnit)
      453. Dim Dic As New Dictionary(Of String, MemoryUnit)
      454. Dic.Add("(b|byte)", MemoryUnit.Byte)
      455. Dic.Add("(kibit|kibibit)", MemoryUnit.Kibibit)
      456. Dic.Add("(kib|kibibyte)", MemoryUnit.Kibibyte)
      457. Dic.Add("(kbit|kilobit)", MemoryUnit.Kilobit)
      458. Dic.Add("(kb|kbyte|kilobyte)", MemoryUnit.Kilobyte)
      459. Dic.Add("(mebit|mebibit)", MemoryUnit.Mebibit)
      460. Dic.Add("(meb|mebibyte)", MemoryUnit.Mebibyte)
      461. Dic.Add("(mbit|megabit)", MemoryUnit.Megabit)
      462. Dic.Add("(mb|megabyte)", MemoryUnit.Megabyte)
      463. Dic.Add("(gibit|gibibit)", MemoryUnit.Gibibit)
      464. Dic.Add("(gib|gibibyte)", MemoryUnit.Gibibyte)
      465. Dic.Add("(gbit|gigabit)", MemoryUnit.Gigabit)
      466. Dic.Add("(gb|gigabyte)", MemoryUnit.Gigabyte)
      467. Dic.Add("(tebbit|tebibit)", MemoryUnit.Tebibit)
      468. Dic.Add("(tebb|tebibyte)", MemoryUnit.Tebibyte)
      469. Dic.Add("(tbit|terabit)", MemoryUnit.Terabit)
      470. Dic.Add("(tb|terabyte)", MemoryUnit.Terabyte)
      471. Dic.Add("(pbibit|pebibit)", MemoryUnit.Pebibit)
      472. Dic.Add("(pbib|pebibyte)", MemoryUnit.Pebibyte)
      473. Dic.Add("(pbit|petabit)", MemoryUnit.Petabit)
      474. Dic.Add("(pb|petabyte)", MemoryUnit.Petabyte)
      475. Dic.Add("(exbibit)", MemoryUnit.Exbibit)
      476. Dic.Add("(exbib|exbibyte)", MemoryUnit.Exbibyte)
      477. Dic.Add("(ebit|exabit)", MemoryUnit.Exabit)
      478. Dic.Add("(eb|exab|exabyte)", MemoryUnit.Exabyte)
      479. Return Dic
      480. End Function
      481. Private Shared Function GetTimeDic() As Dictionary(Of String, TimeUnit)
      482. Dim Dic As New Dictionary(Of String, TimeUnit)
      483. Dic.Add("(s|second|sekunde)", TimeUnit.Second)
      484. Dic.Add("(min|minute)", TimeUnit.Minute)
      485. Dic.Add("(h|hour|stunde)", TimeUnit.Hour)
      486. Return Dic
      487. End Function
      488. ''' <summary>
      489. ''' Errechnet den zu erwartenden Speicherverarbeitung in einer bestimmten Zeit.
      490. ''' </summary>
      491. ''' <param name="time">Die Zeit, zu der der voraussichtliche Fortschritt erreicht sein wird.</param>
      492. Public Function CalculateProcessedMemory(time As TimeSpan) As Memory
      493. Return Me * time
      494. End Function
      495. ''' <summary>
      496. ''' Errechnet den zu erwartenden zeitlichen Aufwand, eine bestimmte Menge zu verarbeiten.
      497. ''' </summary>
      498. ''' <param name="memory">Die zu verabeitende Menge</param>
      499. Public Function CalculatePassedTime(memory As Memory) As TimeSpan
      500. Return memory / Me
      501. End Function
      502. ''' <summary>
      503. ''' Klont die aktuelle Instanz.
      504. ''' </summary>
      505. Public Function Clone() As Object Implements ICloneable.Clone
      506. Return New MemoryVelocity(VarMemory, VarTime)
      507. End Function
      508. ''' <summary>
      509. ''' Vergleicht zwei MemoryVelocity-Instanzen und gibt so die Sortierreihenfolge an.
      510. ''' </summary>
      511. ''' <param name="obj">Ein zu vergleichendes MemoryVelocity-Objekt</param>
      512. Public Function CompareTo(obj As Object) As Integer Implements IComparable.CompareTo
      513. Dim V = DirectCast(obj, MemoryVelocity)
      514. If Value(MemoryUnit.Byte, TimeUnit.Second) < V.Value(MemoryUnit.Byte, TimeUnit.Second) Then Return 1
      515. If Me = V Then Return 0
      516. Return - 1
      517. End Function
      518. #End Region
      519. #Region "Operators"
      520. Public Shared Operator +(v1 As MemoryVelocity, v2 As MemoryVelocity) As MemoryVelocity
      521. Return FromValue(MemoryUnit.Byte, TimeUnit.Second, v1.Value(MemoryUnit.Byte, TimeUnit.Second) + v2.Value(MemoryUnit.Byte, TimeUnit.Second))
      522. End Operator
      523. Public Shared Operator -(v1 As MemoryVelocity, v2 As MemoryVelocity) As MemoryVelocity
      524. Return v1 + - v2
      525. End Operator
      526. Public Shared Operator -(v As MemoryVelocity) As MemoryVelocity
      527. Return FromValue(MemoryUnit.Byte, TimeUnit.Second, - v.Value(MemoryUnit.Byte, TimeUnit.Second))
      528. End Operator
      529. Public Shared Operator =(v1 As MemoryVelocity, v2 As MemoryVelocity) As Boolean
      530. Return Math.Abs(v1.Value(MemoryUnit.Byte, TimeUnit.Second) - v2.Value(MemoryUnit.Byte, TimeUnit.Second)) < Double.Epsilon
      531. End Operator
      532. Public Shared Operator <>(v1 As MemoryVelocity, v2 As MemoryVelocity) As Boolean
      533. Return Not v1 = v2
      534. End Operator
      535. Public Shared Operator <(v1 As MemoryVelocity, v2 As MemoryVelocity) As Boolean
      536. Return v1.Value(MemoryUnit.Byte, TimeUnit.Second) < v2.Value(MemoryUnit.Byte, TimeUnit.Second)
      537. End Operator
      538. Public Shared Operator >(v1 As MemoryVelocity, v2 As MemoryVelocity) As Boolean
      539. Return v2 < v1
      540. End Operator
      541. Public Shared Operator <=(v1 As MemoryVelocity, v2 As MemoryVelocity) As Boolean
      542. Return v1.Value(MemoryUnit.Byte, TimeUnit.Second) <= v2.Value(MemoryUnit.Byte, TimeUnit.Second)
      543. End Operator
      544. Public Shared Operator >=(v1 As MemoryVelocity, v2 As MemoryVelocity) As Boolean
      545. Return v2 <= v1
      546. End Operator
      547. Public Shared Operator /(v1 As MemoryVelocity, v2 As MemoryVelocity) As Double
      548. Return v1.Value(MemoryUnit.Byte, TimeUnit.Second) / v2.Value(MemoryUnit.Byte, TimeUnit.Second)
      549. End Operator
      550. Public Shared Operator \(v1 As MemoryVelocity, v2 As MemoryVelocity) As Long
      551. Return CLng(v1.Value(MemoryUnit.Byte, TimeUnit.Second)) \ CLng(v2.Value(MemoryUnit.Byte, TimeUnit.Second))
      552. End Operator
      553. Public Shared Operator Mod(v1 As MemoryVelocity, v2 As MemoryVelocity) As MemoryVelocity
      554. Return FromValue(MemoryUnit.Byte, TimeUnit.Second, v1.Value(MemoryUnit.Byte, TimeUnit.Second) Mod v2.Value(MemoryUnit.Byte, TimeUnit.Second))
      555. End Operator
      556. Public Shared Widening Operator CType(v As MemoryVelocity) As Double
      557. Return v.Value(MemoryUnit.Byte, TimeUnit.Second)
      558. End Operator
      559. Public Shared Widening Operator CType(v As MemoryVelocity) As Long
      560. Return CLng(v.Value(MemoryUnit.Byte, TimeUnit.Second))
      561. End Operator
      562. Public Shared Widening Operator CType(d#) As MemoryVelocity
      563. Return FromValue(MemoryUnit.Byte, TimeUnit.Second, d)
      564. End Operator
      565. Public Shared Widening Operator CType(l&) As MemoryVelocity
      566. Return FromValue(MemoryUnit.Byte, TimeUnit.Second, l)
      567. End Operator
      568. Public Shared Operator +(v As MemoryVelocity, d#) As MemoryVelocity
      569. Return FromValue(MemoryUnit.Byte, TimeUnit.Second, v.Value(MemoryUnit.Byte, TimeUnit.Second) + d)
      570. End Operator
      571. Public Shared Operator -(v As MemoryVelocity, d#) As MemoryVelocity
      572. Return v + - d
      573. End Operator
      574. Public Shared Operator =(v As MemoryVelocity, d#) As Boolean
      575. Return Math.Abs(v.Value(MemoryUnit.Byte, TimeUnit.Second) - d) < Double.Epsilon
      576. End Operator
      577. Public Shared Operator <>(v As MemoryVelocity, d#) As Boolean
      578. Return Not v = d
      579. End Operator
      580. Public Shared Operator <(v As MemoryVelocity, d#) As Boolean
      581. Return v.Value(MemoryUnit.Byte, TimeUnit.Second) < d
      582. End Operator
      583. Public Shared Operator >(v As MemoryVelocity, d#) As Boolean
      584. Return v.Value(MemoryUnit.Byte, TimeUnit.Second) > d
      585. End Operator
      586. Public Shared Operator <=(v As MemoryVelocity, d#) As Boolean
      587. Return v.Value(MemoryUnit.Byte, TimeUnit.Second) <= d
      588. End Operator
      589. Public Shared Operator >=(v As MemoryVelocity, d#) As Boolean
      590. Return v.Value(MemoryUnit.Byte, TimeUnit.Second) >= d
      591. End Operator
      592. Public Shared Operator *(v As MemoryVelocity, d#) As MemoryVelocity
      593. Return FromValue(MemoryUnit.Byte, TimeUnit.Second, v.Value(MemoryUnit.Byte, TimeUnit.Second) * d)
      594. End Operator
      595. Public Shared Operator /(v As MemoryVelocity, d#) As MemoryVelocity
      596. Return FromValue(MemoryUnit.Byte, TimeUnit.Second, v.Value(MemoryUnit.Byte, TimeUnit.Second) / d)
      597. End Operator
      598. Public Shared Operator Mod(v As MemoryVelocity, d#) As MemoryVelocity
      599. Return FromValue(MemoryUnit.Byte, TimeUnit.Second, v.Value(MemoryUnit.Byte, TimeUnit.Second) Mod d)
      600. End Operator
      601. Public Shared Operator ^(v As MemoryVelocity, d#) As MemoryVelocity
      602. Return FromValue(MemoryUnit.Byte, TimeUnit.Second, v.Value(MemoryUnit.Byte, TimeUnit.Second) ^ d)
      603. End Operator
      604. Public Shared Operator *(v As MemoryVelocity, t As TimeSpan) As Memory
      605. Return New Memory(MemoryUnit.Byte, v.Value(MemoryUnit.Byte, TimeUnit.Second) * t.TotalSeconds)
      606. End Operator
      607. #End Region
      608. End Structure
      609. ''' <summary>
      610. ''' Gibt eine Auswahl an Zeit-Formaten.
      611. ''' </summary>
      612. Public Enum TimeUnit
      613. Second
      614. Minute
      615. Hour
      616. End Enum


      Nutzen könnt ihr sie auf verschiedene Art und Weise. Was alles funktioniert:
      Bei der Memory-Struktur:
      • Wert in allen Einheitsgrössen abrufen/ändern.
      • Aus String filtern
      • Aus Datei Grösse auslesen
      • Aus Internetressource Grösse auslesen
      • Leeren Buffer generieren
      • +, -, =, <>, <, >, <=, >=, /, \, Mod-Operatoren mit anderen Memory-Objekten
      • Implizite Konvertierung von/zu Long/Double(für Anzahl Bytes)
      • +, -, =, <>, <, >, <=, >=, /, \, *, Mod, ^-Operatoren mit Long/Double-Werten
      • /-Oparator mit TimeSpan-Objekt, um MemoryVelocity zu erhalten
      • /-Oparator mit MemoryVelocity-Objekt, um TimeSpan zu erhalten
      • Formatiermöglichkeit in allen Einheitsgrössen für die ToString-Methode

      Bei der MemoryVelocity-Struktur:
      • Wert in allen Einheitsgrössen abrufen/ändern.
      • Aus String filtern
      • Aus Wert und Einheiten initialisieren.
      • Voraussichtliche Grösse zu einem bestimmten Zeitpunkt errechnen.
      • Voraussichtlichen Zeitaufwand für eine bestimmte Menge errechnen.
      • +, -, -(negieren), =, <>, <, >, <=, >=, /, \, Mod-Operatoren mit anderen MemoryVelocity-Objekten
      • Implizite Konvertierung von/zu Long/Double(für Anzahl Bytes)
      • +, -, =, <>, <, >, <=, >=, /, *, Mod, ^-Operatoren mit Long/Double-Werten
      • *-Oparator mit TimeSpan-Objekt, um Memory zu erhalten
      • Formatiermöglichkeit in allen Einheitsgrössen für die ToString-Methode
      Klassendiagramm:



      Es reicht einfache Mathematik:

      Quellcode

      1. V = n/t
      2. n = t*V
      3. t = n/V


      Beispiel:

      VB.NET-Quellcode

      1. 'Laden von Bytebasierenden Grössen:
      2. Dim DownloadSize = Memory.FromURI(New Uri("http://3.bp.blogspot.com/-Jk0gUehmM40/TmkwT_ZftFI/AAAAAAAAAB8/YyTcq9guuJQ/s1600/msdn.png"))
      3. Dim AdditionalSize As New Memory(MemoryUnit.Byte, 32.4)
      4. Dim FileSize = Memory.FromFile("." & My.Application.Info.AssemblyName)
      5. 'Kombinieren:
      6. Dim FinalSize = DownloadSize + AdditionalSize + FileSize
      7. 'Ausgeben:
      8. Console.WriteLine(DownloadSize.ToString)
      9. Console.WriteLine("+")
      10. Console.WriteLine(AdditionalSize.ToString)
      11. Console.WriteLine("+")
      12. Console.WriteLine(FileSize.ToString)
      13. Console.WriteLine("=")
      14. Console.WriteLine(FinalSize.ToString(MemoryUnit.Megabyte))
      15. Console.ReadLine()
      16. 'Oder auch das funktioniert:
      17. Dim StringToParse = "Die Datei ist ca. 128,43 Megabyte gross und wird mit einer Geschwindigkeit von 42.32 Mbit/s übertragen."
      18. Dim ExtractedMemory = Memory.Parse(StringToParse)
      19. Dim ExtractedMemoryVelocity = MemoryVelocity.Parse(StringToParse)
      20. Dim TimeOfCompletion = ExtractedMemory / ExtractedMemoryVelocity


      Grüsse

      Higlav

      Dieser Beitrag wurde bereits 19 mal editiert, zuletzt von „Higlav“ ()

      Hab zwar grad keinen Bock, das Zeugs komplett neu zu machen, jedoch kann ich dir ja nen Ansatz geben, wie ich es machen würde:
      Anstatt den dutzenden Funktionen wie FromKiloBytes und Properties wie Gigabytes, würde ich zwei Funktionen machen:

      VB.NET-Quellcode

      1. Public Shared Function Create(unit As MemorySizeUnit, value As Long) As Memory
      2. Public Function GetValue(unit As MemorySizeUnit) As Double
      3. Public Enum MemorySizeUnit As Long
      4. Bit = 1
      5. [Byte] = 8
      6. KiloBit = Bit * 1000
      7. KibiBit = Bit * 1024
      8. KiloByte = [Byte] * 1000
      9. KibiByte = [Byte] * 1024
      10. MegaBit = Bit * CLng(1000 ^ 2)
      11. MebiBit = Bit * CLng(1024 ^ 2)
      12. MegaByte = [Byte] * CLng(1000 ^ 2)
      13. MebiByte = [Byte] * CLng(1024 ^ 2)
      14. '...
      15. End Enum

      Über die Werte im Enum könntest du dann ganz einfach die einzelnen Werte berechnen :).
      So, ich habe gerade ein paar Fehler ausgemerzt. Jetzt müsste soweit alles funktionieren(die ToString-Methode hatte einen kleinen Haken).