Skip to content

Commit f6deb13

Browse files
committed
Pull-up common Builder attributes to BaseNCodec
1 parent 3e27d96 commit f6deb13

2 files changed

Lines changed: 31 additions & 15 deletions

File tree

src/main/java/org/apache/commons/codec/binary/Base64.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,16 @@ public class Base64 extends BaseNCodec {
6363
*/
6464
public static class Builder extends AbstractBuilder<Base64, Builder> {
6565

66-
private byte[] encodeTable = STANDARD_ENCODE_TABLE;
66+
/**
67+
* Constructs a new instance.
68+
*/
69+
public Builder() {
70+
super(STANDARD_ENCODE_TABLE);
71+
}
6772

6873
@Override
6974
public Base64 get() {
70-
return new Base64(getLineLength(), getLineSeparator(), encodeTable, getDecodingPolicy());
71-
}
72-
73-
/**
74-
* Sets the encode table.
75-
*
76-
* @param encodeTable the encode table, null resets to the default.
77-
* @return this.
78-
*/
79-
public Builder setEncodeTable(final byte... encodeTable) {
80-
this.encodeTable = encodeTable != null ? encodeTable : STANDARD_ENCODE_TABLE;
81-
return this;
75+
return new Base64(getLineLength(), getLineSeparator(), getEncodeTable(), getDecodingPolicy());
8276
}
8377

8478
/**
@@ -88,8 +82,7 @@ public Builder setEncodeTable(final byte... encodeTable) {
8882
* @return this.
8983
*/
9084
public Builder setUrlSafe(final boolean urlSafe) {
91-
this.encodeTable = toUrlSafeEncodeTable(urlSafe);
92-
return this;
85+
return setEncodeTable(toUrlSafeEncodeTable(urlSafe));
9386
}
9487

9588
}

src/main/java/org/apache/commons/codec/binary/BaseNCodec.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ public abstract static class AbstractBuilder<T, B extends AbstractBuilder<T, B>>
6464
private CodecPolicy decodingPolicy = DECODING_POLICY_DEFAULT;
6565
private int lineLength;
6666
private byte[] lineSeparator = CHUNK_SEPARATOR;
67+
private final byte[] defaultEncodeTable;
68+
private byte[] encodeTable;
69+
70+
71+
AbstractBuilder(final byte[] defaultEncodeTable) {
72+
this.defaultEncodeTable = defaultEncodeTable;
73+
this.encodeTable = defaultEncodeTable;
74+
}
6775

6876
@SuppressWarnings("unchecked")
6977
B asThis() {
@@ -74,6 +82,10 @@ CodecPolicy getDecodingPolicy() {
7482
return decodingPolicy;
7583
}
7684

85+
byte[] getEncodeTable() {
86+
return encodeTable;
87+
}
88+
7789
int getLineLength() {
7890
return lineLength;
7991
}
@@ -93,6 +105,17 @@ public B setDecodingPolicy(final CodecPolicy decodingPolicy) {
93105
return asThis();
94106
}
95107

108+
/**
109+
* Sets the encode table.
110+
*
111+
* @param encodeTable the encode table, null resets to the default.
112+
* @return this.
113+
*/
114+
public B setEncodeTable(final byte... encodeTable) {
115+
this.encodeTable = encodeTable != null ? encodeTable : defaultEncodeTable;
116+
return asThis();
117+
}
118+
96119
/**
97120
* Sets the line length.
98121
*

0 commit comments

Comments
 (0)