Is there a Java enum for filesize units that's equivalent to java.util.concurrent.TimeUnit?
Asked Answered
E

5

48

I've always liked the readability of assignments that used TimeUnit like so:

long timePeriodInMillis = TimeUnit.MINUTES.toMillis( 53 );

Over something like:

long timePeriodInMillis = 53 * 60 * 1000;

Is there an equivalent enum I can use for filesize units? Something like

long maxBits = FilesizeUnit.MEGABYTES.toBits( 11 );
Edie answered 13/12, 2011 at 20:40 Comment(0)
C
15

I've done exactly sth like this half a year ago just for fun inspired by TimeUnit enum.

I will upload it at GitHub tomorrow. It contains two enums: BitUnit and ByteUnit. Both also support converting between each other. ByteUnit has support for 2-based Prefixes as well for 10-based Prefixes. (Enum constants and methods in ByteUnit use IEC 80000-13 terminology for the prefixes.)

Usage looks like this:

System.out.println(BitUnit.KBIT.toKiB(16000));

System.out.println(ByteUnit.GIB.toMB(1));
System.out.println(ByteUnit.GIB.toMiB(1));
System.out.println(ByteUnit.GB.toMB(1));
System.out.println(ByteUnit.GB.toMiB(1));

... and prints out:

1953.125

1073.741824
1024.0
1000.0
953.67431640625

For convertion methods between Bits and Bytes you've overloaded methods to specify a word size other than 8 bits per byte. Hope you can wait until tomorrow.


EDIT

Here you are: https://github.com/fabian-barney/Utils

Do not blame me for the directory structure - I am still not familar with Git yet. :)

Cuddle answered 13/12, 2011 at 21:26 Comment(4)
I just created an account. Give me a second to install and configure eGit properly. It's my first time using Git - just having experience with svn and cvs. :)Cuddle
@FabianBarney It would be most awesome if something like this could make its way into the Guava libraries. Do you intend to share your code with them or with some other parties? Your enums would fit perfectly into the other classes contained by Guava (I think).Heelandtoe
@Kohányi Róbert I decided to put it under Apache License 2 like Guava and Apache Commons, too. So there is really no problem when they decide to include it. But there is some work to be done to make this enums mature. Due to its current implementation it is less accurate than it could be for large numbers pushing against borders of double type. But making it more accurate in these spheres would blow code up so I decided for me to stick with a better maintainable version.Cuddle
@Kohányi Róbert I provided it to guava as Enhancement issue - you may want to vote for it: code.google.com/p/guava-libraries/issues/detail?id=827Cuddle
P
23

Apache commons provides constants in the FileUtils class, like

  • FileUtils.ONE_MB
  • FileUtils.ONE_GB
  • ...

source : https://commons.apache.org/proper/commons-io/javadocs/api-2.2/org/apache/commons/io/FileUtils.html

Their definition of a KB is based on 1024 bytes, (a MB is KB², and so long).

Pentadactyl answered 5/7, 2018 at 8:31 Comment(0)
C
15

I've done exactly sth like this half a year ago just for fun inspired by TimeUnit enum.

I will upload it at GitHub tomorrow. It contains two enums: BitUnit and ByteUnit. Both also support converting between each other. ByteUnit has support for 2-based Prefixes as well for 10-based Prefixes. (Enum constants and methods in ByteUnit use IEC 80000-13 terminology for the prefixes.)

Usage looks like this:

System.out.println(BitUnit.KBIT.toKiB(16000));

System.out.println(ByteUnit.GIB.toMB(1));
System.out.println(ByteUnit.GIB.toMiB(1));
System.out.println(ByteUnit.GB.toMB(1));
System.out.println(ByteUnit.GB.toMiB(1));

... and prints out:

1953.125

1073.741824
1024.0
1000.0
953.67431640625

For convertion methods between Bits and Bytes you've overloaded methods to specify a word size other than 8 bits per byte. Hope you can wait until tomorrow.


EDIT

Here you are: https://github.com/fabian-barney/Utils

Do not blame me for the directory structure - I am still not familar with Git yet. :)

Cuddle answered 13/12, 2011 at 21:26 Comment(4)
I just created an account. Give me a second to install and configure eGit properly. It's my first time using Git - just having experience with svn and cvs. :)Cuddle
@FabianBarney It would be most awesome if something like this could make its way into the Guava libraries. Do you intend to share your code with them or with some other parties? Your enums would fit perfectly into the other classes contained by Guava (I think).Heelandtoe
@Kohányi Róbert I decided to put it under Apache License 2 like Guava and Apache Commons, too. So there is really no problem when they decide to include it. But there is some work to be done to make this enums mature. Due to its current implementation it is less accurate than it could be for large numbers pushing against borders of double type. But making it more accurate in these spheres would blow code up so I decided for me to stick with a better maintainable version.Cuddle
@Kohányi Róbert I provided it to guava as Enhancement issue - you may want to vote for it: code.google.com/p/guava-libraries/issues/detail?id=827Cuddle
H
8

If you're using Spring, there's already a utility class for data unit conversion: org.springframework.util.unit.DataSize. This class models data size in terms of bytes and is immutable and thread-safe.

Usage sample:

Long bytesIn5Mb = DataSize.ofMegabytes(5).toBytes(); // 5MB to bytes
Long kbIn1Tb = DataSize.ofTerabytes(1).toKilobytes(); // 1TB to KB
Higgledypiggledy answered 8/5, 2021 at 16:19 Comment(0)
H
0

No, but you can convert yourself easily with:

long maxBits = 11L * 1024 * 1024 * 8;

or

long maxBits = 11L * 1000 * 1000 * 8;

depending on your definition of a megabyte.

Harlequinade answered 13/12, 2011 at 20:44 Comment(3)
This will do integer arithmetic and overflow if above 1 gigabyte. I'd recommend making at least one of those constants have a trailing L or a cast.Enhance
Sure, but explicit multiplication is what I'm trying to avoid.Edie
Good point, fixed above. Doesn't matter with these constants, but would above 255 Mb. The explicit types now could avert trouble later.Harlequinade
E
-3

Looking at the Java documentation for the Comparable interface which TimeUnit implements there is no such thing. See here: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Comparable.html

With that link however it shouldn't be hard for you to derive your own working version to use within your project(s).

Ejection answered 13/12, 2011 at 20:45 Comment(2)
How the Comparable ever related to the question?Phenolic
@Alexei Osipov: If there was an equivalent enum it would implement Comparable (if it had actually the same structure as TimeUnit). I think this is a clever way to search the API for the (non-)existence of such an enum. It's sad to see downvotes to the sole post that tries to answer the question instead of providing an alternative.Atharvaveda

© 2022 - 2024 — McMap. All rights reserved.