11package com .thealgorithms .conversions ;
22
3- import java .util .List ;
4- import java .util .ArrayList ;
53import java .nio .charset .StandardCharsets ;
4+ import java .util .ArrayList ;
5+ import java .util .List ;
66
77/**
88 * Base64 is a group of binary-to-text encoding schemes that represent binary data
99 * in an ASCII string format by translating it into a radix-64 representation.
1010 * Each base64 digit represents exactly 6 bits of data.
11- *
11+ *
1212 * Base64 encoding is commonly used when there is a need to encode binary data
1313 * that needs to be stored and transferred over media that are designed to deal
1414 * with textual data.
15- *
15+ *
1616 * Wikipedia Reference: https://en.wikipedia.org/wiki/Base64
1717 * Author: Nithin U.
1818 * Github: https://github.com/NithinU2802
1919 */
2020
2121public final class Base64 {
22-
22+
2323 // Base64 character set
2424 private static final String BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ;
2525 private static final char PADDING_CHAR = '=' ;
26-
26+
2727 private Base64 () {
28-
2928 }
3029
3130 /**
3231 * Encodes the given byte array to a Base64 encoded string.
33- *
32+ *
3433 * @param input the byte array to encode
3534 * @return the Base64 encoded string
3635 * @throws IllegalArgumentException if input is null
@@ -39,52 +38,52 @@ public static String encode(byte[] input) {
3938 if (input == null ) {
4039 throw new IllegalArgumentException ("Input cannot be null" );
4140 }
42-
41+
4342 if (input .length == 0 ) {
4443 return "" ;
4544 }
46-
45+
4746 StringBuilder result = new StringBuilder ();
4847 int padding = 0 ;
49-
48+
5049 // Process input in groups of 3 bytes
5150 for (int i = 0 ; i < input .length ; i += 3 ) {
5251 // Get up to 3 bytes
5352 int byte1 = input [i ] & 0xFF ;
5453 int byte2 = (i + 1 < input .length ) ? (input [i + 1 ] & 0xFF ) : 0 ;
5554 int byte3 = (i + 2 < input .length ) ? (input [i + 2 ] & 0xFF ) : 0 ;
56-
55+
5756 // Calculate padding needed
5857 if (i + 1 >= input .length ) {
5958 padding = 2 ;
6059 } else if (i + 2 >= input .length ) {
6160 padding = 1 ;
6261 }
63-
62+
6463 // Combine 3 bytes into a 24-bit number
6564 int combined = (byte1 << 16 ) | (byte2 << 8 ) | byte3 ;
66-
65+
6766 // Extract four 6-bit groups
6867 result .append (BASE64_CHARS .charAt ((combined >> 18 ) & 0x3F ));
6968 result .append (BASE64_CHARS .charAt ((combined >> 12 ) & 0x3F ));
7069 result .append (BASE64_CHARS .charAt ((combined >> 6 ) & 0x3F ));
7170 result .append (BASE64_CHARS .charAt (combined & 0x3F ));
7271 }
73-
72+
7473 // Replace padding characters
7574 if (padding > 0 ) {
7675 result .setLength (result .length () - padding );
7776 for (int i = 0 ; i < padding ; i ++) {
7877 result .append (PADDING_CHAR );
7978 }
8079 }
81-
80+
8281 return result .toString ();
8382 }
84-
83+
8584 /**
8685 * Encodes the given string to a Base64 encoded string using UTF-8 encoding.
87- *
86+ *
8887 * @param input the string to encode
8988 * @return the Base64 encoded string
9089 * @throws IllegalArgumentException if input is null
@@ -93,13 +92,13 @@ public static String encode(String input) {
9392 if (input == null ) {
9493 throw new IllegalArgumentException ("Input cannot be null" );
9594 }
96-
95+
9796 return encode (input .getBytes (StandardCharsets .UTF_8 ));
9897 }
99-
98+
10099 /**
101100 * Decodes the given Base64 encoded string to a byte array.
102- *
101+ *
103102 * @param input the Base64 encoded string to decode
104103 * @return the decoded byte array
105104 * @throws IllegalArgumentException if input is null or contains invalid Base64 characters
@@ -108,33 +107,33 @@ public static byte[] decode(String input) {
108107 if (input == null ) {
109108 throw new IllegalArgumentException ("Input cannot be null" );
110109 }
111-
110+
112111 if (input .isEmpty ()) {
113112 return new byte [0 ];
114113 }
115-
114+
116115 // Remove padding for processing
117116 String cleanInput = input .replace ("=" , "" );
118117 int padding = input .length () - cleanInput .length ();
119-
118+
120119 // Validate input length
121120 if ((cleanInput .length () % 4 ) + padding > 4 ) {
122121 throw new IllegalArgumentException ("Invalid Base64 input length" );
123122 }
124-
123+
125124 List <Byte > result = new ArrayList <>();
126-
125+
127126 // Process input in groups of 4 characters
128127 for (int i = 0 ; i < cleanInput .length (); i += 4 ) {
129128 // Get up to 4 characters
130129 int char1 = getBase64Value (cleanInput .charAt (i ));
131130 int char2 = (i + 1 < cleanInput .length ()) ? getBase64Value (cleanInput .charAt (i + 1 )) : 0 ;
132131 int char3 = (i + 2 < cleanInput .length ()) ? getBase64Value (cleanInput .charAt (i + 2 )) : 0 ;
133132 int char4 = (i + 3 < cleanInput .length ()) ? getBase64Value (cleanInput .charAt (i + 3 )) : 0 ;
134-
133+
135134 // Combine four 6-bit groups into a 24-bit number
136135 int combined = (char1 << 18 ) | (char2 << 12 ) | (char3 << 6 ) | char4 ;
137-
136+
138137 // Extract three 8-bit bytes
139138 result .add ((byte ) ((combined >> 16 ) & 0xFF ));
140139 if (i + 2 < cleanInput .length () || (i + 2 == cleanInput .length () && padding < 2 )) {
@@ -144,19 +143,19 @@ public static byte[] decode(String input) {
144143 result .add ((byte ) (combined & 0xFF ));
145144 }
146145 }
147-
146+
148147 // Convert List<Byte> to byte[]
149148 byte [] resultArray = new byte [result .size ()];
150149 for (int i = 0 ; i < result .size (); i ++) {
151150 resultArray [i ] = result .get (i );
152151 }
153-
152+
154153 return resultArray ;
155154 }
156-
155+
157156 /**
158157 * Decodes the given Base64 encoded string to a string using UTF-8 encoding.
159- *
158+ *
160159 * @param input the Base64 encoded string to decode
161160 * @return the decoded string
162161 * @throws IllegalArgumentException if input is null or contains invalid Base64 characters
@@ -165,14 +164,14 @@ public static String decodeToString(String input) {
165164 if (input == null ) {
166165 throw new IllegalArgumentException ("Input cannot be null" );
167166 }
168-
167+
169168 byte [] decodedBytes = decode (input );
170169 return new String (decodedBytes , StandardCharsets .UTF_8 );
171170 }
172-
171+
173172 /**
174173 * Gets the numeric value of a Base64 character.
175- *
174+ *
176175 * @param c the Base64 character
177176 * @return the numeric value (0-63)
178177 * @throws IllegalArgumentException if character is not a valid Base64 character
0 commit comments