|
17 | 17 |
|
18 | 18 | package org.apache.commons.codec.binary; |
19 | 19 |
|
| 20 | +import java.util.Arrays; |
20 | 21 | import java.util.Objects; |
21 | 22 |
|
22 | 23 | import org.apache.commons.codec.CodecPolicy; |
@@ -85,11 +86,13 @@ public static class Builder extends AbstractBuilder<Base32, Builder> { |
85 | 86 | */ |
86 | 87 | public Builder() { |
87 | 88 | super(ENCODE_TABLE); |
| 89 | + setEncodedBlockSize(BYTES_PER_ENCODED_BLOCK); |
| 90 | + setUnencodedBlockSize(BYTES_PER_UNENCODED_BLOCK); |
88 | 91 | } |
89 | 92 |
|
90 | 93 | @Override |
91 | 94 | public Base32 get() { |
92 | | - return new Base32(getLineLength(), getLineSeparator(), getEncodeTable(), getPadding(), getDecodingPolicy()); |
| 95 | + return new Base32(this); |
93 | 96 | } |
94 | 97 |
|
95 | 98 | /** |
@@ -338,6 +341,33 @@ public Base32(final boolean useHex, final byte padding) { |
338 | 341 | this(0, null, useHex, padding); |
339 | 342 | } |
340 | 343 |
|
| 344 | + private Base32(final Builder builder) { |
| 345 | + super(builder); |
| 346 | + Objects.requireNonNull(builder.getEncodeTable(), "encodeTable"); |
| 347 | + this.encodeTable = builder.getEncodeTable(); |
| 348 | + this.decodeTable = builder.getEncodeTable() == HEX_ENCODE_TABLE || Arrays.equals(builder.getEncodeTable(), HEX_ENCODE_TABLE) ? |
| 349 | + HEX_DECODE_TABLE : DECODE_TABLE; |
| 350 | + if (builder.getLineLength() > 0) { |
| 351 | + if (builder.getLineSeparator() == null) { |
| 352 | + throw new IllegalArgumentException("lineLength " + lineLength + " > 0, but lineSeparator is null"); |
| 353 | + } |
| 354 | + final byte[] lineSeparatorCopy = builder.getLineSeparator().clone(); |
| 355 | + // Must be done after initializing the tables |
| 356 | + if (containsAlphabetOrPad(lineSeparatorCopy)) { |
| 357 | + final String sep = StringUtils.newStringUtf8(lineSeparatorCopy); |
| 358 | + throw new IllegalArgumentException("lineSeparator must not contain Base32 characters: [" + sep + "]"); |
| 359 | + } |
| 360 | + this.encodeSize = BYTES_PER_ENCODED_BLOCK + lineSeparatorCopy.length; |
| 361 | + this.lineSeparator = lineSeparatorCopy; |
| 362 | + } else { |
| 363 | + this.encodeSize = BYTES_PER_ENCODED_BLOCK; |
| 364 | + this.lineSeparator = null; |
| 365 | + } |
| 366 | + if (isInAlphabet(builder.getPadding()) || Character.isWhitespace(builder.getPadding())) { |
| 367 | + throw new IllegalArgumentException("pad must not be in alphabet or whitespace"); |
| 368 | + } |
| 369 | + } |
| 370 | + |
341 | 371 | /** |
342 | 372 | * Constructs a Base32 codec used for decoding and encoding. |
343 | 373 | * <p> |
@@ -469,50 +499,8 @@ public Base32(final int lineLength, final byte[] lineSeparator, final boolean us |
469 | 499 | */ |
470 | 500 | @Deprecated |
471 | 501 | public Base32(final int lineLength, final byte[] lineSeparator, final boolean useHex, final byte padding, final CodecPolicy decodingPolicy) { |
472 | | - this(lineLength, lineSeparator, encodeTable(useHex), padding, decodingPolicy); |
473 | | - } |
474 | | - |
475 | | - /** |
476 | | - * Constructs a Base32 / Base32 Hex codec used for decoding and encoding. |
477 | | - * <p> |
478 | | - * When encoding the line length and line separator are given in the constructor. |
479 | | - * </p> |
480 | | - * <p> |
481 | | - * Line lengths that aren't multiples of 8 will still essentially end up being multiples of 8 in the encoded data. |
482 | | - * </p> |
483 | | - * |
484 | | - * @param lineLength Each line of encoded data will be at most of the given length (rounded down to the nearest multiple of 8). If lineLength <= 0, |
485 | | - * then the output will not be divided into lines (chunks). Ignored when decoding. |
486 | | - * @param lineSeparator Each line of encoded data will end with this sequence of bytes. |
487 | | - * @param encodeTable A Base32 alphabet. |
488 | | - * @param padding padding byte. |
489 | | - * @param decodingPolicy The decoding policy. |
490 | | - * @throws IllegalArgumentException Thrown when the {@code lineSeparator} contains Base32 characters. Or the lineLength > 0 and lineSeparator is null. |
491 | | - */ |
492 | | - private Base32(final int lineLength, final byte[] lineSeparator, final byte[] encodeTable, final byte padding, final CodecPolicy decodingPolicy) { |
493 | | - super(BYTES_PER_UNENCODED_BLOCK, BYTES_PER_ENCODED_BLOCK, lineLength, toLength(lineSeparator), padding, decodingPolicy); |
494 | | - Objects.requireNonNull(encodeTable, "encodeTable"); |
495 | | - this.encodeTable = encodeTable; |
496 | | - this.decodeTable = encodeTable == HEX_ENCODE_TABLE ? HEX_DECODE_TABLE : DECODE_TABLE; |
497 | | - if (lineLength > 0) { |
498 | | - if (lineSeparator == null) { |
499 | | - throw new IllegalArgumentException("lineLength " + lineLength + " > 0, but lineSeparator is null"); |
500 | | - } |
501 | | - final byte[] lineSeparatorCopy = lineSeparator.clone(); |
502 | | - // Must be done after initializing the tables |
503 | | - if (containsAlphabetOrPad(lineSeparatorCopy)) { |
504 | | - final String sep = StringUtils.newStringUtf8(lineSeparatorCopy); |
505 | | - throw new IllegalArgumentException("lineSeparator must not contain Base32 characters: [" + sep + "]"); |
506 | | - } |
507 | | - this.encodeSize = BYTES_PER_ENCODED_BLOCK + lineSeparatorCopy.length; |
508 | | - this.lineSeparator = lineSeparatorCopy; |
509 | | - } else { |
510 | | - this.encodeSize = BYTES_PER_ENCODED_BLOCK; |
511 | | - this.lineSeparator = null; |
512 | | - } |
513 | | - if (isInAlphabet(padding) || Character.isWhitespace(padding)) { |
514 | | - throw new IllegalArgumentException("pad must not be in alphabet or whitespace"); |
515 | | - } |
| 502 | + this(builder().setLineLength(lineLength).setLineSeparator(lineSeparator).setEncodeTableRaw(encodeTable(useHex)).setPadding(padding) |
| 503 | + .setDecodingPolicy(decodingPolicy)); |
516 | 504 | } |
517 | 505 |
|
518 | 506 | /** |
|
0 commit comments