You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2024/04/13 12:37:10 UTC
(commons-codec) 02/04: Refactor common data
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-codec.git
commit ffc24a5d0b6b84d286cbc9a7be278a91b625804b
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Apr 13 08:30:26 2024 -0400
Refactor common data
---
.../org/apache/commons/codec/binary/Base64.java | 2 -
.../java/org/apache/commons/codec/net/BCodec.java | 47 +++-------------------
.../java/org/apache/commons/codec/net/QCodec.java | 39 +-----------------
.../org/apache/commons/codec/net/RFC1522Codec.java | 33 +++++++++++++++
.../apache/commons/codec/net/RFC1522CodecTest.java | 6 +++
...522Codec.java => RFC1522OverrideTestCodec.java} | 7 +++-
6 files changed, 52 insertions(+), 82 deletions(-)
diff --git a/src/main/java/org/apache/commons/codec/binary/Base64.java b/src/main/java/org/apache/commons/codec/binary/Base64.java
index a53cb8b0..9046259a 100644
--- a/src/main/java/org/apache/commons/codec/binary/Base64.java
+++ b/src/main/java/org/apache/commons/codec/binary/Base64.java
@@ -241,7 +241,6 @@ public class Base64 extends BaseNCodec {
if (BinaryCodec.isEmpty(binaryData)) {
return binaryData;
}
-
// Create this so can use the super-class method
// Also ensures that the same roundings are performed by the ctor and the code
final Base64 b64 = isChunked ? new Base64(urlSafe) : new Base64(0, CHUNK_SEPARATOR, urlSafe);
@@ -252,7 +251,6 @@ public class Base64 extends BaseNCodec {
") than the specified maximum size of " +
maxResultSize);
}
-
return b64.encode(binaryData);
}
diff --git a/src/main/java/org/apache/commons/codec/net/BCodec.java b/src/main/java/org/apache/commons/codec/net/BCodec.java
index 3f66e557..fae4520e 100644
--- a/src/main/java/org/apache/commons/codec/net/BCodec.java
+++ b/src/main/java/org/apache/commons/codec/net/BCodec.java
@@ -54,11 +54,6 @@ public class BCodec extends RFC1522Codec implements StringEncoder, StringDecoder
*/
private static final CodecPolicy DECODING_POLICY_DEFAULT = CodecPolicy.LENIENT;
- /**
- * The default Charset used for string decoding and encoding.
- */
- private final Charset charset;
-
/**
* If true then decoding should throw an exception for impossible combinations of bits at the
* end of the byte input. The default is to decode as much of them as possible.
@@ -96,7 +91,7 @@ public class BCodec extends RFC1522Codec implements StringEncoder, StringDecoder
* @since 1.15
*/
public BCodec(final Charset charset, final CodecPolicy decodingPolicy) {
- this.charset = charset;
+ super(charset);
this.decodingPolicy = decodingPolicy;
}
@@ -133,9 +128,7 @@ public class BCodec extends RFC1522Codec implements StringEncoder, StringDecoder
if (value instanceof String) {
return decode((String) value);
}
- throw new DecoderException("Objects of type " +
- value.getClass().getName() +
- " cannot be decoded using BCodec");
+ throw new DecoderException("Objects of type " + value.getClass().getName() + " cannot be decoded using BCodec");
}
/**
@@ -190,9 +183,7 @@ public class BCodec extends RFC1522Codec implements StringEncoder, StringDecoder
if (value instanceof String) {
return encode((String) value);
}
- throw new EncoderException("Objects of type " +
- value.getClass().getName() +
- " cannot be encoded using BCodec");
+ throw new EncoderException("Objects of type " + value.getClass().getName() + " cannot be encoded using BCodec");
}
/**
@@ -206,10 +197,7 @@ public class BCodec extends RFC1522Codec implements StringEncoder, StringDecoder
*/
@Override
public String encode(final String strSource) throws EncoderException {
- if (strSource == null) {
- return null;
- }
- return encode(strSource, this.getCharset());
+ return encode(strSource, getCharset());
}
/**
@@ -225,9 +213,6 @@ public class BCodec extends RFC1522Codec implements StringEncoder, StringDecoder
* @since 1.7
*/
public String encode(final String strSource, final Charset sourceCharset) throws EncoderException {
- if (strSource == null) {
- return null;
- }
return encodeText(strSource, sourceCharset);
}
@@ -243,35 +228,13 @@ public class BCodec extends RFC1522Codec implements StringEncoder, StringDecoder
* thrown if a failure condition is encountered during the encoding process.
*/
public String encode(final String strSource, final String sourceCharset) throws EncoderException {
- if (strSource == null) {
- return null;
- }
try {
- return this.encodeText(strSource, sourceCharset);
+ return encodeText(strSource, sourceCharset);
} catch (final UnsupportedCharsetException e) {
throw new EncoderException(e.getMessage(), e);
}
}
- /**
- * Gets the default Charset name used for string decoding and encoding.
- *
- * @return the default Charset name
- * @since 1.7
- */
- public Charset getCharset() {
- return this.charset;
- }
-
- /**
- * Gets the default Charset name used for string decoding and encoding.
- *
- * @return the default Charset name
- */
- public String getDefaultCharset() {
- return this.charset.name();
- }
-
@Override
protected String getEncoding() {
return "B";
diff --git a/src/main/java/org/apache/commons/codec/net/QCodec.java b/src/main/java/org/apache/commons/codec/net/QCodec.java
index cbb4c941..3dad9bde 100644
--- a/src/main/java/org/apache/commons/codec/net/QCodec.java
+++ b/src/main/java/org/apache/commons/codec/net/QCodec.java
@@ -104,11 +104,6 @@ public class QCodec extends RFC1522Codec implements StringEncoder, StringDecoder
private static final byte UNDERSCORE = 95;
- /**
- * The default Charset used for string decoding and encoding.
- */
- private final Charset charset;
-
private boolean encodeBlanks;
/**
@@ -128,7 +123,7 @@ public class QCodec extends RFC1522Codec implements StringEncoder, StringDecoder
* @since 1.7
*/
public QCodec(final Charset charset) {
- this.charset = charset;
+ super(charset);
}
/**
@@ -246,9 +241,7 @@ public class QCodec extends RFC1522Codec implements StringEncoder, StringDecoder
if (obj instanceof String) {
return encode((String) obj);
}
- throw new EncoderException("Objects of type " +
- obj.getClass().getName() +
- " cannot be encoded using Q codec");
+ throw new EncoderException("Objects of type " + obj.getClass().getName() + " cannot be encoded using Q codec");
}
/**
@@ -262,9 +255,6 @@ public class QCodec extends RFC1522Codec implements StringEncoder, StringDecoder
*/
@Override
public String encode(final String sourceStr) throws EncoderException {
- if (sourceStr == null) {
- return null;
- }
return encode(sourceStr, getCharset());
}
@@ -281,9 +271,6 @@ public class QCodec extends RFC1522Codec implements StringEncoder, StringDecoder
* @since 1.7
*/
public String encode(final String sourceStr, final Charset sourceCharset) throws EncoderException {
- if (sourceStr == null) {
- return null;
- }
return encodeText(sourceStr, sourceCharset);
}
@@ -299,9 +286,6 @@ public class QCodec extends RFC1522Codec implements StringEncoder, StringDecoder
* thrown if a failure condition is encountered during the encoding process.
*/
public String encode(final String sourceStr, final String sourceCharset) throws EncoderException {
- if (sourceStr == null) {
- return null;
- }
try {
return encodeText(sourceStr, sourceCharset);
} catch (final UnsupportedCharsetException e) {
@@ -309,25 +293,6 @@ public class QCodec extends RFC1522Codec implements StringEncoder, StringDecoder
}
}
- /**
- * Gets the default Charset name used for string decoding and encoding.
- *
- * @return the default Charset name
- * @since 1.7
- */
- public Charset getCharset() {
- return this.charset;
- }
-
- /**
- * Gets the default Charset name used for string decoding and encoding.
- *
- * @return the default Charset name
- */
- public String getDefaultCharset() {
- return this.charset.name();
- }
-
@Override
protected String getEncoding() {
return "Q";
diff --git a/src/main/java/org/apache/commons/codec/net/RFC1522Codec.java b/src/main/java/org/apache/commons/codec/net/RFC1522Codec.java
index c0e63a9e..39064c52 100644
--- a/src/main/java/org/apache/commons/codec/net/RFC1522Codec.java
+++ b/src/main/java/org/apache/commons/codec/net/RFC1522Codec.java
@@ -20,6 +20,7 @@ package org.apache.commons.codec.net;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
+import java.util.Objects;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.EncoderException;
@@ -51,6 +52,15 @@ abstract class RFC1522Codec {
/** Postfix. */
protected static final String PREFIX = "=?";
+ /**
+ * The default Charset used for string decoding and encoding.
+ */
+ protected final Charset charset;
+
+ RFC1522Codec(final Charset charset) {
+ this.charset = Objects.requireNonNull(charset, "charset");
+ }
+
/**
* Applies an RFC 1522 compliant decoding scheme to the given string of text.
* <p>
@@ -171,9 +181,32 @@ abstract class RFC1522Codec {
* @see Charset
*/
protected String encodeText(final String text, final String charsetName) throws EncoderException {
+ if (text == null) {
+ // Don't attempt charsetName conversion.
+ return null;
+ }
return encodeText(text, Charset.forName(charsetName));
}
+ /**
+ * Gets the default Charset name used for string decoding and encoding.
+ *
+ * @return the default Charset name
+ * @since 1.7
+ */
+ public Charset getCharset() {
+ return charset;
+ }
+
+ /**
+ * Gets the default Charset name used for string decoding and encoding.
+ *
+ * @return the default Charset name
+ */
+ public String getDefaultCharset() {
+ return charset.name();
+ }
+
/**
* Returns the codec name (referred to as encoding in the RFC 1522).
*
diff --git a/src/test/java/org/apache/commons/codec/net/RFC1522CodecTest.java b/src/test/java/org/apache/commons/codec/net/RFC1522CodecTest.java
index b1156000..9e566a81 100644
--- a/src/test/java/org/apache/commons/codec/net/RFC1522CodecTest.java
+++ b/src/test/java/org/apache/commons/codec/net/RFC1522CodecTest.java
@@ -20,6 +20,8 @@ package org.apache.commons.codec.net;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
+import java.nio.charset.StandardCharsets;
+
import org.apache.commons.codec.CharEncoding;
import org.apache.commons.codec.DecoderException;
import org.junit.jupiter.api.Test;
@@ -31,6 +33,10 @@ public class RFC1522CodecTest {
static class RFC1522TestCodec extends RFC1522Codec {
+ RFC1522TestCodec() {
+ super(StandardCharsets.UTF_8);
+ }
+
@Override
protected byte[] doDecoding(final byte[] bytes) {
return bytes;
diff --git a/src/test/java/org/apache/commons/codec/net/CustomRFC1522Codec.java b/src/test/java/org/apache/commons/codec/net/RFC1522OverrideTestCodec.java
similarity index 91%
rename from src/test/java/org/apache/commons/codec/net/CustomRFC1522Codec.java
rename to src/test/java/org/apache/commons/codec/net/RFC1522OverrideTestCodec.java
index 1c99d4aa..c643d9da 100644
--- a/src/test/java/org/apache/commons/codec/net/CustomRFC1522Codec.java
+++ b/src/test/java/org/apache/commons/codec/net/RFC1522OverrideTestCodec.java
@@ -19,6 +19,7 @@ package org.apache.commons.codec.net;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.EncoderException;
@@ -26,7 +27,11 @@ import org.apache.commons.codec.EncoderException;
/**
* Tests overriding the package private RFC1522Codec.
*/
-public class CustomRFC1522Codec extends RFC1522Codec {
+class RFC1522OverrideTestCodec extends RFC1522Codec {
+
+ RFC1522OverrideTestCodec() {
+ super(StandardCharsets.UTF_8);
+ }
@Override
protected String decodeText(final String text) throws DecoderException, UnsupportedEncodingException {