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
Nutzen könnt ihr sie auf verschiedene Art und Weise. Was alles funktioniert:
Bei der Memory-Struktur:
Bei der MemoryVelocity-Struktur:
Es reicht einfache Mathematik:
Beispiel:
Grüsse
Higlav
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:
Quellcode
- Imports System.Text
- Imports System.Text.RegularExpressions
- Imports System.IO
- Imports System.Net
- ''' <summary>
- ''' Bietet Formatierungsmöglichkeiten für Byte-Grössen.
- ''' </summary>
- Public Structure Memory
- Implements IComparable, ICloneable
- #Region "Initializers"
- ''' <summary>
- ''' Initialisiert die Struktur mit einem Wert, der den angegebenen Details entspricht.
- ''' </summary>
- ''' <param name="unit">Die Einheit.</param>
- ''' <param name="value">Der Wert</param>
- ''' <remarks></remarks>
- Public Sub New(unit As MemoryUnit, value#)
- If unit = MemoryUnit.Bestbinary OrElse unit = MemoryUnit.Bestdecimal Then Throw New ArgumentOutOfRangeException("unit")
- VarBytes = CLng(value * unit)
- End Sub
- ''' <summary>
- ''' Initialisiert die Struktur mit einem Byte-Wert.
- ''' </summary>
- ''' <param name="amount">Der Wert</param>
- ''' <remarks></remarks>
- Public Sub New(amount&)
- VarBytes = amount
- End Sub
- #End Region
- #Region "Variables"
- Private VarBytes&
- Public Shared ReadOnly Zero As New Memory(0)
- 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}"})
- #End Region
- #Region "Properties"
- ''' <summary>
- ''' Gibt die gespeicherte Grösse im angegebenen Format aus oder legt sie fest.
- ''' </summary>
- ''' <param name="unit">Die Einheit.</param>
- ''' <value></value>
- ''' <returns></returns>
- ''' <remarks></remarks>
- Default Public Property Value(unit As MemoryUnit) As Double
- Get
- If unit = MemoryUnit.Bestbinary OrElse unit = MemoryUnit.Bestdecimal Then Return VarBytes
- Return VarBytes / unit
- End Get
- Set(value As Double)
- VarBytes = CLng(value * unit)
- End Set
- End Property
- #End Region
- #Region "Functions"
- ''' <summary>
- ''' Bildet die gespeicherte Grösse in einem geeigneten Byte-basierenden Format ab.
- ''' </summary>
- ''' <returns></returns>
- ''' <remarks></remarks>
- Public Overrides Function ToString() As String
- Return ToString(MemoryUnit.Bestdecimal)
- End Function
- ''' <summary>
- ''' Bildet die gespeicherte Grösse nach bestimmten Parametern ab.
- ''' </summary>
- ''' <param name="unit">Die Einheit.</param>
- ''' <returns></returns>
- ''' <remarks></remarks>
- Public Overloads Function ToString(unit As MemoryUnit) As String
- Dim First = String.Empty
- Dim Second = String.Empty
- Dim Border = 1000
- If unit = MemoryUnit.Bestbinary OrElse unit.ToString.Contains("bib") Then First = "i" : Border = 1024
- If unit.ToString.Contains("bit") Then Second = "it"
- If Not unit = MemoryUnit.Bestbinary AndAlso Not unit = MemoryUnit.Bestdecimal Then
- Dim S = VarEndings.First(Function(x) x.Substring(4, 1) = Regex.Match(unit.ToString, "^[A-Z]").Value)
- Return String.Format(S, Value(unit), First, Second)
- End If
- Dim D# = VarBytes
- For i = 0 To [Enum].GetValues(GetType(MemoryUnit)).Length - 1
- If D < Border Then
- Return String.Format(VarEndings(i), D.ToString("###0.0#"), First, Second)
- End If
- D /= Border
- Next
- Return String.Empty
- End Function
- ''' <summary>
- ''' Initialisiert die Struktur mit einer Dateigrösse.
- ''' </summary>
- ''' <param name="path">Der Pfad, der auf eine Datei zeigt.</param>
- ''' <returns></returns>
- ''' <remarks></remarks>
- Public Shared Function FromFile(path$) As Memory
- Return New FileInfo(path).Length
- End Function
- ''' <summary>
- ''' Initialisiert die Struktur mit der Grösse einer Internetressource.
- ''' </summary>
- ''' <param name="address">Ein Uri-Objekt, das auf eine Internetressource zeigt.</param>
- ''' <returns></returns>
- ''' <remarks></remarks>
- Public Shared Function FromUri(address As Uri) As Memory
- Dim Req = HttpWebRequest.CreateHttp(address)
- Using Resp = DirectCast(Req.GetResponse, HttpWebResponse)
- Return Resp.ContentLength
- End Using
- End Function
- ''' <summary>
- ''' Parst den Wert und die Einheit aus einem String.
- ''' </summary>
- ''' <param name="str">String, der die Daten zur Grösse beinhaltet</param>
- ''' <returns></returns>
- ''' <remarks></remarks>
- Public Shared Function Parse(str$) As Memory
- Dim Dic = GetDic()
- Dim Pattern = "(^|[^0-9])(?<Value>[0-9]+?((,|\.)[0-9]+?|))[^0-9,\.]*?(?<Unit>(" & String.Join("|", Dic.ToList.ConvertAll(Function(x) x.Key)) & "))"
- If Regex.IsMatch(str, Pattern) Then
- Dim M = Regex.Match(str, Pattern, RegexOptions.IgnoreCase)
- Dim Value = Double.Parse(M.Groups("Value").Value.Replace(","c, "."c))
- Dim Unit = Dic(M.Groups("Unit").Value)
- Return New Memory(Unit, Value)
- Else
- Return Nothing
- End If
- End Function
- Private Shared Function GetDic() As Dictionary(Of String, MemoryUnit)
- Dim Dic As New Dictionary(Of String, MemoryUnit)
- Dic.Add("(b|byte)", MemoryUnit.Byte)
- Dic.Add("(kibit|kibibit)", MemoryUnit.Kibibit)
- Dic.Add("(kib|kibibyte)", MemoryUnit.Kibibyte)
- Dic.Add("(kbit|kilobit)", MemoryUnit.Kilobit)
- Dic.Add("(kb|kbyte|kilobyte)", MemoryUnit.Kilobyte)
- Dic.Add("(mebit|mebibit)", MemoryUnit.Mebibit)
- Dic.Add("(meb|mebibyte)", MemoryUnit.Mebibyte)
- Dic.Add("(mbit|megabit)", MemoryUnit.Megabit)
- Dic.Add("(mb|megabyte)", MemoryUnit.Megabyte)
- Dic.Add("(gibit|gibibit)", MemoryUnit.Gibibit)
- Dic.Add("(gib|gibibyte)", MemoryUnit.Gibibyte)
- Dic.Add("(gbit|gigabit)", MemoryUnit.Gigabit)
- Dic.Add("(gb|gigabyte)", MemoryUnit.Gigabyte)
- Dic.Add("(tebbit|tebibit)", MemoryUnit.Tebibit)
- Dic.Add("(tebb|tebibyte)", MemoryUnit.Tebibyte)
- Dic.Add("(tbit|terabit)", MemoryUnit.Terabit)
- Dic.Add("(tb|terabyte)", MemoryUnit.Terabyte)
- Dic.Add("(pbibit|pebibit)", MemoryUnit.Pebibit)
- Dic.Add("(pbib|pebibyte)", MemoryUnit.Pebibyte)
- Dic.Add("(pbit|petabit)", MemoryUnit.Petabit)
- Dic.Add("(pb|petabyte)", MemoryUnit.Petabyte)
- Dic.Add("(exbibit)", MemoryUnit.Exbibit)
- Dic.Add("(exbib|exbibyte)", MemoryUnit.Exbibyte)
- Dic.Add("(ebit|exabit)", MemoryUnit.Exabit)
- Dic.Add("(eb|exab|exabyte)", MemoryUnit.Exabyte)
- Return Dic
- End Function
- ''' <summary>
- ''' Generiert einen leeren Buffer.
- ''' </summary>
- ''' <returns></returns>
- ''' <remarks></remarks>
- Public Function CreateBuffer() As Byte()
- Dim Buf(CInt(VarBytes)) As Byte
- Return Buf
- End Function
- ''' <summary>
- ''' Klont die aktuelle Instanz.
- ''' </summary>
- ''' <returns></returns>
- ''' <remarks></remarks>
- Public Function Clone() As Object Implements ICloneable.Clone
- Return New Memory(VarBytes)
- End Function
- ''' <summary>
- ''' Vergleicht zwei Memory-Instanzen und gibt so die Sortierreihenfolge an.
- ''' </summary>
- ''' <param name="obj">Ein zu vergleichendes Memory-Objekt</param>
- ''' <returns></returns>
- ''' <remarks></remarks>
- Public Function CompareTo(obj As Object) As Integer Implements IComparable.CompareTo
- Dim M = DirectCast(obj, Memory)
- If Value(MemoryUnit.Byte) < M.Value(MemoryUnit.Byte) Then Return 1
- If Me = M Then Return 0
- Return - 1
- End Function
- #End Region
- #Region "Operators"
- Public Shared Operator +(m1 As Memory, m2 As Memory) As Memory
- Return New Memory(m1.VarBytes + m2.VarBytes)
- End Operator
- Public Shared Operator -(m1 As Memory, m2 As Memory) As Memory
- Return New Memory(m1.VarBytes - m2.VarBytes)
- End Operator
- Public Shared Operator =(m1 As Memory, m2 As Memory) As Boolean
- Return m1.VarBytes = m2.VarBytes
- End Operator
- Public Shared Operator <>(m1 As Memory, m2 As Memory) As Boolean
- Return m1.VarBytes <> m2.VarBytes
- End Operator
- Public Shared Operator <(m1 As Memory, m2 As Memory) As Boolean
- Return m1.VarBytes < m2.VarBytes
- End Operator
- Public Shared Operator >(m1 As Memory, m2 As Memory) As Boolean
- Return m1.VarBytes > m2.VarBytes
- End Operator
- Public Shared Operator /(m1 As Memory, m2 As Memory) As Double
- Return m1.VarBytes / m2.VarBytes
- End Operator
- Public Shared Operator \(m1 As Memory, m2 As Memory) As Long
- Return m1.VarBytes \ m2.VarBytes
- End Operator
- Public Shared Operator <=(m1 As Memory, m2 As Memory) As Boolean
- Return m1.VarBytes <= m2.VarBytes
- End Operator
- Public Shared Operator >=(m1 As Memory, m2 As Memory) As Boolean
- Return m1.VarBytes >= m2.VarBytes
- End Operator
- Public Shared Operator Mod(m1 As Memory, m2 As Memory) As Memory
- Return New Memory(m1.VarBytes Mod m2.VarBytes)
- End Operator
- Public Shared Widening Operator CType(m As Memory) As Long
- Return m.VarBytes
- End Operator
- Public Shared Widening Operator CType(d&) As Memory
- Return New Memory(d)
- End Operator
- Public Shared Operator +(m As Memory, d&) As Memory
- Return New Memory(m.VarBytes + d)
- End Operator
- Public Shared Operator -(m As Memory, d&) As Memory
- Return New Memory(m.VarBytes - d)
- End Operator
- Public Shared Operator =(m As Memory, d&) As Boolean
- Return m.VarBytes = d
- End Operator
- Public Shared Operator <>(m As Memory, d&) As Boolean
- Return Not m = d
- End Operator
- Public Shared Operator <(m As Memory, d&) As Boolean
- Return m.VarBytes < d
- End Operator
- Public Shared Operator >(m As Memory, d&) As Boolean
- Return m.VarBytes > d
- End Operator
- Public Shared Operator /(m As Memory, d#) As Memory
- Return New Memory(CLng(m.VarBytes / d))
- End Operator
- Public Shared Operator \(m As Memory, d&) As Memory
- Return New Memory(CLng(m.VarBytes \ d))
- End Operator
- Public Shared Operator *(m As Memory, d#) As Memory
- Return New Memory(CLng(m.VarBytes * d))
- End Operator
- Public Shared Operator <=(m As Memory, d&) As Boolean
- Return m.VarBytes <= d
- End Operator
- Public Shared Operator >=(m As Memory, d&) As Boolean
- Return m.VarBytes >= d
- End Operator
- Public Shared Operator Mod(m As Memory, d#) As Memory
- Return New Memory(CLng(m.VarBytes Mod d))
- End Operator
- Public Shared Operator ^(m As Memory, d#) As Memory
- Return New Memory(CLng(m.VarBytes ^ d))
- End Operator
- Public Shared Operator /(m As Memory, t As TimeSpan) As MemoryVelocity
- Return New MemoryVelocity(m, t)
- End Operator
- Public Shared Operator /(m As Memory, v As MemoryVelocity) As TimeSpan
- Return TimeSpan.FromSeconds(m.VarBytes / v.Value(MemoryUnit.Byte, TimeUnit.Second))
- End Operator
- #End Region
- End Structure
- ''' <summary>
- ''' Gibt eine Auswahl an Einheiten für die Grösse.
- ''' </summary>
- ''' <remarks>
- ''' <see cref="MemoryUnit.Bestbinary">Bestbinary</see> und
- ''' <see cref="MemoryUnit.Bestdecimal">Bestdecimal</see> werden nur für die <see cref="Memory.ToString">ToString</see>-Methode verwendet. Wird bei falscher Handhabung als
- ''' <see
- ''' cref="MemoryUnit.[Byte]">
- ''' Byte
- ''' </see>
- ''' behandelt.
- ''' </remarks>
- Public Enum MemoryUnit As Long
- [Byte] = 1
- Kilobyte = 1000
- Kilobit = 1000 \ 8
- Kibibyte = CLng(2 ^ 10)
- Kibibit = CLng(2 ^ 10) \ 8
- Megabyte = 1000000
- Megabit = 1000000 \ 8
- Mebibyte = CLng(2 ^ 20)
- Mebibit = CLng(2 ^ 20) \ 8
- Gigabyte = 1000000000
- Gigabit = 1000000000 \ 8
- Gibibyte = CLng(2 ^ 30)
- Gibibit = CLng(2 ^ 30) \ 8
- Terabyte = 1000000000000
- Terabit = 1000000000000 \ 8
- Tebibyte = CLng(2 ^ 40)
- Tebibit = CLng(2 ^ 40) \ 8
- Petabyte = 1000000000000000
- Petabit = 1000000000000000 \ 8
- Pebibyte = CLng(2 ^ 50)
- Pebibit = CLng(2 ^ 50) \ 8
- Exabyte = 1000000000000000000
- Exabit = 1000000000000000000 \ 8
- Exbibyte = CLng(2 ^ 60)
- Exbibit = CLng(2 ^ 60) \ 8
- ''' <summary>
- ''' Sucht bei Anwendung bei der <seealso cref="Memory.ToString">ToString</seealso>-Methode die optisch beste Einheit(1'024er-Schritte).
- ''' </summary>
- ''' <remarks>Nicht für die Initialisierung zu verwenden.</remarks>
- Bestbinary
- ''' <summary>
- ''' Sucht bei Anwendung bei der <seealso cref="Memory.ToString">ToString</seealso>-Methode die optisch beste Einheit(1'000er-Schritte).
- ''' </summary>
- ''' <remarks>Nicht für die Initialisierung zu verwenden.</remarks>
- Bestdecimal
- End Enum
- ''' <summary>
- ''' Bietet Formatierungsmöglichkeiten für die Geschwindigkeit bei Byte-basierenden Grössen.
- ''' </summary>
- Public Structure MemoryVelocity
- Implements IComparable, ICloneable
- #Region "Initializers"
- ''' <summary>
- ''' Initialisiert die Struktur mit der Anzahl Bytes und eines Zeitspanne.
- ''' </summary>
- ''' <param name="memory">
- ''' Das <seealso cref="Memory">Memory</seealso>-Objekt, das in einer gewissen Zeit abgearbeitet wurde.
- ''' </param>
- ''' <param name="time">
- ''' Die Zeit, in der das <seealso cref="Memory">Memory</seealso>-Objekt abgearbeitet wurde.
- ''' </param>
- Public Sub New(memory As Memory, time As TimeSpan)
- VarMemory = memory
- VarTime = time
- End Sub
- ''' <summary>
- ''' Initialisiert die Struktur mit der Anzahl Bytes und eines Zeitspanne.
- ''' </summary>
- ''' <param name="memUnit">Die Memory-Einheit</param>
- ''' <param name="timeUnit">Die Zeiteinheit </param>
- ''' <param name="value">Wert der Geschwindigkeit</param>
- Public Sub New(memUnit As MemoryUnit, timeUnit As TimeUnit, value#)
- Me.New(0, TimeSpan.FromSeconds(1))
- Me.Value(memUnit, timeUnit) = value
- End Sub
- #End Region
- #Region "Variables"
- Private VarMemory As Memory
- Private VarTime As TimeSpan
- #End Region
- #Region "Properties"
- ''' <summary>
- ''' Gibt die gespeicherte Geschwindigkeit im angegebenen Format zurück oder legt sie fest.
- ''' </summary>
- ''' <param name="memUnit">Das Memory-Format</param>
- ''' <param name="timeUnit">Das Zeit-Format</param>
- Default Public Property Value(memUnit As MemoryUnit, timeUnit As TimeUnit) As Double
- Get
- Select Case timeUnit
- Case Is = timeUnit.Second
- Return VarMemory(memUnit) / VarTime.TotalSeconds
- Case Is = timeUnit.Minute
- Return VarMemory(memUnit) / VarTime.TotalMinutes
- Case Else
- Return VarMemory(memUnit) / VarTime.TotalHours
- End Select
- End Get
- Set(value As Double)
- Select Case timeUnit
- Case Is = timeUnit.Second
- VarMemory = New Memory(memUnit, value * VarTime.TotalSeconds)
- Case Is = timeUnit.Minute
- VarMemory = New Memory(memUnit, value * VarTime.TotalMinutes)
- Case Else
- VarMemory = New Memory(memUnit, value * VarTime.TotalHours)
- End Select
- End Set
- End Property
- #End Region
- #Region "Functions"
- ''' <summary>
- ''' Bildet die gespeicherte Geschwindigkeit in einem geeigneten Format ab(B/s).
- ''' </summary>
- Public Overrides Function ToString() As String
- Return ToString(MemoryUnit.Bestdecimal, TimeUnit.Second)
- End Function
- ''' <summary>
- ''' Bildet die gespeicherte Geschwindigkeit nach bestimmten Parametern ab(X/s).
- ''' </summary>
- ''' <param name="unit">Das Memory-Format</param>
- Public Overloads Function ToString(unit As MemoryUnit) As String
- Return ToString(unit, TimeUnit.Second)
- End Function
- ''' <summary>
- ''' Bildet die gespeicherte Geschwindigkeit nach bestimmten Parametern ab(B/x).
- ''' </summary>
- ''' <param name="unit">Das Zeit-Format</param>
- Public Overloads Function ToString(unit As TimeUnit) As String
- Return ToString(MemoryUnit.Bestdecimal, unit)
- End Function
- ''' <summary>
- ''' Bildet die gespeicherte Geschwindigkeit nach bestimmten Parametern ab(X/y).
- ''' </summary>
- ''' <param name="memUnit">Das Memory-Format</param>
- ''' <param name="timeUnit">Das Zeit-Format</param>
- Public Overloads Function ToString(memUnit As MemoryUnit, timeUnit As TimeUnit) As String
- Select Case timeUnit
- Case Is = timeUnit.Second
- Return CalculateProcessedMemory(TimeSpan.FromSeconds(1)).ToString(memUnit) & "/s"
- Case Is = timeUnit.Minute
- Return CalculateProcessedMemory(TimeSpan.FromMinutes(1)).ToString(memUnit) & "/m"
- Case Else
- Return CalculateProcessedMemory(TimeSpan.FromHours(1)).ToString(memUnit) & "/h"
- End Select
- End Function
- ''' <summary>
- ''' Initialisiert die Struktur mit einer festgelegten Geschwindigkeit.
- ''' </summary>
- ''' <param name="memUnit">Das Memory-Format</param>
- ''' <param name="timeUnit">Das Zeit-Format</param>
- ''' <param name="value">Der Wert der Geschwindigkeit</param>
- Public Shared Function FromValue(memUnit As MemoryUnit, timeUnit As TimeUnit, value#) As MemoryVelocity
- Dim MV As New MemoryVelocity(New Memory(memUnit, 10), New TimeSpan(0, 0, 10))
- MV.Value(memUnit, timeUnit) = value
- Return MV
- End Function
- ''' <summary>
- ''' Parst den Wert und die Einheit aus einem String.
- ''' </summary>
- ''' <param name="str">String, der die Daten zur Grösse beinhaltet</param>
- Public Shared Function Parse(str$) As MemoryVelocity
- Dim MemUnitDic = GetMemDic()
- Dim TimeDic = GetTimeDic()
- Dim Pattern = "(^|[^0-9])(?<Value>[0-9]+?((,|\.)[0-9]+?|))[^0-9,\.]*?(?<Memoryunit>({0}))(\/|\s*?per\s*?|\s*?pro\s*?|\s*?in\s*?)(?<Timeunit>({1}))"
- Pattern = String.Format(Pattern, String.Join("|", MemUnitDic.ToList.ConvertAll(Function(x) x.Key)), String.Join("|", TimeDic.ToList.ConvertAll(Function(x) x.Key)))
- If Regex.IsMatch(str, Pattern) Then
- Dim M = Regex.Match(str, Pattern, RegexOptions.IgnoreCase)
- Dim Value = Double.Parse(M.Groups("Value").Value.Replace(","c, "."c))
- Dim MemUnitstr = M.Groups("Memoryunit").Value
- Dim TimeUnitstr = M.Groups("Timeunit").Value
- Dim MemUnit = MemUnitDic.ToList.First(Function(x) Regex.IsMatch(MemUnitstr, "^" & x.Key & "$", RegexOptions.IgnoreCase)).Value
- Dim TimeUnit = TimeDic.ToList.First(Function(x) Regex.IsMatch(TimeUnitstr, "^" & x.Key & "$", RegexOptions.IgnoreCase)).Value
- Return FromValue(MemUnit, TimeUnit, Value)
- Else
- Return Nothing
- End If
- End Function
- Private Shared Function GetMemDic() As Dictionary(Of String, MemoryUnit)
- Dim Dic As New Dictionary(Of String, MemoryUnit)
- Dic.Add("(b|byte)", MemoryUnit.Byte)
- Dic.Add("(kibit|kibibit)", MemoryUnit.Kibibit)
- Dic.Add("(kib|kibibyte)", MemoryUnit.Kibibyte)
- Dic.Add("(kbit|kilobit)", MemoryUnit.Kilobit)
- Dic.Add("(kb|kbyte|kilobyte)", MemoryUnit.Kilobyte)
- Dic.Add("(mebit|mebibit)", MemoryUnit.Mebibit)
- Dic.Add("(meb|mebibyte)", MemoryUnit.Mebibyte)
- Dic.Add("(mbit|megabit)", MemoryUnit.Megabit)
- Dic.Add("(mb|megabyte)", MemoryUnit.Megabyte)
- Dic.Add("(gibit|gibibit)", MemoryUnit.Gibibit)
- Dic.Add("(gib|gibibyte)", MemoryUnit.Gibibyte)
- Dic.Add("(gbit|gigabit)", MemoryUnit.Gigabit)
- Dic.Add("(gb|gigabyte)", MemoryUnit.Gigabyte)
- Dic.Add("(tebbit|tebibit)", MemoryUnit.Tebibit)
- Dic.Add("(tebb|tebibyte)", MemoryUnit.Tebibyte)
- Dic.Add("(tbit|terabit)", MemoryUnit.Terabit)
- Dic.Add("(tb|terabyte)", MemoryUnit.Terabyte)
- Dic.Add("(pbibit|pebibit)", MemoryUnit.Pebibit)
- Dic.Add("(pbib|pebibyte)", MemoryUnit.Pebibyte)
- Dic.Add("(pbit|petabit)", MemoryUnit.Petabit)
- Dic.Add("(pb|petabyte)", MemoryUnit.Petabyte)
- Dic.Add("(exbibit)", MemoryUnit.Exbibit)
- Dic.Add("(exbib|exbibyte)", MemoryUnit.Exbibyte)
- Dic.Add("(ebit|exabit)", MemoryUnit.Exabit)
- Dic.Add("(eb|exab|exabyte)", MemoryUnit.Exabyte)
- Return Dic
- End Function
- Private Shared Function GetTimeDic() As Dictionary(Of String, TimeUnit)
- Dim Dic As New Dictionary(Of String, TimeUnit)
- Dic.Add("(s|second|sekunde)", TimeUnit.Second)
- Dic.Add("(min|minute)", TimeUnit.Minute)
- Dic.Add("(h|hour|stunde)", TimeUnit.Hour)
- Return Dic
- End Function
- ''' <summary>
- ''' Errechnet den zu erwartenden Speicherverarbeitung in einer bestimmten Zeit.
- ''' </summary>
- ''' <param name="time">Die Zeit, zu der der voraussichtliche Fortschritt erreicht sein wird.</param>
- Public Function CalculateProcessedMemory(time As TimeSpan) As Memory
- Return Me * time
- End Function
- ''' <summary>
- ''' Errechnet den zu erwartenden zeitlichen Aufwand, eine bestimmte Menge zu verarbeiten.
- ''' </summary>
- ''' <param name="memory">Die zu verabeitende Menge</param>
- Public Function CalculatePassedTime(memory As Memory) As TimeSpan
- Return memory / Me
- End Function
- ''' <summary>
- ''' Klont die aktuelle Instanz.
- ''' </summary>
- Public Function Clone() As Object Implements ICloneable.Clone
- Return New MemoryVelocity(VarMemory, VarTime)
- End Function
- ''' <summary>
- ''' Vergleicht zwei MemoryVelocity-Instanzen und gibt so die Sortierreihenfolge an.
- ''' </summary>
- ''' <param name="obj">Ein zu vergleichendes MemoryVelocity-Objekt</param>
- Public Function CompareTo(obj As Object) As Integer Implements IComparable.CompareTo
- Dim V = DirectCast(obj, MemoryVelocity)
- If Value(MemoryUnit.Byte, TimeUnit.Second) < V.Value(MemoryUnit.Byte, TimeUnit.Second) Then Return 1
- If Me = V Then Return 0
- Return - 1
- End Function
- #End Region
- #Region "Operators"
- Public Shared Operator +(v1 As MemoryVelocity, v2 As MemoryVelocity) As MemoryVelocity
- Return FromValue(MemoryUnit.Byte, TimeUnit.Second, v1.Value(MemoryUnit.Byte, TimeUnit.Second) + v2.Value(MemoryUnit.Byte, TimeUnit.Second))
- End Operator
- Public Shared Operator -(v1 As MemoryVelocity, v2 As MemoryVelocity) As MemoryVelocity
- Return v1 + - v2
- End Operator
- Public Shared Operator -(v As MemoryVelocity) As MemoryVelocity
- Return FromValue(MemoryUnit.Byte, TimeUnit.Second, - v.Value(MemoryUnit.Byte, TimeUnit.Second))
- End Operator
- Public Shared Operator =(v1 As MemoryVelocity, v2 As MemoryVelocity) As Boolean
- Return Math.Abs(v1.Value(MemoryUnit.Byte, TimeUnit.Second) - v2.Value(MemoryUnit.Byte, TimeUnit.Second)) < Double.Epsilon
- End Operator
- Public Shared Operator <>(v1 As MemoryVelocity, v2 As MemoryVelocity) As Boolean
- Return Not v1 = v2
- End Operator
- Public Shared Operator <(v1 As MemoryVelocity, v2 As MemoryVelocity) As Boolean
- Return v1.Value(MemoryUnit.Byte, TimeUnit.Second) < v2.Value(MemoryUnit.Byte, TimeUnit.Second)
- End Operator
- Public Shared Operator >(v1 As MemoryVelocity, v2 As MemoryVelocity) As Boolean
- Return v2 < v1
- End Operator
- Public Shared Operator <=(v1 As MemoryVelocity, v2 As MemoryVelocity) As Boolean
- Return v1.Value(MemoryUnit.Byte, TimeUnit.Second) <= v2.Value(MemoryUnit.Byte, TimeUnit.Second)
- End Operator
- Public Shared Operator >=(v1 As MemoryVelocity, v2 As MemoryVelocity) As Boolean
- Return v2 <= v1
- End Operator
- Public Shared Operator /(v1 As MemoryVelocity, v2 As MemoryVelocity) As Double
- Return v1.Value(MemoryUnit.Byte, TimeUnit.Second) / v2.Value(MemoryUnit.Byte, TimeUnit.Second)
- End Operator
- Public Shared Operator \(v1 As MemoryVelocity, v2 As MemoryVelocity) As Long
- Return CLng(v1.Value(MemoryUnit.Byte, TimeUnit.Second)) \ CLng(v2.Value(MemoryUnit.Byte, TimeUnit.Second))
- End Operator
- Public Shared Operator Mod(v1 As MemoryVelocity, v2 As MemoryVelocity) As MemoryVelocity
- Return FromValue(MemoryUnit.Byte, TimeUnit.Second, v1.Value(MemoryUnit.Byte, TimeUnit.Second) Mod v2.Value(MemoryUnit.Byte, TimeUnit.Second))
- End Operator
- Public Shared Widening Operator CType(v As MemoryVelocity) As Double
- Return v.Value(MemoryUnit.Byte, TimeUnit.Second)
- End Operator
- Public Shared Widening Operator CType(v As MemoryVelocity) As Long
- Return CLng(v.Value(MemoryUnit.Byte, TimeUnit.Second))
- End Operator
- Public Shared Widening Operator CType(d#) As MemoryVelocity
- Return FromValue(MemoryUnit.Byte, TimeUnit.Second, d)
- End Operator
- Public Shared Widening Operator CType(l&) As MemoryVelocity
- Return FromValue(MemoryUnit.Byte, TimeUnit.Second, l)
- End Operator
- Public Shared Operator +(v As MemoryVelocity, d#) As MemoryVelocity
- Return FromValue(MemoryUnit.Byte, TimeUnit.Second, v.Value(MemoryUnit.Byte, TimeUnit.Second) + d)
- End Operator
- Public Shared Operator -(v As MemoryVelocity, d#) As MemoryVelocity
- Return v + - d
- End Operator
- Public Shared Operator =(v As MemoryVelocity, d#) As Boolean
- Return Math.Abs(v.Value(MemoryUnit.Byte, TimeUnit.Second) - d) < Double.Epsilon
- End Operator
- Public Shared Operator <>(v As MemoryVelocity, d#) As Boolean
- Return Not v = d
- End Operator
- Public Shared Operator <(v As MemoryVelocity, d#) As Boolean
- Return v.Value(MemoryUnit.Byte, TimeUnit.Second) < d
- End Operator
- Public Shared Operator >(v As MemoryVelocity, d#) As Boolean
- Return v.Value(MemoryUnit.Byte, TimeUnit.Second) > d
- End Operator
- Public Shared Operator <=(v As MemoryVelocity, d#) As Boolean
- Return v.Value(MemoryUnit.Byte, TimeUnit.Second) <= d
- End Operator
- Public Shared Operator >=(v As MemoryVelocity, d#) As Boolean
- Return v.Value(MemoryUnit.Byte, TimeUnit.Second) >= d
- End Operator
- Public Shared Operator *(v As MemoryVelocity, d#) As MemoryVelocity
- Return FromValue(MemoryUnit.Byte, TimeUnit.Second, v.Value(MemoryUnit.Byte, TimeUnit.Second) * d)
- End Operator
- Public Shared Operator /(v As MemoryVelocity, d#) As MemoryVelocity
- Return FromValue(MemoryUnit.Byte, TimeUnit.Second, v.Value(MemoryUnit.Byte, TimeUnit.Second) / d)
- End Operator
- Public Shared Operator Mod(v As MemoryVelocity, d#) As MemoryVelocity
- Return FromValue(MemoryUnit.Byte, TimeUnit.Second, v.Value(MemoryUnit.Byte, TimeUnit.Second) Mod d)
- End Operator
- Public Shared Operator ^(v As MemoryVelocity, d#) As MemoryVelocity
- Return FromValue(MemoryUnit.Byte, TimeUnit.Second, v.Value(MemoryUnit.Byte, TimeUnit.Second) ^ d)
- End Operator
- Public Shared Operator *(v As MemoryVelocity, t As TimeSpan) As Memory
- Return New Memory(MemoryUnit.Byte, v.Value(MemoryUnit.Byte, TimeUnit.Second) * t.TotalSeconds)
- End Operator
- #End Region
- End Structure
- ''' <summary>
- ''' Gibt eine Auswahl an Zeit-Formaten.
- ''' </summary>
- Public Enum TimeUnit
- Second
- Minute
- Hour
- 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
Es reicht einfache Mathematik:
VB.NET-Quellcode
- 'Laden von Bytebasierenden Grössen:
- Dim DownloadSize = Memory.FromURI(New Uri("http://3.bp.blogspot.com/-Jk0gUehmM40/TmkwT_ZftFI/AAAAAAAAAB8/YyTcq9guuJQ/s1600/msdn.png"))
- Dim AdditionalSize As New Memory(MemoryUnit.Byte, 32.4)
- Dim FileSize = Memory.FromFile("." & My.Application.Info.AssemblyName)
- 'Kombinieren:
- Dim FinalSize = DownloadSize + AdditionalSize + FileSize
- 'Ausgeben:
- Console.WriteLine(DownloadSize.ToString)
- Console.WriteLine("+")
- Console.WriteLine(AdditionalSize.ToString)
- Console.WriteLine("+")
- Console.WriteLine(FileSize.ToString)
- Console.WriteLine("=")
- Console.WriteLine(FinalSize.ToString(MemoryUnit.Megabyte))
- Console.ReadLine()
- 'Oder auch das funktioniert:
- Dim StringToParse = "Die Datei ist ca. 128,43 Megabyte gross und wird mit einer Geschwindigkeit von 42.32 Mbit/s übertragen."
- Dim ExtractedMemory = Memory.Parse(StringToParse)
- Dim ExtractedMemoryVelocity = MemoryVelocity.Parse(StringToParse)
- Dim TimeOfCompletion = ExtractedMemory / ExtractedMemoryVelocity
Grüsse
Higlav
Dieser Beitrag wurde bereits 19 mal editiert, zuletzt von „Higlav“ ()