Skip to content

Commit 30ed79a

Browse files
committed
fix: build and lint fix update
1 parent 3011883 commit 30ed79a

File tree

2 files changed

+42
-74
lines changed

2 files changed

+42
-74
lines changed

src/main/java/com/thealgorithms/conversions/Base64.java

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,35 @@
11
package com.thealgorithms.conversions;
22

3-
import java.util.List;
4-
import java.util.ArrayList;
53
import 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

2121
public 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

src/test/java/com/thealgorithms/conversions/Base64Test.java

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
/**
1111
* Test cases for Base64 encoding and decoding.
12-
*
12+
*
1313
* Author: Nithin U.
1414
* Github: https://github.com/NithinU2802
1515
*/
@@ -26,37 +26,15 @@ void testBase64Alphabet() {
2626
}
2727

2828
@ParameterizedTest
29-
@CsvSource({
30-
"'', ''",
31-
"A, QQ==",
32-
"AB, QUI=",
33-
"ABC, QUJD",
34-
"ABCD, QUJDRA==",
35-
"Hello, SGVsbG8=",
36-
"'Hello World', SGVsbG8gV29ybGQ=",
37-
"'Hello, World!', 'SGVsbG8sIFdvcmxkIQ=='",
38-
"'The quick brown fox jumps over the lazy dog', 'VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw=='",
39-
"123456789, MTIzNDU2Nzg5",
40-
"'Base64 encoding test', 'QmFzZTY0IGVuY29kaW5nIHRlc3Q='"
41-
})
29+
@CsvSource({"'', ''", "A, QQ==", "AB, QUI=", "ABC, QUJD", "ABCD, QUJDRA==", "Hello, SGVsbG8=", "'Hello World', SGVsbG8gV29ybGQ=", "'Hello, World!', 'SGVsbG8sIFdvcmxkIQ=='", "'The quick brown fox jumps over the lazy dog', 'VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw=='",
30+
"123456789, MTIzNDU2Nzg5", "'Base64 encoding test', 'QmFzZTY0IGVuY29kaW5nIHRlc3Q='"})
4231
void testStringEncoding(String input, String expected) {
4332
assertEquals(expected, Base64.encode(input));
4433
}
4534

4635
@ParameterizedTest
47-
@CsvSource({
48-
"'', ''",
49-
"QQ==, A",
50-
"QUI=, AB",
51-
"QUJD, ABC",
52-
"QUJDRA==, ABCD",
53-
"SGVsbG8=, Hello",
54-
"'SGVsbG8gV29ybGQ=', 'Hello World'",
55-
"'SGVsbG8sIFdvcmxkIQ==', 'Hello, World!'",
56-
"'VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw==', 'The quick brown fox jumps over the lazy dog'",
57-
"MTIzNDU2Nzg5, 123456789",
58-
"'QmFzZTY0IGVuY29kaW5nIHRlc3Q=', 'Base64 encoding test'"
59-
})
36+
@CsvSource({"'', ''", "QQ==, A", "QUI=, AB", "QUJD, ABC", "QUJDRA==, ABCD", "SGVsbG8=, Hello", "'SGVsbG8gV29ybGQ=', 'Hello World'", "'SGVsbG8sIFdvcmxkIQ==', 'Hello, World!'", "'VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw==', 'The quick brown fox jumps over the lazy dog'",
37+
"MTIzNDU2Nzg5, 123456789", "'QmFzZTY0IGVuY29kaW5nIHRlc3Q=', 'Base64 encoding test'"})
6038
void testStringDecoding(String input, String expected) {
6139
assertEquals(expected, Base64.decodeToString(input));
6240
}
@@ -77,19 +55,10 @@ void testByteArrayDecoding() {
7755

7856
@Test
7957
void testRoundTripEncoding() {
80-
String[] testStrings = {
81-
"",
82-
"A",
83-
"AB",
84-
"ABC",
85-
"Hello, World!",
86-
"The quick brown fox jumps over the lazy dog",
87-
"1234567890",
88-
"Special chars: !@#$%^&*()_+-=[]{}|;:,.<>?",
58+
String[] testStrings = {"", "A", "AB", "ABC", "Hello, World!", "The quick brown fox jumps over the lazy dog", "1234567890", "Special chars: !@#$%^&*()_+-=[]{}|;:,.<>?",
8959
"Unicode: வணக்கம்", // Tamil for "Hello"
90-
"Multi-line\nstring\rwith\tdifferent\nwhitespace"
91-
};
92-
60+
"Multi-line\nstring\rwith\tdifferent\nwhitespace"};
61+
9362
for (String original : testStrings) {
9463
String encoded = Base64.encode(original);
9564
String decoded = Base64.decodeToString(encoded);

0 commit comments

Comments
 (0)