diff --git a/src/java.base/share/classes/java/util/Base64.java b/src/java.base/share/classes/java/util/Base64.java index 0973a63844c..398dee43dba 100644 --- a/src/java.base/share/classes/java/util/Base64.java +++ b/src/java.base/share/classes/java/util/Base64.java @@ -116,8 +116,8 @@ public class Base64 { * * @param lineLength * the length of each output line (rounded down to nearest multiple - * of 4). If {@code lineLength <= 0} the output will not be separated - * in lines + * of 4). If the rounded down line length is not a positive value, + * the output will not be separated in lines * @param lineSeparator * the line separator for each output line * @@ -135,10 +135,12 @@ public class Base64 { throw new IllegalArgumentException( "Illegal base64 line separator character 0x" + Integer.toString(b, 16)); } + // round down to nearest multiple of 4 + lineLength &= ~0b11; if (lineLength <= 0) { return Encoder.RFC4648; } - return new Encoder(false, lineSeparator, lineLength >> 2 << 2, true); + return new Encoder(false, lineSeparator, lineLength, true); } /** diff --git a/test/jdk/java/util/Base64/Base64GetEncoderTest.java b/test/jdk/java/util/Base64/Base64GetEncoderTest.java index 563cb797021..f1d206fa472 100644 --- a/test/jdk/java/util/Base64/Base64GetEncoderTest.java +++ b/test/jdk/java/util/Base64/Base64GetEncoderTest.java @@ -34,23 +34,23 @@ import static java.nio.charset.StandardCharsets.US_ASCII; /** * @test - * @bug 8007799 + * @bug 8007799 8176379 * @summary test Encoder with linemax == 0, line separator should not appear in encoded data */ public class Base64GetEncoderTest { public static void main(String args[]) throws Throwable { - final Base64.Encoder encoder = Base64.getMimeEncoder(0, "$$$".getBytes(US_ASCII)); - testEncodeToString(encoder); + for (int maxlen = -4; maxlen < 4; maxlen++) { - testWrapEncode1(encoder); - - testEncodeToStringWithLongInputData(encoder); - - testWrapEncode2(encoder); + final Base64.Encoder encoder = Base64.getMimeEncoder(maxlen, "$$$".getBytes(US_ASCII)); + testEncodeToString(encoder); + testWrapEncode1(encoder); + testEncodeToStringWithLongInputData(encoder); + testWrapEncode2(encoder); + } } private static void testWrapEncode2(final Base64.Encoder encoder) diff --git a/test/jdk/java/util/Base64/TestBase64.java b/test/jdk/java/util/Base64/TestBase64.java index 423156f2d2c..273592801a9 100644 --- a/test/jdk/java/util/Base64/TestBase64.java +++ b/test/jdk/java/util/Base64/TestBase64.java @@ -24,7 +24,7 @@ /** * @test * @bug 4235519 8004212 8005394 8007298 8006295 8006315 8006530 8007379 8008925 - * 8014217 8025003 8026330 8028397 8129544 8165243 + * 8014217 8025003 8026330 8028397 8129544 8165243 8176379 * @summary tests java.util.Base64 * @library /test/lib * @build jdk.test.lib.RandomFactory @@ -78,6 +78,21 @@ public class TestBase64 { numRuns, numBytes); } + // test mime case with < 4 length + for (int len = 0; len < 4; len++) { + test(Base64.getMimeEncoder(len, nl_1), + Base64.getMimeDecoder(), + numRuns, numBytes); + + test(Base64.getMimeEncoder(len, nl_2), + Base64.getMimeDecoder(), + numRuns, numBytes); + + test(Base64.getMimeEncoder(len, nl_3), + Base64.getMimeDecoder(), + numRuns, numBytes); + } + testNull(Base64.getEncoder()); testNull(Base64.getUrlEncoder()); testNull(Base64.getMimeEncoder());