From eedbb931fba57d398d62024cd77135610d6beadb Mon Sep 17 00:00:00 2001 From: Luck Date: Sun, 2 Jun 2019 09:35:04 +0100 Subject: Add support for heap dump compression (#16) --- .../me/lucko/spark/common/util/FormatUtil.java | 39 ++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 spark-common/src/main/java/me/lucko/spark/common/util/FormatUtil.java (limited to 'spark-common/src/main/java/me/lucko/spark/common/util') diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/FormatUtil.java b/spark-common/src/main/java/me/lucko/spark/common/util/FormatUtil.java new file mode 100644 index 0000000..37165d1 --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/common/util/FormatUtil.java @@ -0,0 +1,39 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * This program 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 . + */ + +package me.lucko.spark.common.util; + +public final class FormatUtil { + private FormatUtil() {} + + public static String percent(double value, double max) { + double percent = (value * 100d) / max; + return (int) percent + "%"; + } + + public static String formatBytes(long bytes) { + if (bytes == 0) { + return "0 bytes"; + } + String[] sizes = new String[]{"bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}; + int sizeIndex = (int) (Math.log(bytes) / Math.log(1024)); + return String.format("%.1f", bytes / Math.pow(1024, sizeIndex)) + " " + sizes[sizeIndex]; + } +} -- cgit