-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ByteUnit.java
265 lines (231 loc) · 7.46 KB
/
ByteUnit.java
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
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.io;
import java.util.Arrays;
import java.util.Locale;
import java.util.stream.Stream;
import org.neo4j.helpers.collection.Pair;
import static java.lang.String.format;
/**
* A ByteUnit is a unit for a quantity of bytes.
* <p>
* The unit knows how to convert between other units in its class, so you for instance can turn a number of KiBs into
* an accurate quantity of bytes. Precision can be lost when converting smaller units into larger units, because of
* integer division.
* <p>
* These units all follow the EIC (International Electrotechnical Commission) standard, which uses a multiplier of
* 1.024. This system is also known as the binary system, and has been accepted as part of the International System of
* Quantities. It is therefor the recommended choice when communicating quantities of information, and the only one
* available in this implementation.
*/
public enum ByteUnit
{
/*
XXX Future notes: This class can potentially replace some of the functionality in org.neo4j.helpers.Format.
*/
Byte( 0, "B" ),
KibiByte( 1, "KiB", "KB", "K", "kB", "kb", "k" ),
MebiByte( 2, "MiB", "MB", "M", "mB", "mb", "m" ),
GibiByte( 3, "GiB", "GB", "G", "gB", "gb", "g" ),
TebiByte( 4, "TiB", "TB" ),
PebiByte( 5, "PiB", "PB" ),
ExbiByte( 6, "EiB", "EB" );
public static final long ONE_KIBI_BYTE = ByteUnit.KibiByte.toBytes( 1 );
public static final long ONE_MEBI_BYTE = ByteUnit.MebiByte.toBytes( 1 );
public static final long ONE_GIBI_BYTE = ByteUnit.GibiByte.toBytes( 1 );
private static final long EIC_MULTIPLIER = 1024;
private final long factor;
private final String shortName;
private final String[] names;
ByteUnit( long power, String... names )
{
this.factor = factorFromPower( power );
this.shortName = names[0];
this.names = names;
}
/**
* Compute the increment factor from the given power.
* <p>
* Giving zero always produces 1. Giving 1 will produce 1000 or 1024, for SI and EIC respectively, and so on.
*/
private long factorFromPower( long power )
{
if ( power == 0 )
{
return 1;
}
long product = EIC_MULTIPLIER;
for ( int i = 0; i < power - 1; i++ )
{
product = product * EIC_MULTIPLIER;
}
return product;
}
/**
* Get the short or abbreviated name of this unit, e.g. KiB or MiB.
*
* @return The short unit name.
*/
public String abbreviation()
{
return shortName;
}
/**
* Convert the given value of this unit, to a value in the given unit.
*
* @param value The value to convert from this unit.
* @param toUnit The unit of the resulting value.
* @return The value in the given result unit.
*/
public long convert( long value, ByteUnit toUnit )
{
return toBytes( value ) / toUnit.factor;
}
public long toBytes( long value )
{
return factor * value;
}
public long toKibiBytes( long value )
{
return convert( value, KibiByte );
}
public long toMebiBytes( long value )
{
return convert( value, MebiByte );
}
public long toGibiBytes( long value )
{
return convert( value, GibiByte );
}
public long toTebiBytes( long value )
{
return convert( value, TebiByte );
}
public long toPebiBytes( long value )
{
return convert( value, PebiByte );
}
public long toExbiBytes( long value )
{
return convert( value, ExbiByte );
}
public static long bytes( long bytes )
{
return bytes;
}
public static long kibiBytes( long kibibytes )
{
return KibiByte.toBytes( kibibytes );
}
public static long mebiBytes( long mebibytes )
{
return MebiByte.toBytes( mebibytes );
}
public static long gibiBytes( long gibibytes )
{
return GibiByte.toBytes( gibibytes );
}
public static long tebiBytes( long tebibytes )
{
return TebiByte.toBytes( tebibytes );
}
public static long pebiBytes( long pebibytes )
{
return PebiByte.toBytes( pebibytes );
}
public static long exbiBytes( long exbibytes )
{
return ExbiByte.toBytes( exbibytes );
}
public static String bytesToString( long bytes )
{
if ( bytes > ONE_GIBI_BYTE )
{
return format( Locale.ROOT, "%.4g%s", bytes / (double) ONE_GIBI_BYTE, GibiByte.shortName );
}
else if ( bytes > ONE_MEBI_BYTE )
{
return format( Locale.ROOT, "%.4g%s", bytes / (double) ONE_MEBI_BYTE, MebiByte.shortName );
}
else if ( bytes > ONE_KIBI_BYTE )
{
return format( Locale.ROOT, "%.4g%s", bytes / (double) ONE_KIBI_BYTE, KibiByte.shortName );
}
else
{
return bytes + Byte.shortName;
}
}
public static long parse( String text )
{
long result = 0;
int len = text.length();
int unitCharacter = 0;
int digitCharacters = 0;
Stream<Pair<String,ByteUnit>> unitsStream = listUnits();
for ( int i = 0; i < len; i++ )
{
char ch = text.charAt( i );
int digit = Character.digit( ch, 10 );
if ( digit != -1 )
{
if ( unitCharacter != 0 )
{
throw invalidFormat( text );
}
if ( result != 0 )
{
result *= 10;
}
result += digit;
digitCharacters++;
}
else if ( !Character.isWhitespace( ch ) )
{
int idx = unitCharacter;
unitsStream = unitsStream.filter( p -> p.first().length() > idx && p.first().charAt( idx ) == ch );
unitCharacter++;
}
}
if ( digitCharacters == 0 )
{
throw invalidFormat( text );
}
if ( unitCharacter > 0 )
{
ByteUnit byteUnit = unitsStream.map( Pair::other ).findFirst().orElse( null );
if ( byteUnit == null )
{
throw invalidFormat( text );
}
result = byteUnit.toBytes( result );
}
return result;
}
private static IllegalArgumentException invalidFormat( String text )
{
return new IllegalArgumentException( "Invalid number format: '" + text + "'" );
}
private static Stream<Pair<String,ByteUnit>> listUnits()
{
return Arrays.stream( values() ).flatMap(
b -> Stream.of( b.names ).map( n -> Pair.of( n, b ) ) );
}
}