{"id":2067,"date":"2025-11-24T10:00:20","date_gmt":"2025-11-24T15:00:20","guid":{"rendered":"http:\/\/www.mymiller.name\/wordpress\/?p=2067"},"modified":"2025-11-24T10:00:20","modified_gmt":"2025-11-24T15:00:20","slug":"unitofmemory","status":"publish","type":"post","link":"https:\/\/www.mymiller.name\/wordpress\/java_extra\/unitofmemory\/","title":{"rendered":"UnitOfMemory &#8211; Measure and convert you bytes"},"content":{"rendered":"<p>Not very often do I need to take a number of bytes and convert them into the most appropriate measurement. &nbsp;This came up recently on a project and I thought it would be nice to just create a class to handle this for me. &nbsp;So I created UnitOfMemory as enum that switches between Byte, KiloByte, MegaByte, GigaByte, TeraByte, and PetraByte. &nbsp;This works very nicely to be able to select the appropriate unit to measure with the number of bytes I have, and then to convert.<\/p>\n<p>This is just a simple Java Enum that uses a enum constructor to create an enum type based on the unit of measure of memory.<\/p>\n\n\n<pre class=\"wp-block-code\"><code>package name.mymiller.extensions.lang;\n\nimport java.math.BigDecimal;\nimport java.math.BigInteger;\n\n\/**\n * Measure UnitOfTime based on milliseconds\n *\n * @author jmiller\n *\/\npublic enum UnitOfTime {\n    \/\/@formatter:off\n    \/**\n     * Millieseconds\n     *\/\n    Millisecond(\"ms\", new BigInteger(\"1\")),\n    \/**\n     * Seconds\n     *\/\n    Second(\"secs\", new BigInteger(\"1000\")),\n    \/**\n     * Minutes\n     *\/\n    Minute(\"mins\", new BigInteger(\"60000\")),\n    \/**\n     * Hours\n     *\/\n    Hour(\"hours\", new BigInteger(\"3600000\")),\n    \/**\n     * Days\n     *\/\n    Day(\"days\", new BigInteger(\"86400000\")),\n    \/**\n     * Weeks\n     *\/\n    Week(\"weeks\", new BigInteger(\"604800000\")),\n    \/**\n     * Years\n     *\/\n    Year(\"years\", new BigInteger(\"31536000000\")),\n    \/**\n     * Millennium\n     *\/\n    Millennium(\"millennium\", new BigInteger(\"31536000000000\"));\n\n    \/\/@formatter:on\n\n    \/**\n     * Label to use on this UnitOfTime\n     *\/\n    private String label = null;\n    \/**\n     * Total Millieseonds\n     *\/\n    private BigInteger milliseconds = null;\n\n    \/**\n     * Constructor for this UnitOfTime\n     *\n     * @param label        Label to use\n     * @param milliseconds Number of milliseconds to make this unit of time.\n     *\/\n    UnitOfTime(String label, BigInteger milliseconds) {\n        this.label = label;\n        this.milliseconds = milliseconds;\n    }\n\n    \/**\n     * Convert a number of milliseconds into a Years Days Hours Minutes Seconds\n     * Milliseconds format\n     *\n     * @param milliseconds Number of milliseconds to convert\n     * @return String containing the conversion\n     *\/\n    public static String convert(BigInteger milliseconds) {\n        BigInteger working = milliseconds;\n        final StringBuilder builder = new StringBuilder();\n\n        final BigInteger years = UnitOfTime.Year.getMilliseconds().divide(working);\n        working = working.subtract(years.multiply(UnitOfTime.Year.getMilliseconds()));\n\n        final BigInteger days = UnitOfTime.Day.getMilliseconds().divide(working);\n        working = working.subtract(days.multiply(UnitOfTime.Day.getMilliseconds()));\n\n        final BigInteger hours = UnitOfTime.Hour.getMilliseconds().divide(working);\n        working = working.subtract(hours.multiply(UnitOfTime.Hour.getMilliseconds()));\n\n        final BigInteger minutes = UnitOfTime.Minute.getMilliseconds().divide(working);\n        working = working.subtract(minutes.multiply(UnitOfTime.Minute.getMilliseconds()));\n\n        final BigInteger seconds = UnitOfTime.Second.getMilliseconds().divide(working);\n        working = working.subtract(seconds.multiply(UnitOfTime.Second.getMilliseconds()));\n\n        if (years.longValue() != 0L) {\n            builder.append(years);\n            builder.append(\" years \");\n        }\n        if (days.longValue() != 0L) {\n            builder.append(days);\n            builder.append(\" days \");\n        }\n        if (hours.longValue() != 0L) {\n            builder.append(hours);\n            builder.append(\" hours \");\n        }\n        if (minutes.longValue() != 0L) {\n            builder.append(minutes);\n            builder.append(\" minutes \");\n        }\n        if (seconds.longValue() != 0L) {\n            builder.append(seconds);\n            builder.append(\" seconds \");\n        }\n        if (working.longValue() != 0L) {\n            builder.append(working);\n            builder.append(\" milliseconds \");\n        }\n\n        return builder.toString();\n    }\n\n    \/**\n     * Determine what unit to use based on the number of milliseconds\n     *\n     * @param milliseconds Total number of milliseconds\n     * @return UnitOfTime you should use.\n     *\/\n    public static UnitOfTime useUnit(BigInteger milliseconds) {\n        if (milliseconds.compareTo(UnitOfTime.Millennium.getMilliseconds()) >= 0) {\n            return UnitOfTime.Millennium;\n        } else if (milliseconds.compareTo(UnitOfTime.Year.getMilliseconds()) >= 0) {\n            return UnitOfTime.Year;\n        } else if (milliseconds.compareTo(UnitOfTime.Week.getMilliseconds()) >= 0) {\n            return UnitOfTime.Week;\n        } else if (milliseconds.compareTo(UnitOfTime.Day.getMilliseconds()) >= 0) {\n            return UnitOfTime.Day;\n        } else if (milliseconds.compareTo(UnitOfTime.Hour.getMilliseconds()) >= 0) {\n            return UnitOfTime.Hour;\n        } else if (milliseconds.compareTo(UnitOfTime.Minute.getMilliseconds()) >= 0) {\n            return UnitOfTime.Minute;\n        } else if (milliseconds.compareTo(UnitOfTime.Second.getMilliseconds()) >= 0) {\n            return UnitOfTime.Second;\n        } else {\n            return UnitOfTime.Millisecond;\n        }\n    }\n\n    \/**\n     * Determine what unit to use based on the number of milliseconds\n     *\n     * @param milliseconds Total number of milliseconds\n     * @return UnitOfTime you should use.\n     *\/\n    public static UnitOfTime useUnit(Integer milliseconds) {\n        return UnitOfTime.useUnit(BigInteger.valueOf(milliseconds));\n    }\n\n    \/**\n     * Determine what unit to use based on the number of milliseconds\n     *\n     * @param milliseconds Total number of milliseconds\n     * @return UnitOfTime you should use.\n     *\/\n    public static UnitOfTime useUnit(Long milliseconds) {\n        return UnitOfTime.useUnit(BigInteger.valueOf(milliseconds));\n    }\n\n    \/**\n     * @return the label\n     *\/\n    public final String getLabel() {\n        return this.label;\n    }\n\n    \/**\n     * @return the milliseconds\n     *\/\n    public final BigInteger getMilliseconds() {\n        return this.milliseconds;\n    }\n\n    \/**\n     * Convert to the UnitOfTime\n     *\n     * @param milliseconds Milliseconds to convert\n     * @return BigDecimal of the number of units\n     *\/\n    public BigDecimal getUnits(BigInteger milliseconds) {\n        return new BigDecimal(milliseconds.divide(this.getMilliseconds()));\n    }\n\n    \/**\n     * Convert to the UnitOfTime\n     *\n     * @param milliseconds Milliseconds to convert\n     * @return BigDecimal of the number of units\n     *\/\n    public BigDecimal getUnits(Integer milliseconds) {\n        return this.getUnits(BigInteger.valueOf(milliseconds));\n    }\n\n    \/**\n     * Convert to the UnitOfTime\n     *\n     * @param milliseconds Milliseconds to convert\n     * @return BigDecimal of the number of units\n     *\/\n    public BigDecimal getUnits(Long milliseconds) {\n        return this.getUnits(BigInteger.valueOf(milliseconds));\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Not very often do I need to take a number of bytes and convert them into the most appropriate measurement. &nbsp;This came up recently on a project and I thought it would be nice to just create a class to handle this for me. &nbsp;So I created UnitOfMemory as enum that switches between Byte, KiloByte, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2069,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[457],"tags":[],"series":[248],"class_list":["post-2067","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java_extra","series-advanced-classes"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2017\/02\/info-1641937_640.jpg?fit=640%2C426&ssl=1","jetpack-related-posts":[{"id":2072,"url":"https:\/\/www.mymiller.name\/wordpress\/java_extra\/unitofdistance\/","url_meta":{"origin":2067,"position":0},"title":"UnitOfDistance &#8211; Measure and Convert your distance","author":"Jeffery Miller","date":"November 24, 2025","format":false,"excerpt":"Well I was bored one night after creating the UnitOfMemory and decided what else could be useful in the future. \u00a0I came up with a UnitOfDistance to convert distances over land. \u00a0Miles v's kilometers and everything in between. \u00a0The class was easy to come up with, finding the exact conversion\u2026","rel":"","context":"In &quot;Java Extras&quot;","block_context":{"text":"Java Extras","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java_extra\/"},"img":{"alt_text":"UnitOfDistance","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2017\/02\/tape-measure-1860811_640.jpg?fit=640%2C426&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2017\/02\/tape-measure-1860811_640.jpg?fit=640%2C426&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2017\/02\/tape-measure-1860811_640.jpg?fit=640%2C426&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":2111,"url":"https:\/\/www.mymiller.name\/wordpress\/java_extra\/unitoftime\/","url_meta":{"origin":2067,"position":1},"title":"UnitOfTime &#8211; Measure and convert you milliseconds","author":"Jeffery Miller","date":"November 24, 2025","format":false,"excerpt":"Continuing my series on advanced classes that are handy to have around when needed. \u00a0I have created the UnitOfTime. \u00a0This class is used to convert a number of milliseconds over to a more understandable period of time. package name.mymiller.extensions.lang; import java.math.BigDecimal; import java.math.BigInteger; \/** * Measure UnitOfTime based on milliseconds\u2026","rel":"","context":"In &quot;Java Extras&quot;","block_context":{"text":"Java Extras","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java_extra\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2017\/03\/time-3222267_640.jpg?fit=640%2C426&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2017\/03\/time-3222267_640.jpg?fit=640%2C426&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2017\/03\/time-3222267_640.jpg?fit=640%2C426&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":2449,"url":"https:\/\/www.mymiller.name\/wordpress\/java\/jackson-configuration\/","url_meta":{"origin":2067,"position":2},"title":"Jackson Configuration","author":"Jeffery Miller","date":"December 23, 2025","format":false,"excerpt":"Jackson configuration for JSON to POJO conversion. Standard conversion for converting JSON to POJO objects for Rest APIs and other implementations.","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java\/"},"img":{"alt_text":"Jackson configuration","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2019\/05\/analytics-3088958_640.jpg?fit=640%2C426&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2019\/05\/analytics-3088958_640.jpg?fit=640%2C426&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2019\/05\/analytics-3088958_640.jpg?fit=640%2C426&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":3380,"url":"https:\/\/www.mymiller.name\/wordpress\/java\/synchronous-to-asynchronous\/","url_meta":{"origin":2067,"position":3},"title":"Synchronous to Asynchronous","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"Converting a synchronous method to an asynchronous one in Java involves modifying the code to allow other tasks to execute while it is waiting for input\/output operations to complete. Here's an example of how to convert a synchronous method to an asynchronous one in Java: Let's say we have a\u2026","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/multitasking-ga6749a2b2_640.jpg?fit=640%2C394&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/multitasking-ga6749a2b2_640.jpg?fit=640%2C394&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/multitasking-ga6749a2b2_640.jpg?fit=640%2C394&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":3718,"url":"https:\/\/www.mymiller.name\/wordpress\/springboot\/deep-dive-into-springs-message-conversion-custom-message-converters-and-their-role-in-serialization-deserialization\/","url_meta":{"origin":2067,"position":4},"title":"Deep Dive into Spring&#8217;s Message Conversion: Custom Message Converters and Their Role in Serialization\/Deserialization","author":"Jeffery Miller","date":"November 24, 2025","format":false,"excerpt":"When working with Spring applications, particularly those that deal with messaging, REST APIs, or data interchange, understanding message conversion is crucial. Spring provides a powerful framework for handling serialization and deserialization through its message converters. In this article, we will explore the concept of custom message converters, how they fit\u2026","rel":"","context":"In &quot;Springboot&quot;","block_context":{"text":"Springboot","link":"https:\/\/www.mymiller.name\/wordpress\/category\/springboot\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/09\/transformation-3753443_1280-jpg.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/09\/transformation-3753443_1280-jpg.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/09\/transformation-3753443_1280-jpg.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/09\/transformation-3753443_1280-jpg.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/09\/transformation-3753443_1280-jpg.avif 3x"},"classes":[]},{"id":3878,"url":"https:\/\/www.mymiller.name\/wordpress\/spring_messaging\/building-robust-kafka-applications-with-spring-boot-and-avro-schema-registry\/","url_meta":{"origin":2067,"position":5},"title":"Building Robust Kafka Applications with Spring Boot, and Avro Schema Registry","author":"Jeffery Miller","date":"November 24, 2025","format":false,"excerpt":"As a software architect, designing solutions that are scalable, maintainable, and resilient is paramount. In the world of event-driven architectures, Apache Kafka has become a cornerstone for high-throughput, low-latency data streaming. However, simply sending raw bytes over Kafka topics can lead to data inconsistency and make future evolution a nightmare.\u2026","rel":"","context":"In &quot;Spring Messaging&quot;","block_context":{"text":"Spring Messaging","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring_messaging\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/06\/ai-generated-7947638_1280.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/06\/ai-generated-7947638_1280.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/06\/ai-generated-7947638_1280.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/06\/ai-generated-7947638_1280.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/06\/ai-generated-7947638_1280.avif 3x"},"classes":[]}],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/2067","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/comments?post=2067"}],"version-history":[{"count":9,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/2067\/revisions"}],"predecessor-version":[{"id":2907,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/2067\/revisions\/2907"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media\/2069"}],"wp:attachment":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media?parent=2067"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/categories?post=2067"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/tags?post=2067"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/series?post=2067"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}