Skip to content

Commit ffc24a5

Browse files
committed
Refactor common data
1 parent d9f0c46 commit ffc24a5

6 files changed

Lines changed: 52 additions & 82 deletions

File tree

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,6 @@ public static byte[] encodeBase64(final byte[] binaryData, final boolean isChunk
241241
if (BinaryCodec.isEmpty(binaryData)) {
242242
return binaryData;
243243
}
244-
245244
// Create this so can use the super-class method
246245
// Also ensures that the same roundings are performed by the ctor and the code
247246
final Base64 b64 = isChunked ? new Base64(urlSafe) : new Base64(0, CHUNK_SEPARATOR, urlSafe);
@@ -252,7 +251,6 @@ public static byte[] encodeBase64(final byte[] binaryData, final boolean isChunk
252251
") than the specified maximum size of " +
253252
maxResultSize);
254253
}
255-
256254
return b64.encode(binaryData);
257255
}
258256

src/main/java/org/apache/commons/codec/net/BCodec.java

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,6 @@ public class BCodec extends RFC1522Codec implements StringEncoder, StringDecoder
5454
*/
5555
private static final CodecPolicy DECODING_POLICY_DEFAULT = CodecPolicy.LENIENT;
5656

57-
/**
58-
* The default Charset used for string decoding and encoding.
59-
*/
60-
private final Charset charset;
61-
6257
/**
6358
* If true then decoding should throw an exception for impossible combinations of bits at the
6459
* end of the byte input. The default is to decode as much of them as possible.
@@ -96,7 +91,7 @@ public BCodec(final Charset charset) {
9691
* @since 1.15
9792
*/
9893
public BCodec(final Charset charset, final CodecPolicy decodingPolicy) {
99-
this.charset = charset;
94+
super(charset);
10095
this.decodingPolicy = decodingPolicy;
10196
}
10297

@@ -133,9 +128,7 @@ public Object decode(final Object value) throws DecoderException {
133128
if (value instanceof String) {
134129
return decode((String) value);
135130
}
136-
throw new DecoderException("Objects of type " +
137-
value.getClass().getName() +
138-
" cannot be decoded using BCodec");
131+
throw new DecoderException("Objects of type " + value.getClass().getName() + " cannot be decoded using BCodec");
139132
}
140133

141134
/**
@@ -190,9 +183,7 @@ public Object encode(final Object value) throws EncoderException {
190183
if (value instanceof String) {
191184
return encode((String) value);
192185
}
193-
throw new EncoderException("Objects of type " +
194-
value.getClass().getName() +
195-
" cannot be encoded using BCodec");
186+
throw new EncoderException("Objects of type " + value.getClass().getName() + " cannot be encoded using BCodec");
196187
}
197188

198189
/**
@@ -206,10 +197,7 @@ public Object encode(final Object value) throws EncoderException {
206197
*/
207198
@Override
208199
public String encode(final String strSource) throws EncoderException {
209-
if (strSource == null) {
210-
return null;
211-
}
212-
return encode(strSource, this.getCharset());
200+
return encode(strSource, getCharset());
213201
}
214202

215203
/**
@@ -225,9 +213,6 @@ public String encode(final String strSource) throws EncoderException {
225213
* @since 1.7
226214
*/
227215
public String encode(final String strSource, final Charset sourceCharset) throws EncoderException {
228-
if (strSource == null) {
229-
return null;
230-
}
231216
return encodeText(strSource, sourceCharset);
232217
}
233218

@@ -243,35 +228,13 @@ public String encode(final String strSource, final Charset sourceCharset) throws
243228
* thrown if a failure condition is encountered during the encoding process.
244229
*/
245230
public String encode(final String strSource, final String sourceCharset) throws EncoderException {
246-
if (strSource == null) {
247-
return null;
248-
}
249231
try {
250-
return this.encodeText(strSource, sourceCharset);
232+
return encodeText(strSource, sourceCharset);
251233
} catch (final UnsupportedCharsetException e) {
252234
throw new EncoderException(e.getMessage(), e);
253235
}
254236
}
255237

