-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathFileSize.cs
236 lines (211 loc) · 6.96 KB
/
FileSize.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
using System;
using System.Collections.Generic;
namespace PathLib
{
/// <summary>
/// Represents the size of a particular file.
/// </summary>
public class FileSize
{
private readonly long _bytes;
private readonly FileSizeScale _scale;
private readonly Dictionary<FileSizeUnits, ConversionData> _conversionTable;
/// <summary>
/// Represents the size of a particular file.
/// </summary>
/// <param name="bytes"></param>
public FileSize(long bytes)
: this(bytes, FileSizeScale.Binary)
{
}
/// <summary>
/// Represents the size of a particular file.
/// </summary>
/// <param name="bytes"></param>
/// <param name="scale"></param>
private FileSize(long bytes, FileSizeScale scale)
{
_bytes = bytes;
_scale = scale;
_conversionTable = scale == FileSizeScale.Binary
? BinaryConversionTable
: SiConversionTable;
}
/// <summary>
/// Returns the file size represented in multiples
/// of 1000.
/// </summary>
/// <returns></returns>
// ReSharper disable once InconsistentNaming
public FileSize ToSIUnits()
{
return _scale == FileSizeScale.SI
? this
: new FileSize(_bytes, FileSizeScale.SI);
}
public override bool Equals(object obj)
{
var other = obj as FileSize;
if (other == null) return false;
return _bytes == other._bytes;
}
public override int GetHashCode()
{
return _bytes.GetHashCode();
}
/// <summary>
/// Size in bytes.
/// </summary>
public long Bytes
{
get { return _bytes; }
}
/// <summary>
/// Size in bytes.
/// </summary>
public long Kilobytes
{
get { return GetAs(FileSizeUnits.Kilobyte); }
}
/// <summary>
/// Size in bytes.
/// </summary>
public long Megabytes
{
get { return GetAs(FileSizeUnits.Megabyte); }
}
/// <summary>
/// Size in bytes.
/// </summary>
public long Gigabytes
{
get { return GetAs(FileSizeUnits.Gigabyte); }
}
/// <summary>
/// Size in bytes.
/// </summary>
public long Terabytes
{
get { return GetAs(FileSizeUnits.Terabyte); }
}
private long GetAs(FileSizeUnits units)
{
return _bytes/_conversionTable[units].Multiplier;
}
private double GetAsFraction(FileSizeUnits units)
{
return 1.0 * _bytes / _conversionTable[units].Multiplier;
}
/// <summary>
/// Convert to a string using the closest "round" units.
/// </summary>
/// <returns></returns>
public override string ToString()
{
const double lowerTolerance = 0.85;
var units = new[] { FileSizeUnits.Byte, FileSizeUnits.Kilobyte, FileSizeUnits.Megabyte };
double scaledValue = _bytes;
var scaledUnit = FileSizeUnits.Byte;
for (var i = 0; i < units.Length; i++)
{
var unit = units[i];
var value = GetAsFraction(unit);
if (value < lowerTolerance)
{
break;
}
scaledValue = value;
scaledUnit = unit;
}
return String.Format("{0:##.##}{1}", scaledValue, _conversionTable[scaledUnit].BasicPrefix);
}
/// <summary>
/// Returns the string using the given units.
/// </summary>
/// <param name="units"></param>
/// <param name="useUnambiguousPrefixes">
/// Use the new IEC prefixes for binary units (kibibytes-KiB,
/// mebibytes-MiB, etc.) instead of the more common units.
/// </param>
/// <returns></returns>
public string ToString(FileSizeUnits units, bool useUnambiguousPrefixes = false)
{
return GetAs(units) + (useUnambiguousPrefixes
? _conversionTable[units].UnambiguousPrefix
: _conversionTable[units].BasicPrefix);
}
/// <summary>
/// The scale used for prefixes.
/// </summary>
private enum FileSizeScale
{
/// <summary>
/// Use the binary prefixes, where each
/// prefix represents multiples of 1024.
/// </summary>
Binary = 0,
/// <summary>
/// Use the binary prefixes, where each
/// prefix represents multiples of 1000.
/// </summary>
// ReSharper disable once InconsistentNaming
SI
}
private static readonly Dictionary<FileSizeUnits, ConversionData> BinaryConversionTable =
new Dictionary<FileSizeUnits, ConversionData>
{
{FileSizeUnits.Byte, new ConversionData(1L, "B", "B")},
{FileSizeUnits.Kilobyte, new ConversionData(1000L, "KB", "KiB")},
{FileSizeUnits.Megabyte, new ConversionData(1000000L, "MB", "MiB")},
{FileSizeUnits.Gigabyte, new ConversionData(1000000000L, "GB", "GiB")},
{FileSizeUnits.Terabyte, new ConversionData(1000000000000L, "TB", "TiB")}
};
private static readonly Dictionary<FileSizeUnits, ConversionData> SiConversionTable =
new Dictionary<FileSizeUnits, ConversionData>
{
{FileSizeUnits.Byte, new ConversionData(1L, "B", "B")},
{FileSizeUnits.Kilobyte, new ConversionData(1024L, "KB", "KB")},
{FileSizeUnits.Megabyte, new ConversionData(1048576L, "MB", "MB")},
{FileSizeUnits.Gigabyte, new ConversionData(1073741824L, "GB", "GB")},
{FileSizeUnits.Terabyte, new ConversionData(1099511627776L, "TB", "TB")}
};
private class ConversionData
{
public long Multiplier { get; private set; }
public string BasicPrefix { get; private set; }
public string UnambiguousPrefix { get; private set; }
public ConversionData(long mult, string basic, string unamb)
{
Multiplier = mult;
BasicPrefix = basic;
UnambiguousPrefix = unamb;
}
}
}
/// <summary>
/// Units of file size.
/// </summary>
public enum FileSizeUnits
{
/// <summary>
///
/// </summary>
Byte,
/// <summary>
///
/// </summary>
Kilobyte,
/// <summary>
///
/// </summary>
Megabyte,
/// <summary>
///
/// </summary>
Gigabyte,
/// <summary>
///
/// </summary>
Terabyte
}
}