Wednesday, 3 May 2023

Convert the memory size representation to a long value in bytes

Write a function that converts the human readable memory representation to a long value in bytes. Please find the below table for the inputs and outputs.

 

Input

Output (in bytes)

10K

10240

10M

10485760

10G

10737418240

10T

10995116277760

10P

11258999068426240

 

Find the below working application.


MemorySizeToLongValue.java

package com.sample.app.strings;

public class MemorySizeToLongValue {
	// Size in bytes
	private static final long KILO_BYTE = 1024;
	private static final long MEGA_BYTE = KILO_BYTE * 1024;
	private static final long GIGA_BYTE = MEGA_BYTE * 1024;
	private static final long TERA_BYTE = GIGA_BYTE * 1024;
	private static final long PETA_BYTE = TERA_BYTE * 1024;

	public static long toBytes(final String memorySize) throws Exception {
		final String inputStr = memorySize.trim();
		String memorySizeWithoutSign = null;
		long sign = 1L;
		char firstChar = memorySize.charAt(0);

		switch (firstChar) {
		case '+':
			memorySizeWithoutSign = inputStr.substring(1);
			break;
		case '-':
			sign = -1L;
			memorySizeWithoutSign = inputStr.substring(1);
			break;
		default:
			memorySizeWithoutSign = inputStr;
			break;
		}

		// last character should be K, M, G, T, P.
		long result = sign;
		char lastChar = memorySizeWithoutSign.charAt(memorySizeWithoutSign.length() - 1);

		switch (lastChar) {
		case 'K':
			result *= KILO_BYTE;
			break;
		case 'M':
			result *= MEGA_BYTE;
			break;
		case 'G':
			result *= GIGA_BYTE;
			break;
		case 'T':
			result *= TERA_BYTE;
			break;
		case 'P':
			result *= PETA_BYTE;
			break;
		default:
			throw new IllegalArgumentException("Invalid input format. Valid values are : (10K, 10M, 10G, 10T, 10P)");
		}

		memorySizeWithoutSign = memorySizeWithoutSign.substring(0, memorySizeWithoutSign.length() - 1);

		return result * Long.parseLong(memorySizeWithoutSign);

	}

	public static void main(String[] args) throws Exception {
		System.out.println("10K : " + toBytes("10K") + " bytes");
		System.out.println("10M : " + toBytes("10M") + " bytes");
		System.out.println("10G : " + toBytes("10G") + " bytes");
		System.out.println("10T : " + toBytes("10T") + " bytes");
		System.out.println("10P : " + toBytes("10P") + " bytes\n");

		System.out.println("+10K : " + toBytes("+10K") + " bytes");
		System.out.println("+10M : " + toBytes("+10M") + " bytes");
		System.out.println("+10G : " + toBytes("+10G") + " bytes");
		System.out.println("+10T : " + toBytes("+10T") + " bytes");
		System.out.println("+10P : " + toBytes("+10P") + " bytes\n");

		System.out.println("-10K : " + toBytes("-10K") + " bytes");
		System.out.println("-10M : " + toBytes("-10M") + " bytes");
		System.out.println("-10G : " + toBytes("-10G") + " bytes");
		System.out.println("-10T : " + toBytes("-10T") + " bytes");
		System.out.println("-10P : " + toBytes("-10P") + " bytes\n");
	}

}

 

Output

10K : 10240 bytes
10M : 10485760 bytes
10G : 10737418240 bytes
10T : 10995116277760 bytes
10P : 11258999068426240 bytes

+10K : 10240 bytes
+10M : 10485760 bytes
+10G : 10737418240 bytes
+10T : 10995116277760 bytes
+10P : 11258999068426240 bytes

-10K : -10240 bytes
-10M : -10485760 bytes
-10G : -10737418240 bytes
-10T : -10995116277760 bytes
-10P : -11258999068426240 bytes

 

 


Previous                                                 Next                                                 Home

No comments:

Post a Comment