Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Signed-off-by: Antonio Santos Izaguirre <[email protected]>
  • Loading branch information
antoniosanct authored Feb 15, 2024
1 parent ebf13ac commit 3eb7824
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
Expand Down Expand Up @@ -706,6 +706,9 @@ private static int guessLength(String text) {
*/
public static byte[] _parseBase64Binary(String text) {
final int buflen = guessLength(text);
if (buflen < 3) {
throw new IllegalArgumentException("base64 text invalid.");
}
final byte[] out = new byte[buflen];
int o = 0;

Expand All @@ -725,6 +728,7 @@ public static byte[] _parseBase64Binary(String text) {
}

if (q == 4) {

// quadruplet is now filled.
out[o++] = (byte) ((quadruplet[0] << 2) | (quadruplet[1] >> 4));
if (quadruplet[2] != PADDING) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,13 @@ public void testParseBoolean() {
Assert.assertEquals(true, DatatypeConverter.parseBoolean("true "));
Assert.assertEquals(true, DatatypeConverter.parseBoolean(" true "));
}

@Test
public void testBase64() {
Assert.assertThrows(IllegalArgumentException.class, () -> DatatypeConverter.parseBase64Binary("Qxx=="));
Assert.assertNotEquals("Hello, world!", new String(DatatypeConverter.parseBase64Binary("SGVsbG8sIJdvcmxkIQ==")));

Assert.assertEquals("Hello, world!", new String(DatatypeConverter.parseBase64Binary("SGVsbG8sIHdvcmxkIQ==")));
}

}

0 comments on commit 3eb7824

Please sign in to comment.