/
ByteMeasurements.cs
311 lines (264 loc) · 13.4 KB
/
ByteMeasurements.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// $Id$
/*
* Classless.Utilities.ByteMeasurements
* A Byte Size and Rate Formatting Utility for C#/.NET
* http://www.classless.net
*
* Version 1.0, 7/5/2004
*
* */
#region License
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Classless.Utilities.ByteMeasurements.
*
* The Initial Developer of the Original Code is Classless.net.
* Portions created by the Initial Developer are Copyright (C) 2004 the Initial
* Developer. All Rights Reserved.
*
* Contributor(s):
* Jason Simeone (jay@classless.net)
*
* ***** END LICENSE BLOCK ***** */
#endregion
using System;
namespace Classless.Utilities {
/// <summary>Represents a unit system that bytes can be measured in.</summary>
public enum MeasurementSystem {
/// <summary>International System of Units (aka Metric System).</summary>
SI,
/// <summary>International Electrotechnical Commission Binary Units (IEC 60027-2).</summary>
Binary
}
/// <summary>Represents the base units of the SI system.</summary>
public enum UnitSI : long {
/// <summary>1 Byte.</summary>
Byte = 1,
/// <summary>1 Kiloyte (10^3).</summary>
Kilobyte = 1000,
/// <summary>1 Megabyte (10^6).</summary>
Megabyte = 1000000,
/// <summary>1 Gigabyte (10^9).</summary>
Gigabyte = 1000000000,
/// <summary>1 Terabyte (10^12).</summary>
Terabyte = 1000000000000,
/// <summary>1 Petabyte (10^15).</summary>
Petabyte = 1000000000000000,
/// <summary>1 Exabyte (10^18).</summary>
Exabyte = 1000000000000000000
}
/// <summary>Represents the base units of the standard IEC system.</summary>
public enum UnitBinary : long {
/// <summary>1 Byte.</summary>
Byte = 1,
/// <summary>1 Kibibyte (2^10).</summary>
Kibibyte = 1024,
/// <summary>1 Mebibyte (2^20).</summary>
Mebibyte = 1048576,
/// <summary>1 Gibibyte (2^30).</summary>
Gibibyte = 1073741824,
/// <summary>1 Tebibyte (2^40).</summary>
Tebibyte = 1099511627776,
/// <summary>1 Pebibyte (2^50).</summary>
Pebibyte = 1125899906842624,
/// <summary>1 Exbibyte (2^60).</summary>
Exbibyte = 1152921504606846976
}
/// <summary>A class for measuring byte quantities.</summary>
public class ByteMeasurements {
private long numBytes = 0;
/// <summary>Gets or sets the number of bytes the measuring is performed against.</summary>
public long NumBytes {
get { return numBytes; }
set { numBytes = value; }
}
/// <summary>Initializes an instance of ByteMeasurements.</summary>
public ByteMeasurements() {}
/// <summary>Initializes an instance of ByteMeasurements.</summary>
/// <param name="numBytes">The number of bytes to measure against.</param>
public ByteMeasurements(long numBytes) { this.NumBytes = numBytes; }
/// <summary>Initializes an instance of ByteMeasurements.</summary>
/// <param name="numBytes">The number of bytes to measure against.</param>
public ByteMeasurements(float numBytes) { this.NumBytes = (long)numBytes; }
/// <summary>Initializes an instance of ByteMeasurements.</summary>
/// <param name="numBytes">The number of bytes to measure against.</param>
public ByteMeasurements(double numBytes) { this.NumBytes = (long)numBytes; }
/// <summary>Reduce the number of bytes to its highest measureable unit.</summary>
/// <returns>A formatted string containing the number of bytes and unit name.</returns>
/// <remarks>Defaults to the non-abbreviated Binary system with 2 digits of precision.</remarks>
public string Units() { return Units(MeasurementSystem.Binary, false, 2); }
/// <summary>Reduce the number of bytes to its highest measureable unit.</summary>
/// <param name="type">The system of measurement to use.</param>
/// <returns>A formatted string containing the number of bytes and unit name.</returns>
/// <remarks>Defaults to the non-abbreviated system with 2 digits of precision.</remarks>
public string Units(MeasurementSystem type) { return Units(type, false, 2); }
/// <summary>Reduce the number of bytes to its highest measureable unit.</summary>
/// <param name="type">The system of measurement to use.</param>
/// <param name="abbreviate">Whether or not to abbreviate the unit name.</param>
/// <returns>A formatted string containing the number of bytes and unit name.</returns>
/// <remarks>Defaults to 2 digits of precision.</remarks>
public string Units(MeasurementSystem type, bool abbreviate) { return Units(type, abbreviate, 2); }
/// <summary>Reduce the number of bytes to its highest measureable unit.</summary>
/// <param name="type">The system of measurement to use.</param>
/// <param name="abbreviate">Whether or not to abbreviate the unit name.</param>
/// <param name="precision">The number of digits after the decimal point to show.</param>
/// <returns>A formatted string containing the number of bytes and unit name.</returns>
public string Units(MeasurementSystem type, bool abbreviate, int precision) {
string temp = "n/a";
try {
temp = units(NumBytes, type, abbreviate, precision);
} catch (ArithmeticException) {}
return temp;
}
/// <summary>Reduce the number of bytes to its highest measureable unit per second.</summary>
/// <param name="seconds">The number of seconds the amount is measured against.</param>
/// <returns>A formatted string containing the number of bytes and unit name per second.</returns>
public string Rate(double seconds) { return Rate(seconds, MeasurementSystem.Binary, false, 2); }
/// <summary>Reduce the number of bytes to its highest measureable unit per second.</summary>
/// <param name="seconds">The number of seconds the amount is measured against.</param>
/// <param name="type">The system of measurement to use.</param>
/// <returns>A formatted string containing the number of bytes and unit name per second.</returns>
public string Rate(double seconds, MeasurementSystem type) { return Rate(seconds, type, false, 2); }
/// <summary>Reduce the number of bytes to its highest measureable unit per second.</summary>
/// <param name="seconds">The number of seconds the amount is measured against.</param>
/// <param name="type">The system of measurement to use.</param>
/// <param name="abbreviate">Whether or not to abbreviate the unit names.</param>
/// <returns>A formatted string containing the number of bytes and unit name per second.</returns>
public string Rate(double seconds, MeasurementSystem type, bool abbreviate) { return Rate(seconds, type, abbreviate, 2); }
/// <summary>Reduce the number of bytes to its highest measureable unit per second.</summary>
/// <param name="seconds">The number of seconds the amount is measured against.</param>
/// <param name="type">The system of measurement to use.</param>
/// <param name="abbreviate">Whether or not to abbreviate the unit names.</param>
/// <param name="precision">The number of digits after the decimal point to show.</param>
/// <returns>A formatted string containing the number of bytes and unit name per second.</returns>
public string Rate(double seconds, MeasurementSystem type, bool abbreviate, int precision) {
string temp = "n/a";
try {
temp = units((NumBytes / seconds), type, abbreviate, precision);
if (abbreviate) {
temp += "/s";
} else {
temp += "/second";
}
} catch (ArithmeticException) {}
return temp;
}
/// <summary>Get the human-readable name for a specified unit.</summary>
/// <param name="unit">The unit to get the name for.</param>
/// <returns>The human-readable name for the specified unit.</returns>
/// <remarks>Defaults to the non-abbreviated name.</remarks>
static public string UnitName(UnitSI unit) { return UnitName(unit, false); }
/// <summary>Get the human-readable name for a specified unit.</summary>
/// <param name="unit">The unit to get the name for.</param>
/// <param name="abbreviate">Whether or not to abbreviate the name.</param>
/// <returns>The human-readable name for the specified unit.</returns>
static public string UnitName(UnitSI unit, bool abbreviate) {
string name;
if (abbreviate) {
if (unit == UnitSI.Byte) {
name = "B";
} else {
name = unit.ToString().Substring(0, 1) + "B";
}
} else {
name = unit.ToString();
}
return name;
}
/// <summary>Get the human-readable name for a specified unit.</summary>
/// <param name="unit">The unit to get the name for.</param>
/// <returns>The human-readable name for the specified unit.</returns>
/// <remarks>Defaults to the non-abbreviated name.</remarks>
static public string UnitName(UnitBinary unit) { return UnitName(unit, false); }
/// <summary>Get the human-readable name for a specified unit.</summary>
/// <param name="unit">The unit to get the name for.</param>
/// <param name="abbreviate">Whether or not to abbreviate the name.</param>
/// <returns>The human-readable name for the specified unit.</returns>
static public string UnitName(UnitBinary unit, bool abbreviate) {
string name;
if (abbreviate) {
if (unit == UnitBinary.Byte) {
name = "B";
} else {
name = unit.ToString().Substring(0, 1) + "iB";
}
} else {
name = unit.ToString();
}
return name;
}
// Internal function that does most of the math.
private string units(double bytes, MeasurementSystem type, bool abbreviate, int precision) {
string temp = string.Empty;
if (type == MeasurementSystem.SI) {
if (bytes < (long)UnitSI.Kilobyte) {
if (Math.Floor(bytes) == bytes) {
temp = bytes.ToString(System.Globalization.CultureInfo.InvariantCulture.NumberFormat);
} else {
temp = string.Format("{0:F" + precision.ToString() + "}", bytes);
}
temp += " " + UnitName(UnitSI.Byte, abbreviate);
} else if (bytes < (long)UnitSI.Megabyte) {
temp = string.Format("{0:F" + precision.ToString() + "}", (bytes / (double)UnitSI.Kilobyte)) + " " + UnitName(UnitSI.Kilobyte, abbreviate);
} else if (bytes < (long)UnitSI.Gigabyte) {
temp = string.Format("{0:F" + precision.ToString() + "}", (bytes / (double)UnitSI.Megabyte)) + " " + UnitName(UnitSI.Megabyte, abbreviate);
} else if (bytes < (long)UnitSI.Terabyte) {
temp = string.Format("{0:F" + precision.ToString() + "}", (bytes / (double)UnitSI.Gigabyte)) + " " + UnitName(UnitSI.Gigabyte, abbreviate);
} else if (bytes < (long)UnitSI.Petabyte) {
temp = string.Format("{0:F" + precision.ToString() + "}", (bytes / (double)UnitSI.Terabyte)) + " " + UnitName(UnitSI.Terabyte, abbreviate);
} else if (bytes < (long)UnitSI.Exabyte) {
temp = string.Format("{0:F" + precision.ToString() + "}", (bytes / (double)UnitSI.Petabyte)) + " " + UnitName(UnitSI.Petabyte, abbreviate);
} else {
// Make sure we have a "real" number before returning it.
double check = (bytes / (double)UnitSI.Exabyte);
if ((double.IsInfinity(check)) || (double.IsNaN(check)) || (double.IsNegativeInfinity(check)) || (double.IsPositiveInfinity(check))) {
throw new ArithmeticException("Invalid result.");
} else {
temp = string.Format("{0:F" + precision.ToString() + "}", check) + " " + UnitName(UnitSI.Exabyte, abbreviate);
}
}
} else if (type == MeasurementSystem.Binary) {
if (bytes < (long)UnitBinary.Kibibyte) {
if (Math.Floor(bytes) == bytes) {
temp = bytes.ToString(System.Globalization.CultureInfo.InvariantCulture.NumberFormat);
} else {
temp = string.Format("{0:F" + precision.ToString() + "}", bytes);
}
temp += " " + UnitName(UnitBinary.Byte, abbreviate);
} else if (bytes < (long)UnitBinary.Mebibyte) {
temp = string.Format("{0:F" + precision.ToString() + "}", (bytes / (double)UnitBinary.Kibibyte)) + " " + UnitName(UnitBinary.Kibibyte, abbreviate);
} else if (bytes < (long)UnitBinary.Gibibyte) {
temp = string.Format("{0:F" + precision.ToString() + "}", (bytes / (double)UnitBinary.Mebibyte)) + " " + UnitName(UnitBinary.Mebibyte, abbreviate);
} else if (bytes < (long)UnitBinary.Tebibyte) {
temp = string.Format("{0:F" + precision.ToString() + "}", (bytes / (double)UnitBinary.Gibibyte)) + " " + UnitName(UnitBinary.Gibibyte, abbreviate);
} else if (bytes < (long)UnitBinary.Pebibyte) {
temp = string.Format("{0:F" + precision.ToString() + "}", (bytes / (double)UnitBinary.Tebibyte)) + " " + UnitName(UnitBinary.Tebibyte, abbreviate);
} else if (bytes < (long)UnitBinary.Exbibyte) {
temp = string.Format("{0:F" + precision.ToString() + "}", (bytes / (double)UnitBinary.Pebibyte)) + " " + UnitName(UnitBinary.Pebibyte, abbreviate);
} else {
// Make sure we have a "real" number before returning it.
double check = (bytes / (double)UnitBinary.Exbibyte);
if ((double.IsInfinity(check)) || (double.IsNaN(check)) || (double.IsNegativeInfinity(check)) || (double.IsPositiveInfinity(check))) {
throw new ArithmeticException("Invalid result.");
} else {
temp = string.Format("{0:F" + precision.ToString() + "}", check) + " " + UnitName(UnitBinary.Exbibyte, abbreviate);
}
}
} else {
throw new System.NotSupportedException(type.ToString() + " is not supported.");
}
return temp;
}
}
}