256-
/**
257-
* Gets the default Charset name used for string decoding and encoding.
258-
*
259-
* @return the default Charset name
260-
* @since 1.7
261-
*/
262-
public Charset getCharset() {
263-
return this.charset;
264-
}
265-
266-
/**
267-
* Gets the default Charset name used for string decoding and encoding.
268-
*
269-
* @return the default Charset name
270-
*/
271-
public String getDefaultCharset() {
272-
return this.charset.name();
273-
}
274-
275238
@Override
276239
protected String getEncoding() {
277240
return "B";

src/main/java/org/apache/commons/codec/net/QCodec.java

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,6 @@ public class QCodec extends RFC1522Codec implements StringEncoder, StringDecoder
104104

105105
private static final byte UNDERSCORE = 95;
106106

107-
/**
108-
* The default Charset used for string decoding and encoding.
109-
*/
110-
private final Charset charset;
111-
112107
private boolean encodeBlanks;
113108

114109
/**
@@ -128,7 +123,7 @@ public QCodec() {
128123
* @since 1.7
129124
*/
130125
public QCodec(final Charset charset) {
131-
this.charset = charset;
126+
super(charset);
132127
}
133128

134129
/**
@@ -246,9 +241,7 @@ public Object encode(final Object obj) throws EncoderException {
246241
if (obj instanceof String) {
247242
return encode((String) obj);
248243
}
249-
throw new EncoderException("Objects of type " +
250-
obj.getClass().getName() +
251-
" cannot be encoded using Q codec");
244+
throw new EncoderException("Objects of type " + obj.getClass().getName() + " cannot be encoded using Q codec");
252245
}
253246

254247
/**
@@ -262,9 +255,6 @@ public Object encode(final Object obj) throws EncoderException {
262255
*/
263256
@Override
264257
public String encode(final String sourceStr) throws EncoderException {
265-
if (sourceStr == null) {
266-
return null;
267-
}
268258
return encode(sourceStr, getCharset());
269259
}
270260

@@ -281,9 +271,6 @@ public String encode(final String sourceStr) throws EncoderException {
281271
* @since 1.7
282272
*/
283273
public String encode(final String sourceStr, final Charset sourceCharset) throws EncoderException {
284-
if (sourceStr == null) {
285-
return null;
286-
}
287274
return encodeText(sourceStr, sourceCharset);
288275
}
289276

@@ -299,35 +286,13 @@ public String encode(final String sourceStr, final Charset sourceCharset) throws
299286
* thrown if a failure condition is encountered during the encoding process.
300287
*/
301288
public String encode(final String sourceStr, final String sourceCharset) throws EncoderException {
302-
if (sourceStr == null) {
303-
return null;
304-
}
305289
try {
306290
return encodeText(sourceStr, sourceCharset);
307291
} catch (final UnsupportedCharsetException e) {
308292
throw new EncoderException(e.getMessage(), e);
309293
}
310294
}
311295

312-
/**
313-
* Gets the default Charset name used for string decoding and encoding.
314-
*
315-
* @return the default Charset name
316-
* @since 1.7
317-
*/
318-
public Charset getCharset() {
319-
return this.charset;
320-
}
321-
322-
/**
323-
* Gets the default Charset name used for string decoding and encoding.
324-
*
325-
* @return the default Charset name
326-
*/
327-
public String getDefaultCharset() {
328-
return this.charset.name();
329-
}
330-
331296
@Override
332297
protected String getEncoding() {
333298
return "Q";

src/main/java/org/apache/commons/codec/net/RFC1522Codec.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.UnsupportedEncodingException;
2121
import java.nio.charset.Charset;
2222
import java.nio.charset.UnsupportedCharsetException;
23+
import java.util.Objects;
2324

2425
import org.apache.commons.codec.DecoderException;
2526
import org.apache.commons.codec.EncoderException;
@@ -51,6 +52,15 @@ abstract class RFC1522Codec {
5152
/** Postfix. */
5253
protected static final String PREFIX = "=?";
5354

55+
/**
56+
* The default Charset used for string decoding and encoding.
57+
*/
58+
protected final Charset charset;
59+
60+
RFC1522Codec(final Charset charset) {
61+
this.charset = Objects.requireNonNull(charset, "charset");
62+
}
63+
5464
/**
5565
* Applies an RFC 1522 compliant decoding scheme to the given string of text.
5666
* <p>
@@ -171,9 +181,32 @@ protected String encodeText(final String text, final Charset charset) throws Enc
171181
* @see Charset
172182
*/
173183
protected String encodeText(final String text, final String charsetName) throws EncoderException {
184+
if (text == null) {
185+
// Don't attempt charsetName conversion.
186+
return null;
187+
}
174188
return encodeText(text, Charset.forName(charsetName));
175189
}
176190

191+
/**
192+
* Gets the default Charset name used for string decoding and encoding.
193+
*
194+
* @return the default Charset name
195+
* @since 1.7
196+
*/
197+
public Charset getCharset() {
198+
return charset;
199+
}
200+
201+
/**
202+
* Gets the default Charset name used for string decoding and encoding.
203+
*
204+
* @return the default Charset name
205+
*/
206+
public String getDefaultCharset() {
207+
return charset.name();
208+
}
209+
177210
/**
178211
* Returns the codec name (referred to as encoding in the RFC 1522).
179212
*

src/test/java/org/apache/commons/codec/net/RFC1522CodecTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import static org.junit.jupiter.api.Assertions.assertNull;
2121
import static org.junit.jupiter.api.Assertions.assertThrows;
2222

23+
import java.nio.charset.StandardCharsets;
24+
2325
import org.apache.commons.codec.CharEncoding;
2426
import org.apache.commons.codec.DecoderException;
2527
import org.junit.jupiter.api.Test;
@@ -31,6 +33,10 @@ public class RFC1522CodecTest {
3133

3234
static class RFC1522TestCodec extends RFC1522Codec {
3335

36+
RFC1522TestCodec() {
37+
super(StandardCharsets.UTF_8);
38+
}
39+
3440
@Override
3541
protected byte[] doDecoding(final byte[] bytes) {
3642
return bytes;

src/test/java/org/apache/commons/codec/net/CustomRFC1522Codec.java renamed to src/test/java/org/apache/commons/codec/net/RFC1522OverrideTestCodec.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,19 @@
1919

2020
import java.io.UnsupportedEncodingException;
2121
import java.nio.charset.Charset;
22+
import java.nio.charset.StandardCharsets;
2223

2324
import org.apache.commons.codec.DecoderException;
2425
import org.apache.commons.codec.EncoderException;
2526

2627
/**
2728
* Tests overriding the package private RFC1522Codec.
2829
*/
29-
public class CustomRFC1522Codec extends RFC1522Codec {
30+
class RFC1522OverrideTestCodec extends RFC1522Codec {
31+
32+
RFC1522OverrideTestCodec() {
33+
super(StandardCharsets.UTF_8);
34+
}
3035

3136
@Override
3237
protected String decodeText(final String text) throws DecoderException, UnsupportedEncodingException {

0 commit comments

Comments
 (0)