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-io.git
commit da09365587fa86b488bcbaecc5abe5bbdc62afd5 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Tue Jan 11 09:13:20 2022 -0500 Add CharsetDecoders. --- src/changes/changes.xml | 3 ++ .../{CharsetEncoders.java => CharsetDecoders.java} | 20 ++++---- .../apache/commons/io/charset/CharsetEncoders.java | 6 ++- .../commons/io/charset/CharsetDecdersTest.java | 54 ++++++++++++++++++++++ 4 files changed, 74 insertions(+), 9 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 2229fd3..151b755 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -302,6 +302,9 @@ The <action> type attribute can be add,update,fix,remove. <action dev="ggregory" type="add" due-to="Gary Gregory"> Add CharsetEncoders. </action> + <action dev="ggregory" type="add" due-to="Gary Gregory"> + Add CharsetDecoders. + </action> <!-- UPDATE --> <action dev="ggregory" type="add" due-to="Gary Gregory"> Update FileEntry to use FileTime instead of long for file time stamps. diff --git a/src/main/java/org/apache/commons/io/charset/CharsetEncoders.java b/src/main/java/org/apache/commons/io/charset/CharsetDecoders.java similarity index 62% copy from src/main/java/org/apache/commons/io/charset/CharsetEncoders.java copy to src/main/java/org/apache/commons/io/charset/CharsetDecoders.java index 815aaef..afb7977 100644 --- a/src/main/java/org/apache/commons/io/charset/CharsetEncoders.java +++ b/src/main/java/org/apache/commons/io/charset/CharsetDecoders.java @@ -18,19 +18,23 @@ package org.apache.commons.io.charset; import java.nio.charset.Charset; -import java.nio.charset.CharsetEncoder; +import java.nio.charset.CharsetDecoder; -public class CharsetEncoders { +/** + * Works with {@link CharsetDecoder}. + * + * @since 2.12.0 + */ +public class CharsetDecoders { /** - * Returns the given non-null CharsetEncoder or a new default CharsetEncoder. + * Returns the given non-null CharsetDecoder or a new default CharsetDecoder. * - * @param charsetEncoder The CharsetEncoder to test. - * @return the given non-null CharsetEncoder or a new default CharsetEncoder. - * @since 2.12.0 + * @param charsetDecoder The CharsetDecoder to test. + * @return the given non-null CharsetDecoder or a new default CharsetDecoder. */ - public static CharsetEncoder toCharsetEncoder(CharsetEncoder charsetEncoder) { - return charsetEncoder != null ? charsetEncoder : Charset.defaultCharset().newEncoder(); + public static CharsetDecoder toCharsetDecoder(CharsetDecoder charsetDecoder) { + return charsetDecoder != null ? charsetDecoder : Charset.defaultCharset().newDecoder(); } } diff --git a/src/main/java/org/apache/commons/io/charset/CharsetEncoders.java b/src/main/java/org/apache/commons/io/charset/CharsetEncoders.java index 815aaef..c3ddcad 100644 --- a/src/main/java/org/apache/commons/io/charset/CharsetEncoders.java +++ b/src/main/java/org/apache/commons/io/charset/CharsetEncoders.java @@ -20,6 +20,11 @@ package org.apache.commons.io.charset; import java.nio.charset.Charset; import java.nio.charset.CharsetEncoder; +/** + * Works with {@link CharsetEncoder}. + * + * @since 2.12.0 + */ public class CharsetEncoders { /** @@ -27,7 +32,6 @@ public class CharsetEncoders { * * @param charsetEncoder The CharsetEncoder to test. * @return the given non-null CharsetEncoder or a new default CharsetEncoder. - * @since 2.12.0 */ public static CharsetEncoder toCharsetEncoder(CharsetEncoder charsetEncoder) { return charsetEncoder != null ? charsetEncoder : Charset.defaultCharset().newEncoder(); diff --git a/src/test/java/org/apache/commons/io/charset/CharsetDecdersTest.java b/src/test/java/org/apache/commons/io/charset/CharsetDecdersTest.java new file mode 100644 index 0000000..2595419 --- /dev/null +++ b/src/test/java/org/apache/commons/io/charset/CharsetDecdersTest.java @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.io.charset; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; +import java.nio.charset.StandardCharsets; + +import org.junit.jupiter.api.Test; + +/** + * Tests {@link CharsetDecoders}. + */ +public class CharsetDecdersTest { + + @Test + public void testToCharsetDecoders_default() { + final CharsetDecoder charsetEncoder = CharsetDecoders.toCharsetDecoder(Charset.defaultCharset().newDecoder()); + assertNotNull(charsetEncoder); + assertEquals(Charset.defaultCharset(), charsetEncoder.charset()); + } + + @Test + public void testToCharsetDecoders_ISO_8859_1() { + final CharsetDecoder charsetEncoder = CharsetDecoders.toCharsetDecoder(StandardCharsets.ISO_8859_1.newDecoder()); + assertNotNull(charsetEncoder); + assertEquals(StandardCharsets.ISO_8859_1, charsetEncoder.charset()); + } + + @Test + public void testToCharsetDecoders_null() { + final CharsetDecoder charsetEncoder = CharsetDecoders.toCharsetDecoder(null); + assertNotNull(charsetEncoder); + assertEquals(Charset.defaultCharset(), charsetEncoder.charset()); + } +}