Skip to content

Commit 9fb8ee2

Browse files
committed
Add Base32.Builder.setHexDecodeTable(boolean)
- Add Base32.Builder.setHexEncodeTable(boolean) - New public API means the next version will be 1.18.0
1 parent 0d68382 commit 9fb8ee2

File tree

4 files changed

+62
-9
lines changed

4 files changed

+62
-9
lines changed

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ limitations under the License.
2828
</parent>
2929
<groupId>commons-codec</groupId>
3030
<artifactId>commons-codec</artifactId>
31-
<version>1.17.3-SNAPSHOT</version>
31+
<version>1.18.0-SNAPSHOT</version>
3232
<name>Apache Commons Codec</name>
3333
<inceptionYear>2002</inceptionYear>
3434
<description>
@@ -93,9 +93,9 @@ limitations under the License.
9393
<checkstyle.header.file>${basedir}/src/conf/checkstyle-header.txt</checkstyle.header.file>
9494
<checkstyle.config.file>${basedir}/src/conf/checkstyle.xml</checkstyle.config.file>
9595
<!-- Commons Release Plugin -->
96-
<commons.release.version>1.17.2</commons.release.version>
96+
<commons.release.version>1.18.0</commons.release.version>
9797
<commons.bc.version>1.17.1</commons.bc.version>
98-
<commons.release.next>1.17.3</commons.release.next>
98+
<commons.release.next>1.18.1</commons.release.next>
9999
<commons.rc.version>RC1</commons.rc.version>
100100
<commons.release.isDistModule>true</commons.release.isDistModule>
101101
<commons.distSvnStagingUrl>scm:svn:https://dist.apache.org/repos/dist/dev/commons/${commons.componentid}</commons.distSvnStagingUrl>

src/changes/changes.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ The <action> type attribute can be add,update,fix,remove.
4343
<author>Apache Commons Developers</author>
4444
</properties>
4545
<body>
46-
<release version="1.17.3" date="YYYY-MM-DD" description="This is a feature and maintenance release. Java 8 or later is required.">
46+
<release version="1.18.0" date="YYYY-MM-DD" description="This is a feature and maintenance release. Java 8 or later is required.">
4747
<!-- FIX -->
4848
<!-- ADD -->
49+
<action type="add" dev="ggregory" due-to="Gary Gregory, Julian Reschke">Add Base32.Builder.setHexDecodeTable(boolean).</action>
50+
<action type="add" dev="ggregory" due-to="Gary Gregory, Julian Reschke">Add Base32.Builder.setHexEncodeTable(boolean).</action>
4951
<!-- UPDATE -->
5052
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump org.apache.commons:commons-parent from 78 to 79.</action>
5153
</release>

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

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,33 @@ public Base32 get() {
7575
return new Base32(getLineLength(), getLineSeparator(), getEncodeTable(), getPadding(), getDecodingPolicy());
7676
}
7777

78+
/**
79+
* Sets the decode table to use Base32 hexadecimal if {@code true}, otherwise use the Base32 alphabet.
80+
* <p>
81+
* This overrides a value previously set with {@link #setEncodeTable(byte...)}.
82+
* </p>
83+
*
84+
* @param useHex use Base32 hexadecimal if {@code true}, otherwise use the Base32 alphabet.
85+
* @return this instance.
86+
* @since 1.18.0
87+
*/
88+
public Builder setHexDecodeTable(final boolean useHex) {
89+
return setEncodeTable(decodeTable(useHex));
90+
}
91+
92+
/**
93+
* Sets the encode table to use Base32 hexadecimal if {@code true}, otherwise use the Base32 alphabet.
94+
* <p>
95+
* This overrides a value previously set with {@link #setEncodeTable(byte...)}.
96+
* </p>
97+
*
98+
* @param useHex use Base32 hexadecimal if {@code true}, otherwise use the Base32 alphabet.
99+
* @return this instance.
100+
* @since 1.18.0
101+
*/
102+
public Builder setHexEncodeTable(final boolean useHex) {
103+
return setEncodeTable(encodeTable(useHex));
104+
}
78105
}
79106

