@@ -98,11 +98,13 @@ public static class Builder extends AbstractBuilder<Base64, Builder> {
9898 */
9999 public Builder () {
100100 super (STANDARD_ENCODE_TABLE );
101+ setEncodedBlockSize (BYTES_PER_ENCODED_BLOCK );
102+ setUnencodedBlockSize (BYTES_PER_UNENCODED_BLOCK );
101103 }
102104
103105 @ Override
104106 public Base64 get () {
105- return new Base64 (getLineLength (), getLineSeparator (), getPadding (), getEncodeTable (), getDecodingPolicy () );
107+ return new Base64 (this );
106108 }
107109
108110 /**
@@ -529,6 +531,9 @@ static byte[] toUrlSafeEncodeTable(final boolean urlSafe) {
529531
530532 private final boolean isUrlSafe ;
531533
534+ private final boolean isStandardEncodeTable ;
535+
536+
532537 /**
533538 * Constructs a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.
534539 * <p>
@@ -646,7 +651,8 @@ public Base64(final int lineLength, final byte[] lineSeparator) {
646651 */
647652 @ Deprecated
648653 public Base64 (final int lineLength , final byte [] lineSeparator , final boolean urlSafe ) {
649- this (lineLength , lineSeparator , PAD_DEFAULT , toUrlSafeEncodeTable (urlSafe ), DECODING_POLICY_DEFAULT );
654+ this (builder ().setLineLength (lineLength ).setLineSeparator (lineSeparator != null ? lineSeparator : EMPTY_BYTE_ARRAY ).setPadding (PAD_DEFAULT )
655+ .setEncodeTableRaw (toUrlSafeEncodeTable (urlSafe )).setDecodingPolicy (DECODING_POLICY_DEFAULT ));
650656 }
651657
652658 /**
@@ -680,56 +686,31 @@ public Base64(final int lineLength, final byte[] lineSeparator, final boolean ur
680686 */
681687 @ Deprecated
682688 public Base64 (final int lineLength , final byte [] lineSeparator , final boolean urlSafe , final CodecPolicy decodingPolicy ) {
683- this (lineLength , lineSeparator , PAD_DEFAULT , toUrlSafeEncodeTable (urlSafe ), decodingPolicy );
689+ this (builder ().setLineLength (lineLength ).setLineSeparator (lineSeparator ).setPadding (PAD_DEFAULT ).setEncodeTableRaw (toUrlSafeEncodeTable (urlSafe ))
690+ .setDecodingPolicy (decodingPolicy ));
684691 }
685692
686- /**
687- * Constructs a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.
688- * <p>
689- * When encoding the line length and line separator are given in the constructor, and the encoding table is STANDARD_ENCODE_TABLE.
690- * </p>
691- * <p>
692- * Line lengths that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data.
693- * </p>
694- * <p>
695- * When decoding all variants are supported.
696- * </p>
697- *
698- * @param lineLength Each line of encoded data will be at most of the given length (rounded down to the nearest multiple of 4). If lineLength <= 0,
699- * then the output will not be divided into lines (chunks). Ignored when decoding.
700- * @param lineSeparator Each line of encoded data will end with this sequence of bytes; the constructor makes a defensive copy. May be null.
701- * @param padding padding byte.
702- * @param encodeTable The manual encodeTable - a byte array of 64 chars.
703- * @param decodingPolicy The decoding policy.
704- * @throws IllegalArgumentException Thrown when the {@code lineSeparator} contains Base64 characters.
705- */
706- private Base64 (final int lineLength , final byte [] lineSeparator , final byte padding , final byte [] encodeTable , final CodecPolicy decodingPolicy ) {
707- super (BYTES_PER_UNENCODED_BLOCK , BYTES_PER_ENCODED_BLOCK , lineLength , toLength (lineSeparator ), padding , decodingPolicy );
708- Objects .requireNonNull (encodeTable , "encodeTable" );
709- if (encodeTable .length != STANDARD_ENCODE_TABLE .length ) {
693+ private Base64 (final Builder builder ) {
694+ super (builder );
695+ Objects .requireNonNull (builder .getEncodeTable (), "encodeTable" );
696+ if (builder .getEncodeTable ().length != STANDARD_ENCODE_TABLE .length ) {
710697 throw new IllegalArgumentException ("encodeTable must have exactly 64 entries." );
711698 }
712- // same array first or equal contents second
713- this .isUrlSafe = encodeTable == URL_SAFE_ENCODE_TABLE || Arrays .equals (encodeTable , URL_SAFE_ENCODE_TABLE );
714- if (encodeTable == STANDARD_ENCODE_TABLE || this .isUrlSafe ) {
715- decodeTable = DECODE_TABLE ;
716- // No need of a defensive copy of an internal table.
717- this .encodeTable = encodeTable ;
718- } else {
719- this .encodeTable = encodeTable .clone ();
720- this .decodeTable = calculateDecodeTable (this .encodeTable );
721- }
699+ this .isStandardEncodeTable = Arrays .equals (builder .getEncodeTable (), STANDARD_ENCODE_TABLE );
700+ this .isUrlSafe = Arrays .equals (builder .getEncodeTable (), URL_SAFE_ENCODE_TABLE );
701+ this .encodeTable = builder .getEncodeTable ();
702+ this .decodeTable = this .isStandardEncodeTable || this .isUrlSafe ? DECODE_TABLE : calculateDecodeTable (this .encodeTable );
722703 // TODO could be simplified if there is no requirement to reject invalid line sep when length <=0
723704 // @see test case Base64Test.testConstructors()
724- if (lineSeparator != null ) {
725- final byte [] lineSeparatorCopy = lineSeparator . clone ();
726- if (containsAlphabetOrPad (lineSeparatorCopy )) {
727- final String sep = StringUtils .newStringUtf8 (lineSeparatorCopy );
705+ if (builder . getLineSeparator (). length > 0 ) {
706+ final byte [] lineSeparatorB = builder . getLineSeparator ();
707+ if (containsAlphabetOrPad (lineSeparatorB )) {
708+ final String sep = StringUtils .newStringUtf8 (lineSeparatorB );
728709 throw new IllegalArgumentException ("lineSeparator must not contain base64 characters: [" + sep + "]" );
729710 }
730- if (lineLength > 0 ) { // null line-sep forces no chunking rather than throwing IAE
731- this .encodeSize = BYTES_PER_ENCODED_BLOCK + lineSeparatorCopy .length ;
732- this .lineSeparator = lineSeparatorCopy ;
711+ if (builder . getLineLength () > 0 ) { // null line-sep forces no chunking rather than throwing IAE
712+ this .encodeSize = BYTES_PER_ENCODED_BLOCK + lineSeparatorB .length ;
713+ this .lineSeparator = lineSeparatorB ;
733714 } else {
734715 this .encodeSize = BYTES_PER_ENCODED_BLOCK ;
735716 this .lineSeparator = null ;
@@ -885,7 +866,7 @@ void encode(final byte[] in, int inPos, final int inAvail, final Context context
885866 // remaining 2:
886867 buffer [context .pos ++] = encodeTable [context .ibitWorkArea << 4 & MASK_6_BITS ];
887868 // URL-SAFE skips the padding to further reduce size.
888- if (encodeTable == STANDARD_ENCODE_TABLE ) {
869+ if (isStandardEncodeTable ) {
889870 buffer [context .pos ++] = pad ;
890871 buffer [context .pos ++] = pad ;
891872 }
@@ -896,7 +877,7 @@ void encode(final byte[] in, int inPos, final int inAvail, final Context context
896877 buffer [context .pos ++] = encodeTable [context .ibitWorkArea >> 4 & MASK_6_BITS ];
897878 buffer [context .pos ++] = encodeTable [context .ibitWorkArea << 2 & MASK_6_BITS ];
898879 // URL-SAFE skips the padding to further reduce size.
899- if (encodeTable == STANDARD_ENCODE_TABLE ) {
880+ if (isStandardEncodeTable ) {
900881 buffer [context .pos ++] = pad ;
901882 }
902883 break ;
0 commit comments