80107
/**
@@ -162,6 +189,10 @@ public Base32 get() {
162189
/** Mask used to extract 1 bits, used when decoding final trailing character. */
163190
private static final long MASK_1BITS = 0x01L;
164191

192+
// The static final fields above are used for the original static byte[] methods on Base32.
193+
// The private member fields below are used with the new streaming approach, which requires
194+
// some state be preserved between calls of encode() and decode().
195+
165196
/**
166197
* Creates a new Builder.
167198
*
@@ -172,9 +203,13 @@ public static Builder builder() {
172203
return new Builder();
173204
}
174205

175-
// The static final fields above are used for the original static byte[] methods on Base32.
176-
// The private member fields below are used with the new streaming approach, which requires
177-
// some state be preserved between calls of encode() and decode().
206+
private static byte[] decodeTable(final boolean useHex) {
207+
return useHex ? HEX_DECODE_TABLE : DECODE_TABLE;
208+
}
209+
210+
private static byte[] encodeTable(final boolean useHex) {
211+
return useHex ? HEX_ENCODE_TABLE : ENCODE_TABLE;
212+
}
178213

179214
/**
180215
* Decode table to use.
@@ -326,14 +361,14 @@ public Base32(final int lineLength, final byte[] lineSeparator, final boolean us
326361
* @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 &lt;= 0,
327362
* then the output will not be divided into lines (chunks). Ignored when decoding.
328363
* @param lineSeparator Each line of encoded data will end with this sequence of bytes.
329-
* @param useHex if {@code true}, then use Base32 Hex alphabet, otherwise use Base32 alphabet
364+
* @param useHex use Base32 hexadecimal if {@code true}, otherwise use the Base32 alphabet.
330365
* @param padding padding byte.
331366
* @param decodingPolicy The decoding policy.
332367
* @throws IllegalArgumentException Thrown when the {@code lineSeparator} contains Base32 characters. Or the lineLength &gt; 0 and lineSeparator is null.
333368
* @since 1.15
334369
*/
335370
public Base32(final int lineLength, final byte[] lineSeparator, final boolean useHex, final byte padding, final CodecPolicy decodingPolicy) {
336-
this(lineLength, lineSeparator, useHex ? HEX_ENCODE_TABLE : ENCODE_TABLE, padding, decodingPolicy);
371+
this(lineLength, lineSeparator, encodeTable(useHex), padding, decodingPolicy);
337372
}
338373

339374
/**

src/test/java/org/apache/commons/codec/binary/Base32Test.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,22 @@ public void testBase32DecodingOfTrailing5Bits() {
292292
@Test
293293
public void testBase32HexImpossibleSamples() {
294294
testImpossibleCases(new Base32(0, null, true, BaseNCodec.PAD_DEFAULT, CodecPolicy.STRICT), BASE32HEX_IMPOSSIBLE_CASES);
295+
// @formatter:off
296+
testImpossibleCases(Base32.builder()
297+
.setHexEncodeTable(true)
298+
.setDecodingPolicy(CodecPolicy.STRICT)
299+
.get(), BASE32HEX_IMPOSSIBLE_CASES);
300+
// @formatter:on
301+
// overrides, last set wins
302+
// @formatter:off
303+
testImpossibleCases(Base32.builder()
304+
.setHexDecodeTable(false)
305+
.setHexDecodeTable(true)
306+
.setHexEncodeTable(false)
307+
.setHexEncodeTable(true)
308+
.setDecodingPolicy(CodecPolicy.STRICT)
309+
.get(), BASE32HEX_IMPOSSIBLE_CASES);
310+
// @formatter:on
295311
}
296312

297313
@Test

0 commit comments

Comments
 (0)